Test Instructions:Link
Method:Segment Tree
parsing:This problem just began to think of an O (n/2* (LOGN) *m) of the brain extraction algorithm wrote =-= but then I magically wrote an O (sqrt (n)/2* (LOGN) *m). At that time, thinking about what the other piece of something has not been brain =-= and then began to find patterns. You might as well put your weight on the point. Then consider what the contribution of a point is. The number of points on the left and the number of dots on the right. It's obvious that you're enumerating the left and right ends of a long line segment. So assuming that the weight of a point is Val then that is Val[i]
(i-x+1)(y-i+1), right. Then it was unfolded. Unfold and found we only need to maintain val[i] interval and, val[i]*i interval and, val[i]*i*i of the interval and just fine. The two math formulas are done.
Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 100100Using namespace Std;typedef long Long ll;ll sumv[n<<2];ll sumvi[n<<2];ll sumvii[n<<2];ll col[n<<2];ll c[n][4];intNm; chars[5];ll GCD (LLx, LLy){ while(y) {ll t=y;y=x%y;x=t; }return x;} voidgetc() {c[1][0]=c[1][1]=1; for(intI=2; i<=n;i++) {c[i][0]=1; for(intj=1; j<=2; j + +) {c[i][j]=c[i-1][j]+c[i-1][j-1]; }}}void Pushup (intRT) {sumv[rt]=sumv[rt<<1]+sumv[rt<<1|1]; sumvi[rt]=sumvi[rt<<1]+sumvi[rt<<1|1]; sumvii[rt]=sumvii[rt<<1]+sumvii[rt<<1|1];} void Pushdown (intRt,ll L,ll R) {if(col[rt]!=0) {ll mid= (l+r) >>1; ll tmpmid=mid+1; sumv[rt<<1]+= (mid-l+1)*col[RT]; sumv[rt<<1|1]+= (r-tmpmid+1)*col[RT]; sumvi[rt<<1]+= ((L+mid)*(mid-l+1))/2ll*col[RT]; sumvi[rt<<1|1]+= ((tmpmid+r)*(R-tmpmid+1))/2ll*col[RT]; sumvii[rt<<1]+= (((Mid*(Mid+1)*(2*mid+1))/6LL)-((((((() ((()1)*l*(2*l-1))/6ll)))*col[RT]; sumvii[rt<<1|1]+= (((R*(r+1)*(2*r+1))/6LL)-(((((tmpmid-1)*tmpmid*(2*tmpmid-1))/6ll)))*col[RT]; col[rt<<1]+=COL[RT]; col[rt<<1|1]+=COL[RT]; col[rt]=0; }}void Update (intLintR,ll D,ll L,ll R,intRT) {if(L<=L&&R<=R) {Sumv[rt]+=d*(r-l+1); Sumvi[rt]+=d*(r-l+1)*(L+R)/2ll Sumvii[rt]+=d*(R*(r+1)*(2*r+1)-(L-1)*(L*(2*l-1))/6ll Col[rt]+=d;return; } pushdown (Rt,l,r);intMid= (l+r) >>1;if(l<=mid) Update (l,r,d,l,mid,rt<<1);if(r>mid) Update (l,r,d,mid+1,r,rt<<1|1); Pushup (RT);} ll query (intLintRintLintRintRT) {LL ret=0;if(L<=L&&R<=R) {returnSUMV[RT]; } pushdown (Rt,l,r);intMid= (l+r) >>1;if(L<=mid) Ret+=query (l,r,l,mid,rt<<1);if(R>mid) Ret+=query (l,r,mid+1,r,rt<<1|1); Pushup (RT);returnRET;} ll Queryi (intLintRintLintRintRT) {LL ret=0;if(L<=L&&R<=R) {returnSUMVI[RT]; } pushdown (Rt,l,r);intMid= (l+r) >>1;if(L<=mid) ret+=queryi (l,r,l,mid,rt<<1);if(R>mid) ret+=queryi (l,r,mid+1,r,rt<<1|1); Pushup (RT);returnRET;} ll Queryii (intLintRintLintRintRT) {LL ret=0;if(L<=L&&R<=R) {returnSUMVII[RT]; } pushdown (Rt,l,r);intMid= (l+r) >>1;if(L<=mid) Ret+=queryii (l,r,l,mid,rt<<1);if(R>mid) Ret+=queryii (l,r,mid+1,r,rt<<1|1); Pushup (RT);returnRET;}intMain () {scanf ("%d%d",&n,&m);getc(); for(intI=1; i<=m; i++) {llx,y; ll Z; scanf'%s',s); Switchs[0]) {case' C ': scanf ("%lld%lld%lld",&x,&y, &z);y--; Updatex,yZ1N1); Break; Case' Q ': scanf ("%lld%lld",&x,&y);y--; ll ans= (y-x-x*y+1)*query(x,y,1N1) +queryi (x,y,1N1)*(y+x)-queryii (x,y,1N1); ll c_cnt=c[y-x+2][2]; ll TMPGCD=GCD (ans,c_cnt); ANS/=TMPGCD; C_CNT/=TMPGCD;if(c_cnt==1)printf("%lld/1\n", ans);Else printf("%lld/%lld\ n", ans,c_cnt); Break; } } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 2752 [HAOI2012] Expressway (road) segment tree