HDU 1698 Just a Hook(线段树延迟更新)
2016-09-27 12:48
267 查看
题意:一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2,铜为1),最初可当做全为铜;最后求这条线段的总价值。
思路:区间更新。
区间更新思路: 更新时,把无代表性的父节点标记成0,
把要更新的父节点的旧状态甩给下一层,
然后再向下更新就行。
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.
![](http://7xjob4.com1.z0.glb.clouddn.com/779a6c86f4db19106cba2c46a7dafe46)
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
Sample Output
思路:区间更新。
区间更新思路: 更新时,把无代表性的父节点标记成0,
把要更新的父节点的旧状态甩给下一层,
然后再向下更新就行。
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int MAXN=100000; int tree[3*MAXN+10],v,st,en; int ans; void update(int root,int n_left,int n_right){ int mid=(n_left+n_right)/2; if(n_left>=st&&n_right<=en){ tree[root]=v; }else{ if(tree[root]){//把当前层的状态甩给下一层 tree[root*2]=tree[root*2+1]=tree[root]; tree[root]=0; } if(mid<st){ update(root*2+1,mid+1,n_right); }else if(mid>=en){ update(root*2,n_left,mid); }else{ update(root*2,n_left,mid); update(root*2+1,mid+1,n_right); } } return ; } void query(int root,int n_left,int n_right){ int mid=(n_left+n_right)/2; if(tree[root]!=0){ ans+=(n_right-n_left+1)*tree[root]; return ; } query(root*2,n_left,mid); query(root*2+1,mid+1,n_right); return ; } int main() { int T; cin>>T; int cut=0; while(T--){ cut++; memset(tree,0,sizeof(tree)); int m,n; cin>>n>>m; st=1;en=n;v=1; update(1,1,n); while(m--){ scanf("%d%d%d",&st,&en,&v); update(1,1,n); } ans=0; query(1,1,n); cout<<"Case "<<cut<<": The total value of the hook is "<<ans<<'.'<<endl; } }
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.
相关文章推荐
- HDU 1698 Just a Hook(线段树 + Lazy Tag(延迟更新))
- hdu - 1698 - Just a Hook(线段树(区间更新))
- HDU-1698-Just a Hook(线段树 区间更新)
- HDU 1698 Just a Hook // 线段树 区间更新
- hdu 1698(区间更新线段树)Just a Hook
- HDU 1698 Just a Hook (线段树延迟标记(lazy))
- 杭电 HDU ACM 1698 Just a Hook(线段树 区间更新 延迟标记)
- HDU 1698 Just a Hook (线段树 区间更新基础)
- HDU 1698 (线段树 区间更新) Just a Hook
- hdu1698 Just a Hook (线段树区间更新 懒惰标记)
- HDU 1698 Just a Hook 线段树区间更新
- hdu-------(1698)Just a Hook(线段树区间更新)
- hdu 1698 Just a Hook 线段树区间更新
- hdu 1698 Just a Hook (线段树区间更新裸题)
- hdu 1698:Just a Hook(线段树,区间更新)
- HDU 1698 Just a Hook (线段树区间更新)
- HDU 1698 Just a Hook (线段树区间更新)
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
- HDU 1698 Just a Hook(线段树区间更新)
- hdu-1698-Just a Hook-线段树-区域更新,区域查询