hdu1698(线段树区间更新模板)
2017-07-29 19:32
260 查看
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
110
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 0x7fffffff using 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; }
相关文章推荐
- 线段树区间更新(2)(lazy)(区间都变为v)(序号从1开始)(O(logn))模板(hdu1698)
- 线段树 区间更新(hdu1698) 区间合并(poj3667)
- 线段树区间更新模板
- 区间更新 区间和查询 带有延迟标记 线段树 hdu1698; 附:csa 区间加值,维护最大值
- POJ 3468 A Simple Problem with Integers (线段树区间更新模板)
- poj 3468 线段树区间更新,模板题
- hdu1698 Just a Hook(线段树 区间更新+懒惰标记)
- 线段树 + 区间更新(区间增加v)模板 ---- poj 3468 - Snarl_jsb
- Color the ball (线段树,区间更新模板题)
- 线段树区间最大值查询,单点更新,建立模板
- poj 3468 A Simple Problem with Integers 线段树模板区间更新
- hdu1698 Just a Hook 线段树区间更新,模板题
- 洛谷 2068 线段树模板:单点更新,区间求和
- 线段树专题#4_蒟蒻训练历程记录_HDU1698_ 延迟标记、区间更新
- hdu1698 线段树区间更新
- 线段树区间更新求和模板
- hihocoder 1078 线段树的区间修改 (线段树 区间更新 模板)
- POJ - 3468 B - A Simple Problem with Integers 线段树区间更新模板
- hdu1698 线段树区间更新
- Just a Hook +线段树区间更新模板题