http://acm.hdu.edu.cn/showproblem.php?pid=1698 #include<iostream> using namespace std; #define N 100001 struct node { int l,r; int type; int getmid(){ return (l+r)>>1; } int getdis(){ return r-l+1; } }p[3*N]; void build(int l,int r,int id) { p[id].l=l;p[id].r=r; p[id].type=1; if(l==r) return; int mid=p[id].getmid(); build(l,mid,id<<1); build(mid+1,r,id<<1|1); } void update(int l,int r,int id,int type) { if(p[id].l==l&&p[id].r==r) { p[id].type=type; return; } if(p[id].type==type) return; if(p[id].type>0) { p[id<<1].type=p[id<<1|1].type=p[id].type; p[id].type=0; } int mid=p[id].getmid(); if(r<=mid) update(l,r,id<<1,type); else if(l>mid) update(l,r,id<<1|1,type); else { update(l,mid,id<<1,type); update(mid+1,r,id<<1|1,type); } } int query(int l,int r,int id) { if(p[id].type>0) return p[id].type*p[id].getdis(); int mid=p[id].getmid(); if(r<=mid) return query(l,r,id<<1); else if(l>mid) return query(l,r,id<<1|1); else return query(l,mid,id<<1)+query(mid+1,r,id<<1|1); } int main(void) { int t,k=1; int n,m; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); build(1,n,1); while(m--) { int x,y,type; scanf("%d%d%d",&x,&y,&type); update(x,y,1,type); } printf("Case %d: The total value of the hook is %d./n",k++,query(1,n,1)); } } 与POJ2777几乎一样