[HDU 1698]Just a Hook(线段树)
来源:互联网 发布:手机模拟器连发软件 编辑:程序博客网 时间:2024/06/05 12:01
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.
Solution
编号为1到N的金属棒初始为铜,每次可更改某区间金属棒的材质
价值:铜 1,银 2,金 3
对于每组数据求所有更改做完后的总价值
线段树来记录颜色时,0表示该区间有多种颜色
int t,n,q;struct Node{ int l,r,col;}sti[MAXN*4+10];void build(int idx,int a,int b){ sti[idx].l=a; sti[idx].r=b; sti[idx].col=1; if(a==b)return; build(idx*2,a,mid(a,b)); build(idx*2+1,mid(a,b)+1,b);}void update(int idx,int a,int b,int k){ if(a<=sti[idx].l&&b>=sti[idx].r) { sti[idx].col=k; //如果需更新的区间完全包含了该节点,把该节点的颜色赋为k return; //但此处没有更新该节点的子节点 lazy思想 } if(sti[idx].col) { sti[idx*2].col=sti[idx*2+1].col=sti[idx].col;//此时更新 sti[idx].col=0; } if(a>mid(sti[idx].l,sti[idx].r))update(idx*2+1,a,b,k); else if(b<=mid(sti[idx].l,sti[idx].r))update(idx*2,a,b,k); else { update(idx*2+1,a,b,k); update(idx*2,a,b,k); }}int query(int idx){ if(sti[idx].col)return sti[idx].col*(sti[idx].r-sti[idx].l+1); return query(idx*2)+query(idx*2+1);}int main(){ int Case=1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&q); build(1,1,n); int x,y,z; for(int i=1;i<=q;i++) { scanf("%d%d%d",&x,&y,&z); update(1,x,y,z); } printf("Case %d: The total value of the hook is %d.\n",Case++,query(1)); } return 0;}
- 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 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 1698 Just a Hook 线段树
- hdu 1698 Just a Hook 线段树
- Just a Hook - HDU 1698 线段树
- 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(线段树)
- 令人振奋的Class(上)
- 保存服务器的运行状态
- linux debian下安装中国农历
- 最短路径(Floyd、Dijstra、BellmanFord)
- 知乎标签详情页爬虫
- [HDU 1698]Just a Hook(线段树)
- server研发 交流思考
- Android上传图片文件工具类
- 令人振奋的Class(下):继承和实战代码示例
- 亚像素级点定位及边缘定位算法
- P1220 关路灯
- 《posix多线程编程》笔记(一)
- 无线路由连接(不用连接WAN照样可以上网)
- python模拟登陆实战(1)