hdu1698(线段树区间更新模板)
来源:互联网 发布:linux监控tomcat进程 编辑:程序博客网 时间:2024/05/21 17:49
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.
题意:有长为n的为铜(1)的区间。现在有Q个操作,每一个操作包含一个区间以及要替换的材质铜(1)银(2)金(3),最后求1~n总价值。
………………
#include <iostream>#include <fstream>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <vector>#include <map>#include <cmath>#include <algorithm>#include <functional>#define inf 0x7fffffffusing namespace std;typedef long long ll;const int MAXN=1e9+10;const int MAX=1e6+10;int n;int s[MAX];struct NODE{ int l,r; ll sum,add;}tree[MAX*4];void push_up(int node){ tree[node].sum=tree[node<<1].sum+tree[node<<1|1].sum;}void push_down(int node,int l_num,int r_num){ //替换,向下更新 if(tree[node].add){ tree[node<<1].add=tree[node].add; tree[node<<1|1].add=tree[node].add; tree[node<<1].sum=tree[node].add*l_num; tree[node<<1|1].sum=tree[node].add*r_num; tree[node].add=0; }}void init(int node,int l,int r){ tree[node].l=l; tree[node].r=r; tree[node].add=0; if(l==r){ tree[node].sum=1; return; } int mid=(l+r)>>1; init(node<<1,l,mid); init(node<<1|1,mid+1,r); push_up(node);}int query(int node,int x,int y){ int l=tree[node].l; int r=tree[node].r; if(l>y||r<x) return -1; if(l>=x&&r<=y) return tree[node].sum; int mid=(l+r)>>1; push_down(node,mid-l+1,r-mid); if(x>mid) return query(node<<1|1,x,y); else if(y<=mid) return query(node<<1,x,y); else return query(node<<1,x,y)+query(node<<1|1,x,y);}void updata_tree(int node,int x,int y,int num){ int l=tree[node].l; int r=tree[node].r; if(x<=l&&r<=y){ tree[node].add=num; tree[node].sum=(r-l+1)*num; return ; } int mid=(l+r)>>1; push_down(node,mid-l+1,r-mid); if(x<=mid) updata_tree(node<<1,x,y,num); if(y>mid) updata_tree(node<<1|1,x,y,num); push_up(node);}int main(){ //ios::sync_with_stdio(false); #ifdef ONLINE_JUDGE #else freopen("in.txt","r",stdin); #endif int T; cin>>T; int flag=1; while(T--){ cin>>n; init(1,1,n); int Q,x,y,num; cin>>Q; while(Q--){ scanf("%d%d%d",&x,&y,&num); updata_tree(1,x,y,num); } printf("Case %d: The total value of the hook is %d.\n", flag++,query(1,1,n)); } return 0;}
- 线段树区间更新&&HDU1698模板
- hdu1698(线段树区间更新模板)
- HDU1698线段树区间更新
- hdu1698 线段树区间更新
- hdu1698 线段树 区间更新
- HDU1698线段树区间更新
- hdu1698(线段树区间更新)
- 线段树区间更新hdu1698
- hdu1698 线段树区间更新
- HDU1698 线段树 区间更新
- hdu1698 Just a Hook 线段树区间更新,模板题
- HDU1698(线段树区间更新求和)
- HDU1698线段树区间更新,一次查询
- hdu1698线段树(区间更新)
- 线段树 区间更新(成段更新) HDU1698
- 线段树区间更新(2)(lazy)(区间都变为v)(序号从1开始)(O(logn))模板(hdu1698)
- 线段树模板(区间修改)——hdu1698
- HDU1698--Just a hook--线段树区间更新
- OutOfMemoryError系列(1): Java heap space
- 2017暑期ACM俱乐部个人训练赛第4场 G题和J题
- activity工作流多实例并发绘图工具类
- COJ1974-神奇药水
- 分组统计之facet
- hdu1698(线段树区间更新模板)
- 1546: 回形取数
- 链接收藏2
- CSS 选择器总结
- Spring boot 定时任务
- solr设置高亮
- CSU 1561(More) Multiplication
- CAVLC和CABAC简介
- 例说数据结构&STL(五)——stack