标签:
1 10 5 1 1 10 2 3 10 3 5 10 4 7 10 5 9 10
30
#include<stdio.h> #include<string.h> #include<algorithm> #define M 100007 #define inf 0x3f3f3f3f using namespace std; long long sum; long long r,c,m; struct T { long long left,right,sum; long long add,set; }tree[M<<4]; void build(long long l,long long r,long long i) { tree[i].left=l;tree[i].right=r;tree[i].set=-1; tree[i].add=tree[i].sum=0; if(l==r)return; long long mid=l+(r-l)/2; build(l,mid,i<<1); build(mid+1,r,i<<1|1); } void pushdown(long long i) { if(tree[i].left>=tree[i].right)return; if(tree[i].set!=-1) { tree[i<<1].set=tree[i<<1|1].set=tree[i].set; tree[i<<1].add=tree[i<<1|1].add=0; tree[i<<1].sum=(tree[i<<1].right-tree[i<<1].left+1)*tree[i].set; tree[i<<1|1].sum=(tree[i<<1|1].right-tree[i<<1|1].left+1)*tree[i].set; } if(tree[i].add>0) { long long add=tree[i].add; tree[i<<1].add+=add;tree[i<<1|1].add+=add; tree[i<<1].sum+=add*(tree[i<<1].right-tree[i<<1].left+1); tree[i<<1|1].sum+=add*(tree[i<<1|1].right-tree[i<<1|1].left+1); } } void maintain(long long i) { if(tree[i].left>=tree[i].right)return; tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum; } void update_add(long long l,long long r,long long val,long long i) { if(tree[i].left==l&&tree[i].right==r) { tree[i].add+=val; tree[i].sum+=(tree[i].right-tree[i].left+1)*val; return; } pushdown(i); tree[i].set=-1; tree[i].add=0; long long mid=tree[i].left+(tree[i].right-tree[i].left)/2; if(r<=mid)update_add(l,r,val,i<<1); else if(l>mid)update_add(l,r,val,i<<1|1); else { update_add(l,mid,val,i<<1); update_add(mid+1,r,val,i<<1|1); } maintain(i); } void update_set(long long l,long long r,long long val,long long i) { if(tree[i].left==l&&tree[i].right==r) { tree[i].set=val; tree[i].add=0; tree[i].sum=(tree[i].right-tree[i].left+1)*val; return; } pushdown(i); tree[i].set=-1; tree[i].add=0; long long mid=tree[i].left+(tree[i].right-tree[i].left)/2; if(r<=mid)update_set(l,r,val,i<<1); else if(l>mid)update_set(l,r,val,i<<1|1); else { update_set(l,mid,val,i<<1); update_set(mid+1,r,val,i<<1|1); } maintain(i); } void query(long long l,long long r,long long i) { if(tree[i].left==l&&tree[i].right==r) { sum+=tree[i].sum; return; } pushdown(i); tree[i].set=-1;tree[i].add=0; long long mid=tree[i].left+(tree[i].right-tree[i].left)/2; if(r<=mid)query(l,r,i<<1); else if(l>mid)query(l,r,i<<1|1); else { query(l,mid,i<<1); query(mid+1,r,i<<1|1); } maintain(i); } int main() { long long t; scanf("%lld",&t); while(t--) { long long n,m; scanf("%lld%lld",&n,&m); build(1,n,1); long long a,b,c,time=0,summ=0; while(m--) { sum=0; scanf("%lld%lld%lld",&c,&a,&b); update_add(1,n,c-time,1); time=c; query(a,b,1); summ+=sum; update_set(a,b,0,1); } printf("%lld\n",summ); } return 0; }
标签:
原文地址:http://blog.csdn.net/crescent__moon/article/details/45080211