标签:des style blog http color os io for ar
1 5 5 D 5 5 Q 5 6 D 2 3 D 1 2 Q 1 7
Case #1: 2 3
#include <stdio.h> #define max(A,B)(A>B?A:B) long long node[50005],cnt[50005]; int n,m; long long sum(int x) { long long res=0; while(x>=1) { res+=node[x]; x-=x&-x; } return res; } void add(int x,long long val) { while(x<=n) { node[x]+=val; x+=x&-x; } } int getpos(long long val) { int l=1,r=n,mid,res; while(l<=r) { mid=(l+r)>>1; if(sum(mid)>=val) { res=mid; r=mid-1; } else l=mid+1; } return res; } int main() { int T,i,l,r,cases=1; long long a,b,tempa,tempb,ans; char s[5]; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); printf("Case #%d:\n",cases++); for(i=1;i<=n;i++) node[i]=0,cnt[i]=1; for(i=1;i<=n;i++) add(i,1); while(m--) { scanf("%s%I64d%I64d",s,&a,&b); if(s[0]=='D') { l=getpos(a); r=getpos(b); if(l==r) { cnt[l]+=b-a+1; add(l,b-a+1); } else { tempa=sum(l)-a+1; tempb=b-sum(r-1); cnt[l]+=tempa; add(l,tempa); cnt[r]+=tempb; add(r,tempb); for(i=l+1;i<=r-1;i++) { add(i,cnt[i]); cnt[i]+=cnt[i]; } } } else { l=getpos(a); r=getpos(b); if(l==r) printf("%I64d\n",b-a+1); else { ans=max(sum(l)-a+1,b-sum(r-1)); for(i=l+1;i<=r-1;i++) ans=max(ans,cnt[i]); printf("%I64d\n",ans); } } } } }
HDU-4973-A simple simulation problem.(二分+树状数组)
标签:des style blog http color os io for ar
原文地址:http://blog.csdn.net/faithdmc/article/details/38805903