hdu1698线段树的区间更新区间查询

Just a Hook

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

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.

线段树的区间更新区间查询:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdio.h>
 4 #include <string.h>
 5 #include <math.h>
 6 using namespace std;
 7 #define ll long long int
 8 ll a[800000];
 9 ll d[800000];
10 void build(int b,int c,int x)
11 {
12     if(b==c)
13     {
14         a[x]=1;
15         return ;
16     }
17     int m=(b+c)>>1;
18     build(b,m,x<<1);
19     build(m+1,c,x<<1|1);
20     a[x]=a[x<<1]+a[x<<1|1];
21 }
22 void fun(int x,int m)
23 {
24     if(d[x])
25     {
26         d[x<<1]=d[x<<1|1]=d[x];
27         a[x<<1]=(m-(m>>1))*d[x];
28         a[x<<1|1]=(m>>1)*d[x];
29         d[x]=0;
30     }
31 }
32 void update(int x,int y,int b,int c,int t,int z)
33 {
34     if(x<=b&&y>=c)
35     {
36         d[t]=z;
37         a[t]=(c-b+1)*z;
38         return;
39     }
40     fun(t,c-b+1);
41     int m=(b+c)>>1;
42     if(x<=m) update(x,y,b,m,t<<1,z);
43     if(y>m) update(x,y,m+1,c,t<<1|1,z);
44     a[t]=a[t<<1]+a[t<<1|1];
45 }
46 int main()
47 {
48     //freopen("int.txt","r",stdin);
49     int m;
50     int i,j,x,y,z;
51     cin>>m;
52     for(i=1;i<=m;i++)
53     {
54         int n,k;
55         memset(a,0,sizeof(a));
56         memset(d,0,sizeof(d));
57         cin>>n>>k;
58         build(1,n,1);
59         for(j=0;j<k;j++)
60         {
61             scanf("%d%d%d",&x,&y,&z);
62             update(x,y,1,n,1,z);
63         }
64         cout<<"Case "<<i<<": The total value of the hook is "<<a[1]<<"."<<endl;
65     }
66 }
View Code
原文地址:https://www.cnblogs.com/ERKE/p/3256963.html