結果

問題 No.619 CardShuffle
ユーザー 👑 kmjpkmjp
提出日時 2017-12-19 00:46:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 338 ms / 3,000 ms
コード長 3,670 bytes
コンパイル時間 4,260 ms
コンパイル使用メモリ 170,336 KB
実行使用メモリ 18,296 KB
最終ジャッジ日時 2023-08-24 14:09:19
合計ジャッジ時間 11,735 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
15,372 KB
testcase_01 AC 6 ms
15,172 KB
testcase_02 AC 5 ms
15,348 KB
testcase_03 AC 5 ms
15,344 KB
testcase_04 AC 6 ms
15,116 KB
testcase_05 AC 5 ms
15,112 KB
testcase_06 AC 5 ms
15,308 KB
testcase_07 AC 6 ms
15,372 KB
testcase_08 AC 6 ms
15,304 KB
testcase_09 AC 5 ms
15,412 KB
testcase_10 AC 6 ms
15,212 KB
testcase_11 AC 6 ms
15,172 KB
testcase_12 AC 6 ms
15,216 KB
testcase_13 AC 6 ms
15,412 KB
testcase_14 AC 5 ms
15,124 KB
testcase_15 AC 5 ms
15,340 KB
testcase_16 AC 306 ms
16,908 KB
testcase_17 AC 277 ms
18,252 KB
testcase_18 AC 312 ms
16,964 KB
testcase_19 AC 277 ms
18,232 KB
testcase_20 AC 179 ms
15,244 KB
testcase_21 AC 270 ms
16,964 KB
testcase_22 AC 218 ms
18,296 KB
testcase_23 AC 273 ms
16,968 KB
testcase_24 AC 215 ms
18,256 KB
testcase_25 AC 94 ms
15,112 KB
testcase_26 AC 334 ms
16,972 KB
testcase_27 AC 338 ms
18,248 KB
testcase_28 AC 328 ms
16,908 KB
testcase_29 AC 338 ms
18,228 KB
testcase_30 AC 262 ms
15,120 KB
testcase_31 AC 5 ms
15,112 KB
testcase_32 AC 181 ms
15,216 KB
testcase_33 AC 303 ms
16,928 KB
testcase_34 AC 304 ms
16,976 KB
testcase_35 AC 196 ms
15,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
int A[101010];
ll V[101010];
string S[101010];
ll mo=1000000007;

template<class V,int NV> class SegTree_mul {
public:
	vector<V> val;
	static V const def=1;
	V comp(V l,V r){ return l*r%mo;};
	
	SegTree_mul(){val=vector<V>(NV*2,def);};
	V getval(int x,int y,int l=0,int r=NV,int k=1) { // x<=i<y
		if(r<=x || y<=l) return 1;
		if(x<=l && r<=y) return val[k];
		return comp(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry]=v;
		while(entry>1) entry>>=1, val[entry]=comp(val[entry*2],val[entry*2+1]);
	}
};
SegTree_mul<ll,1<<18> st;

template<class V, int ME> class BIT {
public:
	V bit[1<<ME];
	V operator()(int e) {if(e<0) return 0;V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
	void add(int e,V v) { e++; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
};
BIT<ll,20> bt;



void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	set<int> C;
	C.insert(0);
	C.insert((N+1)/2);
	FOR(i,N) {
		if(i%2==0) {
			cin>>A[i/2];
			st.update(i/2,A[i/2]);
		}
		else {
			cin>>S[i/2];
			if(S[i/2]=="+") C.insert((i+1)/2);
		}
	}
	int pre=-1;
	FORR(r,C) {
		if(pre!=-1) {
			V[pre]=st.getval(pre,r);
			bt.add(pre,V[pre]);
		}
		pre=r;
	}
	
	cin>>M;
	while(M--) {
		/*
		FOR(i,N/2+1) cout<<st.getval(i,i+1)<<" ";
		FOR(i,N/2+1) cout<<bt(i)-bt(i-1)<<" ";
		cout<<endl;
		*/
		
		cin>>s>>x>>y;
		if(s=="!") {
			x--,y--;
			if(x%2==0) {
				x/=2;
				y/=2;
				auto itx=C.lower_bound(x+1);
				int nx=*itx;
				i=*(--itx);
				auto ity=C.lower_bound(y+1);
				int ny=*ity;
				j=*(--ity);
				
				if(i==j) {
					swap(A[x],A[y]);
					st.update(x,A[x]);
					st.update(y,A[y]);
				}
				else {
					bt.add(i,-V[i]);
					bt.add(j,-V[j]);
					swap(A[x],A[y]);
					st.update(x,A[x]);
					st.update(y,A[y]);
					V[i]=st.getval(i,nx);
					V[j]=st.getval(j,ny);
					bt.add(i,V[i]);
					bt.add(j,V[j]);
				}
			}
			else {
				x/=2;
				y/=2;
				if(S[x]==S[y]) continue;
				set<int> cand;
				
				if(S[x]=="+") swap(x,y);
				auto it=C.lower_bound(x+1);
				cand.insert(*(--it));
				it=C.lower_bound(y+1);
				cand.insert(*(--it));
				
				C.insert(x+1);
				C.erase(y+1);
				cand.insert(x+1);
				cand.insert(y+1);
				swap(S[x],S[y]);
				FORR(c,cand) {
					bt.add(c,-V[c]);
					V[c]=0;
					if(c>0 && S[c-1]=="*") continue;
					it=C.lower_bound(c+1);
					V[c]=st.getval(c,*it);
					bt.add(c,V[c]);
				}
				
			}
			/*
			{
				set<int> cand;
				FOR(i,N/2+1) cand.insert(i);
				FORR(c,cand) {
					bt.add(c,-V[c]);
					V[c]=0;
					if(c>0 && S[c-1]=="*") continue;
					auto it=C.lower_bound(c+1);
					V[c]=st.getval(c,*it);
					bt.add(c,V[c]);
				}
			}
			*/
		}
		else {
			x/=2;
			y/=2;
			auto itx=C.lower_bound(x);
			auto ity=C.lower_bound(y+1);
			if(itx==ity) {
				cout<<st.getval(x,y+1)<<endl;
			}
			else {
				ll ret=0;
				if(x<*itx) ret+=st.getval(x,*itx);
				ity--;
				ret+=st.getval(*ity,y+1);
				ret+=bt(*ity-1)-bt(*itx-1);
				cout<<(ret%mo+mo)%mo<<endl;
				
			}
			
		}
	}
	
	
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0