想定解とは違う形で解いてしまった。
https://yukicoder.me/problems/no/619
問題
整数と記号(加算または乗算)が交互に並んだ式が与えられる。
以下のクエリに順次処理せよ。
- 指定された2か所の数字を交換する
- 指定された2か所の記号を交換する
- 指定された部分列を数式とみなし計算結果を答える
解法
想定解はSegTree。
部分列に対し+記号の有無により、(X+Y+Z)またはXの形で数字を持ち、隣の要素と連結していく。
自分は別の解法を取った。
記号は無視して、連続する数字の列に対し積をとるSegTreeと、総和を取るBITを用いる。
式において、乗算でまとめられた一連の項に対し、先頭に数字を考える。
つまり、A*_*_*_+B*_*_*_+C*_*_*_+D*_*_*_+....という式があればA,B,C,Dを考える。
これらに対してのみまとまった項の積を取り、BITに乗せる。
この式の部分式の計算をすることを考える。例えば元の式において、括弧で囲まれた部分を考える。
A*_*(_*_+B*_*_*_+C*_*_*_+D*_*_)*_+....
積でまとめられた項が分断されるのは、AとDでまとめられた高々2か所である。
よってこれらの2か所は個別に積を求め、項全体が区間に入る場合はBITで総和を求める。
数字や記号の変更に対しては、SegTreeやBIT、および積でまとめられた項の先頭を管理していけばよい。
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--) { 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]); } } } 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; } } } }
まとめ
ゴリ押ししたけどどうにかなった。