hdu 1698 just a hook 线段数

Just a Hook

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 4
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.

hdu 1698 just a hook 线段数


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.
 
这道题目和之前的一样,修改线段数的点。
代码:
View Code
 1 # include<stdio.h>

 2 # define N 400000

 3 struct node{

 4     int left,right,mid;

 5     int num;

 6 }a[N];

 7 int val;

 8 void make(int s,int t,int step)

 9 {

10     a[step].num=1;

11     a[step].left=s;

12     a[step].right=t;

13     a[step].mid=(s+t)/2;

14     if(s==t) return;

15     make(s,a[step].mid,2*step);

16     make(a[step].mid+1,t,2*step+1);

17  

18 }

19 void change(int ans1,int ans2,int step)

20 {

21     if(a[step].num==val) return; 

22     if(a[step].left==ans1 && a[step].right==ans2 )  

23     {a[step].num=val;return;}

24     if(a[step].num!=-1) {a[step*2].num=a[step*2+1].num=a[step].num;}

25     a[step].num=-1;

26     if(ans1>a[step].mid) change(ans1,ans2,2*step+1);

27     else if(ans2<=a[step].mid) change(ans1,ans2,2*step);

28     else

29     {

30         change(ans1,a[step].mid,2*step);

31         change(a[step].mid+1,ans2,2*step+1);

32     }

33 }

34 int count(int step)

35 {

36     if(a[step].num>0) return (a[step].right-a[step].left+1)*a[step].num;

37     return count(step*2)+count(2*step+1);

38 }

39 int main()

40 {

41     int ncase=0,n,m,t,ans1,ans2;

42     scanf("%d",&t);

43     while(t--)

44     {

45         scanf("%d%d",&n,&m);

46         make(1,n,1);

47         while(m--)

48         {

49             scanf("%d%d%d",&ans1,&ans2,&val);

50             change(ans1,ans2,1);

51         }

52         printf("Case %d: The total value of the hook is %d.\n",++ncase,count(1));

53     }

54     return 0;

55 }

 

你可能感兴趣的:(HDU)