結果

問題 No.619 CardShuffle
ユーザー Sebastian KingSebastian King
提出日時 2017-12-19 18:20:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 639 ms / 3,000 ms
コード長 2,610 bytes
コンパイル時間 1,454 ms
コンパイル使用メモリ 145,796 KB
実行使用メモリ 9,908 KB
最終ジャッジ日時 2023-08-24 14:54:32
合計ジャッジ時間 11,836 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
9,664 KB
testcase_01 AC 3 ms
9,632 KB
testcase_02 AC 3 ms
9,668 KB
testcase_03 AC 3 ms
9,568 KB
testcase_04 AC 3 ms
9,648 KB
testcase_05 AC 3 ms
9,628 KB
testcase_06 AC 3 ms
9,632 KB
testcase_07 AC 3 ms
9,644 KB
testcase_08 AC 3 ms
9,648 KB
testcase_09 AC 3 ms
9,908 KB
testcase_10 AC 3 ms
9,576 KB
testcase_11 AC 3 ms
9,644 KB
testcase_12 AC 3 ms
9,648 KB
testcase_13 AC 3 ms
9,628 KB
testcase_14 AC 3 ms
9,724 KB
testcase_15 AC 3 ms
9,636 KB
testcase_16 AC 615 ms
9,880 KB
testcase_17 AC 381 ms
9,672 KB
testcase_18 AC 613 ms
9,668 KB
testcase_19 AC 383 ms
9,652 KB
testcase_20 AC 317 ms
9,868 KB
testcase_21 AC 639 ms
9,648 KB
testcase_22 AC 342 ms
9,736 KB
testcase_23 AC 638 ms
9,656 KB
testcase_24 AC 341 ms
9,756 KB
testcase_25 AC 341 ms
9,648 KB
testcase_26 AC 574 ms
9,704 KB
testcase_27 AC 406 ms
9,680 KB
testcase_28 AC 571 ms
9,700 KB
testcase_29 AC 409 ms
9,636 KB
testcase_30 AC 293 ms
9,576 KB
testcase_31 AC 2 ms
9,668 KB
testcase_32 AC 316 ms
9,648 KB
testcase_33 AC 616 ms
9,656 KB
testcase_34 AC 605 ms
9,652 KB
testcase_35 AC 41 ms
9,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

const int MM = 1e9 + 7;
const double eps = 1e-8;
const int MAXN = 2e6 + 10;

int n, m;

void prework(){

}

void read(){

}

char s[100];
ll a[MAXN];
int L[MAXN], R[MAXN];

const int MAKI = 9;

struct node{
	int tp;
	ll a, b, c;
} rst[MAXN], nico;

void Getans(node &ret, int l, int r){
	int p = -1, q = -1;
	for (int i = l + 1; i < r; i += 2)
		if (a[i] == -1){
			if (p == -1) p = i;
			q = i;
		}
	if (p == -1){
		ret.tp = 1;
		ll tmp = 1;
		for (int i = l; i <= r; i += 2)
			tmp = (tmp * a[i] % MM);
		ret.a = tmp;
	}
	else{
		ret.tp = 2;
		ll tmp = 1;
		for (int i = l; i < p; i += 2)
			tmp = (tmp * a[i] % MM);
		ret.a = tmp;
		tmp = 1;
		for (int i = q + 1; i <= r; i += 2)
			tmp = tmp * a[i] % MM;
		ret.c = tmp;
		ll sum = 0;
		tmp = 1;
		for (int i = p + 1; i < q; i += 2){
			tmp = tmp * a[i] % MM;
			if (a[i + 1] == -1){
				sum = (sum + tmp) % MM;
				tmp = 1;
			}
		}
		ret.b = sum;
	}
}

void fill_in(int &pos, node &x, int opt){
	if (x.tp == 1){
		a[pos++] = x.a;
		a[pos++] = opt;
	}
	else{
		a[pos++] = x.a;
		a[pos++] = -1;
		a[pos++] = x.b;
		a[pos++] = -1;
		a[pos++] = x.c;
		a[pos++] = opt;
	}
}

void solve(int casi){
//	cout << "Case #" << casi << ": ";
	int M, Q;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++){
		scanf("%s", s);
		if (s[0] == '+') a[i] = -1;
		else if (s[0] == '*') a[i] = -2;
		else a[i] = s[0] - '0';
	}
	M = n >> MAKI;
	for (int i = 0; i <= M; i++){
		L[i] = (i << MAKI) + 1;
		R[i] = ((i + 1) << MAKI) - 1;
	}
	L[0] = 1, R[M] = n;
	for (int i = 0; i <= M; i++)
		Getans(rst[i], L[i], R[i]);
	scanf("%d", &Q);
	while(Q--){
		int x, y;
		scanf("%s", s);
		scanf("%d%d", &x, &y);
		int p = x >> MAKI, q = y >> MAKI;
		if (s[0] == '?'){
			int l = n + 2, r = n + 2;
			if (p == q || p + 1 == q){
				Getans(nico, x, y);
				if (nico.tp == 1) printf("%lld\n", nico.a);
				else printf("%lld\n", (nico.a+nico.b+nico.c)%MM);
			}
			else{
				Getans(nico, x, R[p]);
				fill_in(r, nico, a[R[p] + 1]);
				for (int i = p + 1; i < q; i++)
					fill_in(r, rst[i], a[R[i] + 1]);
				Getans(nico, L[q], y);
				fill_in(r, nico, -1);
				r -= 2;
				Getans(nico, l, r);
				if (nico.tp == 1) printf("%lld\n", nico.a);
				else printf("%lld\n", (nico.a+nico.b+nico.c)%MM);
			}
		}
		else{
			swap(a[x], a[y]);
			Getans(rst[p], L[p], R[p]);
			Getans(rst[q], L[q], R[q]);
		}
	}
}

void printans(){

}


int main(){
//	std::ios::sync_with_stdio(false);
	prework();
	int T = 1;
//	cin>>T;
	for(int i = 1; i <= T; i++){
		read();
		solve(i);
		printans();
	}
	return 0;
}

0