【hdu】【线段树入门】Just a Hook
来源:互联网 发布:svm算法代码 编辑:程序博客网 时间:2024/06/18 13:49
Problem 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.
Code
线段树区间修改。和对[l,r]同加同减的区别只在于把+=换成=就可以。
#include <cstdio>#include <algorithm>#include <cstring>#define maxn 200000+1000#define lson L,mid,rt<<1#define rson mid+1,R,rt<<1|1#define root L,R,rtusing namespace std;struct node{ int val; }T[3*maxn];int n,m,a,b;char c;void pushUp(int rt){ T[rt].val=max(T[rt<<1].val,T[rt<<1|1].val); return;}void build(int L,int R,int rt){ if(L==R){ scanf("%d",&T[rt].val); return; } else{ int mid=(L+R)/2; build(lson); build(rson); } pushUp(rt); return;}void update(int pos,int num,int L,int R,int rt){ if(L==R){ T[rt].val=num; return; } else{ int mid=(L+R)/2; if(pos<=mid) update(pos,num,lson); else update(pos,num,rson); } pushUp(rt); return;}int query(int l,int r,int L,int R,int rt){ if(l<=L&&r>=R) return T[rt].val;//如果当前结点区间被所求区间完全覆盖 else { int mid=(L+R)/2; int temp=0; if(l<=mid) temp=max(temp,query(l,r,lson)); if(r>mid) temp=max(temp,query(l,r,rson));//这个地方超级坑!!!!!!!!!!!!!!!!!!! return temp; }}int main(){ while(~scanf("%d %d",&n,&m)) { build(1,n,1); for(int i=0;i<m;i++) { scanf("%*c%c%d %d",&c,&a,&b); if(c=='Q') printf("%d\n",query(a,b,1,n,1)); else update(a,b,1,n,1); } } return 0;}
- 【hdu】【线段树入门】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 Just a Hook (线段树)
- HDU 1698 Just a Hook(线段树)
- HDU 1698 just a hook 线段树
- HDU 1698 Just a Hook(线段树)
- hdu just a hook 线段树
- hdu 1698 - Just a Hook(线段树)
- HDU 1698 Just a Hook (线段树)
- hdu 1698 Just a Hook 线段树
- hdu 1698 Just a Hook 线段树
- Just a Hook - HDU 1698 线段树
- hdu 1698 Just a Hook(线段树)
- hdu 1698 Just a Hook(线段树)
- Android关于ListView中的getItemViewType与getViewTypeCount
- RecyclerView 下拉刷新,自动加载更多
- ubuntu开机启动
- 【C#工具类】SFTP--自定义SFTPFactory(上传下载)
- uploadify 初始化 error http 404
- 【hdu】【线段树入门】Just a Hook
- CALayer的阴影添加以及抖动效果构建
- Linux命令总结-df
- AndroidAudioRecorder
- 解决真机版本高于Xcode模拟器版本不能测试问题
- java复制文件到指定目录
- 前端学习之jQuery中.html() .text() .val()的区别
- AOP是什么?
- 菜鸟重新学习基础概念--c语言篇-字符串处理函数