hdoj 1689 Just a Hook 【线段树区间更新求和】

来源:互联网 发布:淘宝全屏店招怎么做 编辑:程序博客网 时间:2024/05/16 14:44

Just a Hook

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


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
11021 5 25 9 3
 

Sample Output
Case 1: The total value of the hook is 24.
 

Source
2008 “Sunline Cup” National Invitational Contest
 

Recommend
wangye   |   We have carefully selected several similar problems for you:  1542 1394 2795 1255 1828 

思路:先建立一个求和树,然后进行多次区间更新,记住进行区间延迟,然后将tree[1]输出就行了!

代码:

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int tree[400005];//数组的大小应该为总长度的4倍就不会超内存了! int add[400005];void build(int rt,int l,int r){if(l==r){tree[rt]=1;return;}int mid=(l+r)>>1,pt=rt<<1;build(pt,l,mid);build(pt|1,mid+1,r);tree[rt]=tree[pt]+tree[pt|1];}void pushdown(int rt,int len){if(add[rt]){int pt=rt<<1,lt=len>>1;add[pt]=add[rt];add[pt|1]=add[rt];tree[pt]=add[rt]*(len-lt);//左区间的长度大于等于有区间的长度! tree[pt|1]=add[rt]*lt;add[rt]=0;}}void updata(int rt,int l,int r,int a,int b,int c){if(a<=l&&b>=r){add[rt]=c;//这一点记住是为add赋值为c,而不是c*(r-l+1); tree[rt]=c*(r-l+1);//因为它用的是相应每个数应该加的数,而不是总和对应的值! return;}pushdown(rt,r-l+1);int mid=(l+r)>>1,pt=rt<<1;if(a<=mid){updata(pt,l,mid,a,b,c);}if(b>mid){updata(pt|1,mid+1,r,a,b,c);}tree[rt]=tree[pt]+tree[pt|1];}int main(){int T,n,m;int N;scanf("%d",&T);N=T;while(T--){memset(add,0,sizeof(add));scanf("%d%d",&n,&m);build(1,1,n);for(int i=1;i<=m;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);updata(1,1,n,a,b,c);}printf("Case %d: The total value of the hook is %d.\n",N-T,tree[1]);}return 0;}


0 0
原创粉丝点击