Just a Hook (区间更新,lazy标记)
来源:互联网 发布:linux 拷贝后重命名 编辑:程序博客网 时间:2024/06/05 02:19
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.
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.
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.
11021 5 25 9 3
Case 1: The total value of the hook is 24.
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;typedef long long ll;const int maxn=1e5+10;struct node{ int l,r; int sum_val; int tag,lazy;}tree[maxn*4];void build(int st,int ed,int now_id){ tree[now_id].l=st; tree[now_id].r=ed; tree[now_id].tag=0,tree[now_id].lazy=0;//标记是否未释放,应该释放多少 if(st==ed)//叶子上才能填充值 { tree[now_id].sum_val=1; return; } int mid=(st+ed)>>1; build(st,mid,now_id<<1); build(mid+1,ed,now_id<<1|1); tree[now_id].sum_val=tree[now_id<<1].sum_val+tree[now_id<<1|1].sum_val;//对数值进行加和操作}void update(int st,int ed,int val,int now_id){ if(tree[now_id].l==st&&tree[now_id].r==ed)//说明正好到了我们要更新的区间,我们就可以停在这里,lazy一下 { tree[now_id].sum_val=(ed-st+1)*val; tree[now_id].lazy=1, tree[now_id].tag=val; return; } int mid=(tree[now_id].l+tree[now_id].r)>>1;//这是当前数的mid,我们那个st和ed是不变的 if(tree[now_id].lazy==1)//如果这个点有懒惰标记,那我们就去先释放,要不然,这个值不下去,下面的值 { //上次应该更新的可是没有更新,所以我们先释放一下 tree[now_id].lazy=0; update(tree[now_id].l,mid,tree[now_id].tag,now_id<<1);//本来就懒,所以就只是走了一步,不会说直接到了叶子节点, update(mid+1,tree[now_id].r,tree[now_id].tag,now_id<<1|1);//这里是更新树,就是之前的数据,不是我们这次的数据 tree[now_id].tag=0;//当前树的节点值已经释放,所以重新写为0 } if(ed<=mid) update(st,ed,val,now_id<<1); else if(st>mid) update(st,ed,val,now_id<<1|1); else { update(st,mid,val,now_id<<1); update(mid+1,ed,val,now_id<<1|1); } tree[now_id].sum_val=tree[now_id<<1].sum_val+tree[now_id<<1|1].sum_val;}int main(){ int T; scanf("%d",&T); int cnt=1; while(T--) { int n; scanf("%d",&n); build(1,n,1); int time; scanf("%d",&time); while(time--) { int a,b,x; scanf("%d %d %d",&a,&b,&x); update(a,b,x,1); } printf("Case %d: The total value of the hook is %d.\n",cnt++,tree[1].sum_val); } return 0;}
阅读全文
0 0
- Just a Hook (区间更新,lazy标记)
- HDU 1698-Just a Hook(线段树_区间更新+lazy标记)
- hdu1698 Just a Hook (线段树区间更新 懒惰标记)
- hdu1698 Just a Hook 线段树区间更新,延时标记
- HDU1698 线段树+区间更新+懒惰标记-Just a Hook
- [线段树]hdu1698 Just a Hook(区间更新、延迟标记
- hdu1698 Just a Hook(线段树 区间更新+懒惰标记)
- hdu1698 Just a Hook(成段替换 lazy标记)
- Just a Hook (线段树+lazy标记)
- Just a Hook (区间更新)
- Just a Hook(区间更新)
- 杭电 HDU ACM 1698 Just a Hook(线段树 区间更新 延迟标记)
- HDU 1698 Just a Hook(线段树的区间更新《标记》)
- hdu1698 Just a hook (线段树 延迟标记 区间更新)
- hdu1698-Just a Hook (线段树区间更新)(延迟标记)
- hdu1698 Just a Hook(线段树成段更新lazy)
- HDU1698 Just a Hook 【线段树】+【成段更新】+【lazy标记】
- HDU1698 Just a Hook(线段树+成段更新+lazy标记)
- Weblogic Server Console启动免输入用户名、口令地址
- UML学习(一)-----用例图
- Unity 打包IOS平台错误
- 用命令行停止Weblogic Server
- (hdu6092) 2017杭电多校联赛第5场-Rikka with Subset 动态规划
- Just a Hook (区间更新,lazy标记)
- 使用weblogic Script工具执行管理
- PHP MySQL Update函数的使用
- 不定时搞事情篇 No.2 神途BOSS时间记录器(续篇)
- Configuring Servers and Machines
- Android:使用ViewFlipper实现上下滚动消息
- Starting Servers Using Node Manager
- 如何构建日请求数十亿次高性能高可用广告系统:微博广告架构解密
- java实现简单的二叉排序树