Just a Hook 【线段树】-区间更改 求和
来源:互联网 发布:115年费会员淘宝 编辑:程序博客网 时间:2024/06/05 22:44
Just a Hook
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.
思路 : 模版题 ,区间更改和区间求和
代码
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<map>#include<vector>#include<set>#define CLR(a,b) memset((a),(b),sizeof(a))#define inf 0x3f3f3f3f#define mod 100009#define LL long long#define M 5000000#define ll o<<1#define rr o<<1|1#define lson o<<1,l,mid#define rson o<<1|1,mid+1,rusing namespace std;struct Tree{ int l,r,len,sum,lazy;};Tree tree[M<<2];void up(int o){ tree[o].sum=tree[ll].sum+tree[rr].sum;}void down(int o){ if(tree[o].lazy!=-1) // 因为 这个区间修改的时候可能是 0 所以不能用 0 来做标记 { tree[rr].lazy=tree[o].lazy; // 注意都是等号 tree[ll].lazy=tree[o].lazy; tree[rr].sum=tree[o].lazy*tree[rr].len; tree[ll].sum=tree[o].lazy*tree[ll].len; tree[o].lazy=-1; }}void build(int o,int l,int r){ tree[o].l=l;tree[o].r=r; tree[o].len=r-l+1; tree[o].lazy=-1; if(l==r) { tree[o].sum=1;// 初始的价值都是1 return ; } int mid=(l+r)>>1; build(lson); build(rson); up(o);}void update(int o,int l,int r,int val){ if(l<=tree[o].l&&tree[o].r<=r) { tree[o].lazy=val; tree[o].sum=tree[o].len*val; return ; } down(o); int mid=(tree[o].l+tree[o].r)>>1; if(r<=mid) update(ll,l,r,val); else if(l>mid) update(rr,l,r,val); else { update(ll,l,mid,val); update(rr,mid+1,r,val); } up(o);}int query(int o,int l,int r){ if(tree[o].l>=l&&tree[o].r<=r) return tree[o].sum; down(o); int mid=(tree[o].l+tree[o].r)>>1; if(r<=mid) query(ll,l,r); else if(l>mid) query(rr,l,r); else return query(ll,l,mid)+query(rr,mid+1,r); }int main(){ int t ;int k=1; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); build(1,1,n); while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); update(1,a,b,c); } printf("Case %d: The total value of the hook is %d.\n",k++,query(1,1,n)); } return 0;}
- Just a Hook 【线段树】-区间更改 求和
- Just a Hook ,线段树,区间更新 ,区间求和
- HDU 1689 Just a Hook 线段树区间更新求和
- hdoj 1689 Just a Hook 【线段树区间更新求和】
- hdu 1698 Just a Hook(线段树区间更新求和)
- HDU 1698 Just a Hook 线段树区间更新求和
- 20140719 「线段树 - 区间替换,区间求和」 HDOJ 1698 Just a Hook
- poj 1698 Just a Hook -- (线段树,区间值修改,区间求和)
- HDU 1698 Just a Hook 数据结构+线段树+区间修改(设值)+区间求和
- 线段树—Just a Hook(区间更新,区间求和)
- HDU 1698 Just a Hook(线段树区间更新+区间求和+染色问题)
- Just a Hook(线段树区间更新)
- 线段树区间更新 Just a Hook
- Just a Hook ---线段树区间更新
- hdu 1698 Just a Hook(线段树 成段更新+总区间求和)
- hdu1698 Just a Hook (线段树功能:成段替换,总区间求和)
- hdu 1698 Just a hook 线段树 成段更新~~区间求和
- hdu 1698 Just a Hook(线段树的区间更新及求和)
- leetcode 535. Encode and Decode TinyURL(长短网址互译)
- jvm 老年代新生代
- 406. Queue Reconstruction by Height
- DNN-HMM 中 DNN 与 HMM 的关系
- MFC-两个基本控件:DateTimeControl以及CalendarControl
- Just a Hook 【线段树】-区间更改 求和
- 31. Next Permutation Medium
- 面象对象的三大特点
- Zookeeper 的安装
- 第五章 JAVA数组初级学习
- 模拟实现queue
- static执行顺序
- 51nod 1103 N的倍数 (抽屉原理)
- HDU 4454 Stealing a Cake (三分)