HDU-1698-Just a Hook(线段树 区间维护)
2016-07-16 17:01
309 查看
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27038 Accepted Submission(s): 13456
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.
第三次写这道题了
代码
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27038 Accepted Submission(s): 13456
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.
第三次写这道题了
代码
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<math.h> #include<queue> #include<iomanip> using namespace std; //线段树延迟更新 const int maxn=1e5+10; int Tree[maxn<<2]; int Lazy[maxn<<2]; void Push_Up(int root) { Tree[root]=Tree[root<<1]+Tree[root<<1|1]; } void Push_Down(int root,int len)//传入待更改区间长度 { if(Lazy[root]!=0) { Lazy[root<<1]=Lazy[root]; Lazy[root<<1|1]=Lazy[root];//数据更新到下一层 Tree[root<<1]=Lazy[root]*(len-(len>>1)); Tree[root<<1|1]=Lazy[root]*(len>>1); Lazy[root]=0; } } void Build(int root,int left,int right) { Lazy[root]=0;//可以考虑初始化为0 if(left==right) { Tree[root]=1; return; } int mid=(left+right)>>1; Build(root<<1,left,mid); Build(root<<1|1,mid+1,right); Push_Up(root); } void Update(int root,int left,int right,int find_left,int find_right,int num) { if(left>=find_left&&right<=find_right) { Lazy[root]=num; Tree[root]=num*(right-left+1); return ; } Push_Down(root,right-left+1); int mid=(left+right)>>1; if(find_right<=mid) Update(root<<1,left,mid,find_left,find_right,num); else if(find_left>mid) Update(root<<1|1,mid+1,right,find_left,find_right,num); else { Update(root<<1,left,mid,find_left,mid,num); Update(root<<1|1,mid+1,right,mid+1,find_right,num); } Push_Up(root); } int main() { int T; scanf("%d",&T); for(int casen=1; casen<=T; casen++) { int N; scanf("%d",&N); Build(1,1,N); int Q; scanf("%d",&Q); while(Q--) { int X,Y,Z; scanf("%d%d%d",&X,&Y,&Z); Update(1,1,N,X,Y,Z); } printf("Case %d: The total value of the hook is %d.\n",casen,Tree[1]); } return 0; }
相关文章推荐
- opencv之离散傅里叶变换(DFT)
- linux优化基础之更改“字符集”
- Linux下查看哪些文件占用磁盘空间比较大
- Linux 服务器安装配置 TimeMachine
- linux系统下python tab键补全(2步搞定)
- linux线程控制
- Opencv图像识别从零到精通(7)----图像平移、旋转、镜像
- 我的OpenCV学习笔记:VideoCapture类
- Hadoop 相关命令说明
- linux系统实用程序(1)
- Apache服务器运维笔记(4)----服务器扩展部分
- 云直播系统架构与实施
- Linux进程间通讯(IPC)------FIFO
- Centos6服务器部署JavaWeb项目(tomcat6+jdk6)
- CentOS6.7下安装MySQL
- 【COCI2012 Task 5】T6 poplocavanje ([JZOJ3172]贴瓷砖)(AC自动机模板)
- CentOS6.7下安装MySQL
- CentOS6.7下安装MySQL
- linux中搜索文件
- tomcat系统架构简介