hdu 1698 Just a Hook(线段树)
2015-07-18 20:15
363 查看
ust a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 21293 Accepted Submission(s): 10648
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1 10 2 1 5 2 5 9 3
[align=left]Sample Output[/align]
Case 1: The total value of the hook is 24.
[align=left]Source[/align]
2008 “Sunline Cup” National Invitational Contest
又是模板题。
#include<stdio.h> #include<string.h> #define N 100005 struct node{ int l,r,sum,add; }tree[N<<2]; void pushup(int root) { if(tree[root].l==tree[root].r)return; tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum; return; } void pushdown(int root) { if(tree[root].l==tree[root].r)return; if(tree[root].add==-1)return; tree[root<<1].add=tree[root<<1|1].add=tree[root].add; tree[root<<1].sum=(tree[root<<1].r-tree[root<<1].l+1)*tree[root].add; tree[root<<1|1].sum=(tree[root<<1|1].r-tree[root<<1|1].l+1)*tree[root].add; tree[root].add=-1; return; } void build(int l,int r,int root) { tree[root].l=l; tree[root].r=r; tree[root].sum=0; tree[root].add=-1; if(l==r){ tree[root].sum=1; return; } int mid=l+r>>1; build(l,mid,root<<1); build(mid+1,r,root<<1|1); pushup(root); return; } void update(int l,int r,int z,int root) { if(l==tree[root].l&&r==tree[root].r){ tree[root].sum=(tree[root].r-tree[root].l+1)*z; tree[root].add=z; return ; } pushdown(root); int mid=tree[root].l+tree[root].r>>1; if(r<=mid){ update(l,r,z,root<<1); } else if(l>mid){ update(l,r,z,root<<1|1); } else { update(l,mid,z,root<<1); update(mid+1,r,z,root<<1|1); } pushup(root); return ; } int query(int l,int r,int root) { if(l==tree[root].l&&r==tree[root].r){ return tree[root].sum; } pushdown(root); int mid=tree[root].l+tree[root].r>>1; if(r<=mid)query(l,r,root<<1); else if(l>mid)query(l,r,root<<1|1); else return query(l,mid,root<<1)+query(r,mid+1,root<<1|1); } int main() { int n,Q,x,y,z,i,j,k,T; scanf("%d",&T); for(i=1;i<=T;i++) { scanf("%d",&n); build(1,n,1); scanf("%d",&Q); while(Q--) { scanf("%d%d%d",&x,&y,&z); update(x,y,z,1); } printf("Case %d: The total value of the hook is %d.\n",i,query(1,n,1)); } return 0; }
相关文章推荐
- 使用Matlab从Excel中读取数据并实现回归统计计算
- href="#"和javasrcript:void(0)的区别
- 轻装上阵,安卓工程师之路---day01(HTML&CSS)
- Windows、Unix、Linux是什么类型的操作系统?
- UVA - 232 Crossword Answers
- cURL模拟POST提交数据
- Leetcode-Min Stack(C++)
- poj 1947 经典树形dp
- JavaScript中逗号运算符和this
- 《程序设计实践》笔记
- Objective-C 2.0 基础要点归纳
- 陶斯咏名字的由来
- 南阳oj 动态规划 又见拦截导弹 题目841
- MySQL数据库基础知识点总结
- 数据结构复习之查找算法的总结回顾
- Java并发编程:Callable、Future和FutureTask
- JavaScript---DOM文档
- MATLAB与R函数对照(1)——说明与目录
- (7)unity入门,音乐
- JS中怎么将json对象转化成字符串