首页 > 代码库 > HDU 1698 线段树(区间染色)

HDU 1698 线段树(区间染色)

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16255    Accepted Submission(s): 8089


Problem Description
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.



Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
 

 

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
 

 

Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
 

 

Sample Input
1
10
2
1 5 2
5 9 3
 

 

Sample Output
Case 1: The total value of the hook is 24.
 
 
 
题目意思:
给两个数n,m分别为数组大小和操作次数,数组元素初始化为1,下面m次操作,每次操作有三个元素a,b,c   (c最大为3)即把数组区间[a,b]内的元素全部变为c。
求m次操作后数组元素总和为多少。
 
 
思路:
这道题就像染色一样,染过一遍又一遍。。这种题目一般不能更新到叶子节点,会超时,需要用一个标记,标记染得颜色,当更新[a,b]区间颜色时就在线段树中查找,若[l,r](l<=a&&r>=b)的颜色和更新的颜色一样则不用更新,反之若[l,r]是一种和更新德尔颜色不一样的颜色时,则向下传递[l,r]的颜色,而[l,r]标记为杂色(后面求和需要用到)。
 
 
代码:
 
 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 #define N 100005 7  8 struct node{ 9     int l, r;10     int val;              //val为-1表示杂色 11 }a[N*4];12 13 14 void build(int left,int right,int root){15     a[root].l=left;16     a[root].r=right;17     a[root].val=1;18     if(left==right) return;19     int mid=(left+right)/2;20     build(left,mid,root*2);21     build(mid+1,right,root*2+1);22 }23 24 void update(int left,int right,int val,int root){25     if(a[root].val==val) return;                //若区间颜色和需要更新的颜色一样则不用更新 26     if(a[root].l==left&&a[root].r==right){27         a[root].val=val;28         return;29     }30     if(a[root].val!=-1){31         a[root*2].val=a[root*2+1].val=a[root].val;32         a[root].val=-1;33     }34     int mid=(a[root].l+a[root].r)/2;35     if(left>mid) update(left,right,val,root*2+1);36     else if(right<=mid) update(left,right,val,root*2);37     else{38         update(left,mid,val,root*2);39         update(mid+1,right,val,root*2+1);40     }41 }42 43 int get_sum(int root){44     if(a[root].val!=-1) return (a[root].r-a[root].l+1)*a[root].val;45     else return get_sum(root*2)+get_sum(root*2+1);46 }47 48 main()49 {50     int t, n, m;51     int i, j;52     cin>>t;53     int x, y, z;54     int kase=1;55     while(t--){56         scanf("%d %d",&n,&m);57         build(1,n,1);58         while(m--){59             scanf("%d %d %d",&x,&y,&z);60             update(x,y,z,1);61         }62         printf("Case %d: The total value of the hook is %d.\n",kase++,get_sum(1));63     }64 }