C - Just a Hook(写的第一道线段树区间更新的题目,卡了半天)

来源:互联网 发布:网络女神排行 编辑:程序博客网 时间:2024/05/17 04:34

C - Just a Hook
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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.
 


区间更新就是懒惰标志lazy的问题,但是这是我第一道写的,他跟一般的区间更新不同,他不是什么加减,而是直接换了每个数组元素里面的值!!!会区间更新的都应该明白

void pushdown(int node){if(segtree[node].lazy!=0){segtree[node<<1].lazy += segtree[node].lazy;segtree[node<<1|1].lazy += segtree[node].lazy;segtree[node<<1].sum += segtree[node].lazy * (segtree[node<<1].r - segtree[node<<1].l + 1);segtree[node<<1|1].sum += segtree[node].lazy * (segtree[node<<1|1].r - segtree[node<<1|1].l + 1);segtree[node].lazy = 0;}}

这是一般的区间更新,但是你真正弄懂了区间更新后,这个地方还能用+=吗?对于这道题来说,这里四个+=都要改为=!!还有好几个地方都是这个问题!所以照搬模版最无谓,一定理解思想,然后要自己写自己的代码,理解一个思想比做几道题都有用。


接下来是我AC的代码:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int Maxn = 100000 + 5;int a[Maxn],val;struct seg{int l,r,sum,lazy;}segtree[Maxn*4];void build(int node,int l,int r){    segtree[node].l=l;segtree[node].r=r;segtree[node].lazy=0;    if(l==r)    {    segtree[node].sum=a[l];return;}else{int mid=(l+r)>>1;    build(node<<1,l,mid);    build(node<<1|1,mid+1,r);    segtree[node].sum = segtree[node<<1].sum + segtree[node<<1|1].sum;}}int query(int node,int x,int y){if(segtree[node].l == x && segtree[node].r == y)//包含在这个[x,y]范围内,直接返回 {return segtree[node].sum;}int mid = segtree[node].l + segtree[node].r;int res = 0;mid >>= 1;if(mid >= y)//[x,y]包含在左子树中 res += query(node<<1,x,y);else if(mid < x)//[x,y]包含在右子树中 res += query(node<<1|1,x,y);else//[x,y]在左右子树都有交集 {res += query(node<<1,x,mid);res += query(node<<1|1,mid+1,y);}return res;}void pushdown(int node){if(segtree[node].lazy!=0){segtree[node<<1].lazy = segtree[node].lazy;segtree[node<<1|1].lazy = segtree[node].lazy;segtree[node<<1].sum = segtree[node].lazy * (segtree[node<<1].r - segtree[node<<1].l + 1);segtree[node<<1|1].sum = segtree[node].lazy * (segtree[node<<1|1].r - segtree[node<<1|1].l + 1);;segtree[node].lazy = 0;}}void update(int node,int x,int y){if(segtree[node].l == x && segtree[node].r == y)//如果node的l和r正好就是这个x和y {segtree[node].lazy = val;//懒惰标记 segtree[node].sum = val * (y - x + 1);//原本都是1,sum是10,都改为2,变成20 return;//先不更新子树,segtree[node].sum已经是正确的了 }if(segtree[node].l == segtree[node].r)//如果已经是树叶节点,就不用pushdown传递lazy下去了。否则访问非法内存空间 return;pushdown(node);//找到一个node的懒惰标记不为0传递下去 int mid = segtree[node].l + segtree[node].r;mid >>= 1;if(mid >= y)update(node<<1,x,y);else if(mid < x)update(node<<1|1,x,y);else//如果卡在两个子树中间各有一段,则两个子树都要更新 {update(node<<1,x,mid);update(node<<1|1,mid+1,y);}segtree[node].sum = segtree[node<<1].sum + segtree[node<<1|1].sum;//更新子树后再来更新自己的sum }int main(){int t,n,q,l,r,i,k=1;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=1;i<=n;i++)a[i] = 1;build(1,1,n);scanf("%d",&q);while(q--){scanf("%d%d%d",&l,&r,&val);update(1,l,r);}printf("Case %d: The total value of the hook is %d.\n",k++,query(1,1,n));}return 0;}

还是要说一句,这题写了至少三个小时,都要崩溃了,但是我还是坚持下来没去看答案自己理解了出来。

0 0