結果

問題 No.619 CardShuffle
ユーザー heyshbheyshb
提出日時 2017-12-19 18:11:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 314 ms / 3,000 ms
コード長 2,816 bytes
コンパイル時間 1,198 ms
コンパイル使用メモリ 160,540 KB
実行使用メモリ 9,932 KB
最終ジャッジ日時 2024-06-02 05:02:22
合計ジャッジ時間 6,257 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,772 KB
testcase_01 AC 2 ms
9,808 KB
testcase_02 AC 2 ms
7,764 KB
testcase_03 AC 2 ms
9,804 KB
testcase_04 AC 2 ms
7,900 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
7,904 KB
testcase_07 AC 2 ms
7,892 KB
testcase_08 AC 2 ms
9,680 KB
testcase_09 AC 2 ms
9,804 KB
testcase_10 AC 2 ms
7,904 KB
testcase_11 AC 2 ms
9,808 KB
testcase_12 AC 3 ms
7,904 KB
testcase_13 AC 2 ms
7,644 KB
testcase_14 AC 2 ms
7,772 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 229 ms
8,280 KB
testcase_17 AC 135 ms
9,672 KB
testcase_18 AC 237 ms
8,152 KB
testcase_19 AC 133 ms
9,908 KB
testcase_20 AC 161 ms
8,160 KB
testcase_21 AC 214 ms
8,156 KB
testcase_22 AC 84 ms
8,268 KB
testcase_23 AC 215 ms
9,904 KB
testcase_24 AC 82 ms
8,264 KB
testcase_25 AC 94 ms
8,280 KB
testcase_26 AC 247 ms
8,288 KB
testcase_27 AC 192 ms
9,676 KB
testcase_28 AC 245 ms
9,804 KB
testcase_29 AC 195 ms
9,804 KB
testcase_30 AC 227 ms
9,932 KB
testcase_31 AC 2 ms
9,816 KB
testcase_32 AC 145 ms
9,680 KB
testcase_33 AC 172 ms
8,152 KB
testcase_34 AC 314 ms
9,804 KB
testcase_35 AC 36 ms
7,772 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:129:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  129 |         scanf("%d",&N);
      |         ~~~~~^~~~~~~~~
main.cpp:140:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  140 |                 scanf("%s",tmp);
      |                 ~~~~~^~~~~~~~~~
main.cpp:150:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  150 |         scanf("%d",&Q);
      |         ~~~~~^~~~~~~~~
main.cpp:155:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  155 |                 scanf("%s",tmp);
      |                 ~~~~~^~~~~~~~~~
main.cpp:156:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  156 |                 scanf("%d%d",&x,&y);
      |                 ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MOD = 1e9+7;
int N;
char v[200010];
const int blk = 200;
int bl[200010],L[200010],R[200010];
int S[200010],rv[200010],lv[200010],mv[200010],B;
int fp[200010],lp[200010];

void upd(int &x)
{
	if (x < 0) x += MOD;
	if (x >= MOD) x -= MOD;
}

int calc(int l,int r)//num---num
{
	assert(!(l==1 && r==100000));
	int sum = 0,nowv = 1;
	//printf("calc %d %d\n",l,r);
	for (int i=l;i<=r;i+=2)
	{
		nowv = (1LL * nowv * (v[i] - '0')) % MOD;
		//printf("%d %d\n",i,nowv);
		if (v[i+1] == '+' || i==r)
		{
			upd(sum += nowv);
			nowv = 1;
		}
	}
	//printf("calc %d %d=%d\n",l,r,sum);
	return sum;
}

void build(int b)
{
	fp[b] = lp[b] = 0;
	for (int i=L[b]+1;i<R[b];i+=2)
	{
		if (v[i] == '+')
		{
			if (!fp[b]) fp[b] = i;
			lp[b] = i;
		}
	}
	//printf("start calc %d~%d %d %d\n",L[b],R[b],fp[b],lp[b]);
	S[b] = calc(L[b],R[b]-1);
	if (fp[b])
	{
		lv[b] = calc(L[b],fp[b]-1);
		rv[b] = calc(lp[b]+1,R[b]-1);
		mv[b] = S[b] - lv[b];
		upd(mv[b]);
		upd(mv[b] -= rv[b]);
	}
}

int query(int x,int y)
{
	//printf("query %d %d\n",x,y);
	if (bl[x] == bl[y]) return calc(x,y);
	int sum = 0,nowv = 1; 
	for (int i=x;i<R[bl[x]];i+=2)
	{
		nowv = 1LL * nowv * (v[i] - '0') % MOD;
		if (v[i+1] == '+')
		{
			upd(sum += nowv);
			nowv = 1;	
		}
	}
	//printf("%d %d\n",sum,nowv);
	for (int i=bl[x]+1;i<bl[y];i++)
	{
		//printf("blk %d\n",i); 
		if (v[R[i]] == '+')
		{
			if (fp[i])
			{
				nowv = 1LL * nowv * lv[i] % MOD;
				upd(sum += nowv);
				upd(sum += S[i]);
				upd(sum -= lv[i]);
			}
			else
			{
				nowv = 1LL * nowv * S[i] % MOD;
				upd(sum += nowv);
			}
			nowv = 1;
		}
		else
		{
			if (fp[i])
			{
				nowv = 1LL * nowv * lv[i] % MOD;
				upd(sum += nowv);
				upd(sum += mv[i]);
				nowv = rv[i];
			}
			else
			{
				nowv = 1LL * nowv * S[i] % MOD;
			}
		}
		//printf("%d %d\n",sum,nowv);
	}
	for (int i=L[bl[y]];i<=y;i+=2)
	{
		nowv = 1LL * nowv * (v[i] - '0') % MOD;
		if (v[i+1] == '+' || i==y)
		{
			upd(sum += nowv);
			nowv = 1;	
		}
	}
	//printf("%d %d\n",sum,nowv);
	return sum;
}

int main()
{
	//freopen("619.in","r",stdin);
	//freopen("619.out","w",stdout);
	scanf("%d",&N);
	for (int i=1;i<=N;i++)bl[i] = (i - 1) / blk + 1;
	B = bl[N];
	for (int i=1;i<=N;i++)
	{
		if (bl[i] != bl[i-1]) L[bl[i]] = i;
		if (bl[i] != bl[i+1]) R[bl[i]] = i;
	}
	for (int i=1;i<=N;i++)
	{
		char tmp[2];
		scanf("%s",tmp);
		v[i] = tmp[0];
	}
	
	for (int i=1;i<=B;i++)
	{
		build(i);
		//printf("%d: fp=%d lp=%d S=%d\n",i,fp[i],lp[i],S[i]);
	}
	int Q;
	scanf("%d",&Q);
	while(Q--)
	{
		char tmp[2];
		int x,y;
		scanf("%s",tmp);
		scanf("%d%d",&x,&y);
		if (tmp[0] == '?')
		{
			printf("%d\n",query(x,y));
		}
		else
		{
			//puts("wow");
			if (v[x] != v[y])
			{
				swap(v[x],v[y]);
				build(bl[x]);
				build(bl[y]);
			}
		}
	}
}
0