标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20532 Accepted Submission(s): 10284
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 11 #define N 100005 12 #define ll root<<1 13 #define rr root<<1|1 14 #define mid (a[root].l+a[root].r)/2 15 16 17 int max(int x,int y){return x>y?x:y;} 18 int min(int x,int y){return x<y?x:y;} 19 int abs(int x,int y){return x<0?-x:x;} 20 21 int n; 22 23 struct node{ 24 int l, r, val; 25 bool f; 26 }a[N*4]; 27 28 void build(int l,int r,int root){ 29 a[root].l=l; 30 a[root].r=r; 31 a[root].val=1; 32 if(l==r){ 33 return; 34 } 35 build(l,mid,ll); 36 build(mid+1,r,rr); 37 } 38 39 void update(int l,int r,int val,int root){ 40 if(a[root].val==val) return; 41 if(a[root].l==l&&a[root].r==r){ 42 a[root].val=val; 43 return; 44 } 45 if(a[root].val){ 46 if(a[root].l!=a[root].r) { 47 a[ll].val=a[rr].val=a[root].val; 48 } 49 } 50 if(l>=a[rr].l) update(l,r,val,rr); 51 else if(r<=a[ll].r) update(l,r,val,ll); 52 else { 53 update(l,mid,val,ll); 54 update(mid+1,r,val,rr); 55 } 56 if(a[ll].val==a[rr].val) a[root].val=a[ll].val; 57 else a[root].val=0; 58 } 59 60 int get_sum(int root){ 61 if(a[root].val) return a[root].val*(a[root].r-a[root].l+1); 62 return get_sum(ll)+get_sum(rr); 63 } 64 65 main() 66 { 67 int q, i, j, k, x, y, z; 68 int t; 69 cin>>t; 70 for(k=1;k<=t;k++){ 71 scanf("%d %d",&n,&q); 72 build(1,n,1); 73 while(q--){ 74 scanf("%d %d %d",&x,&y,&z); 75 update(x,y,z,1); 76 } 77 printf("Case %d: The total value of the hook is %d.\n",k,get_sum(1)); 78 79 } 80 }
标签:
原文地址:http://www.cnblogs.com/qq1012662902/p/4531693.html