HDU-1698 Just a Hook(线段树模板)
2016-07-24 18:33
435 查看
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27332 Accepted Submission(s): 13572
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.
Source
2008 “Sunline Cup” National Invitational Contest
Recommend
wangye | We have carefully selected several similar problems for you: 1166 1754 1542 1394 2795
重点在于优化时间(虽然不知道为什么数组开小会超时)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<limits.h> #include<algorithm> using namespace std; const int maxn=100000+10; int n,m; struct node { int l,r,flag; }str[maxn<<2]; void build(int id,int l,int r) { str[id].l=l; str[id].r=r; str[id].flag=1; if(l!=r) { int m=(l+r)/2; build(id*2,l,m); build(id*2+1,m+1,r); } } void change(int i,int x,int y,int m) { if(str[i].flag == m)//相同则不用修改了 return ; if(str[i].l == x && str[i].r == y)//找到了区间,直接更新 { str[i].flag = m; return ; } if(str[i].flag != -1) { str[2*i].flag = str[2*i+1].flag = str[i].flag; str[i].flag = -1; } int mid = (str[i].l+str[i].r)>>1; if(x>mid) change(2*i+1,x,y,m); else if(y<=mid) change(2*i,x,y,m); else { change(2*i,x,mid,m); change(2*i+1,mid+1,y,m); } } int find(int i) { if(str[i].flag!=-1) return (str[i].r-str[i].l+1)*str[i].flag; else return find(i*2)+find(i*2+1); } int main() { int t,i,j,cases=0; scanf("%d",&t); while(t--) { ++cases; int b,c,d; scanf("%d%d",&n,&m); build(1,1,n); for(i=1;i<=m;i++) { scanf("%d%d%d",&b,&c,&d); change(1,b,c,d); } printf("Case %d: The total value of the hook is %d.\n",cases,find(1)); } }
相关文章推荐
- 第29天:控制进度,控制进度知识点,敏捷中控制进度知识点
- 转载:在百度文库上找到的一个管理员权限的问答
- [安卓开发] 完美解决Dialog不能全屏
- Java网络编程学习笔记(二)流
- 第一章.对象导论之方法的重写和重载
- 自动发送邮件程序开发
- Iframe实现Ajax文件上传Servlet响应上传结果
- POJ 2406 Power Strings (kmp求循环节)
- (菜鸟来动手-第四天)SSM整合之BaseService,BaseDao
- eclipse启动不了
- Android 开发知识小集
- 用单链表实现大数相加,相乘
- 关于anaconda3下安装opencv3
- 码神-day10-java
- 欢迎使用CSDN-markdown编辑器
- json数组转普通数组 普通数组转json数组
- 神奇的并查集
- 【泛型】跟着 Oracle java doc 学习 <1> 泛型
- 表达式求值
- std::move