HDU-1698-Just a Hook(线段树 区间维护)
来源:互联网 发布:只看数据伯德被低估 编辑:程序博客网 时间:2024/05/22 02:25
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.
第三次写这道题了
代码
#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;}
- 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(线段树区间修改)
- HDU 1698 Just a Hook (线段树,区间更新)
- hdu 1698Just a Hook(线段树区间修改)
- HDU 1698 Just a Hook(线段树区间替换)
- (HDU 1698)Just a Hook 线段树区间修改
- HDU 1698 Just a Hook(线段树区间替换)
- HDU 1698 Just a Hook (线段树区间更新)
- 一.树莓派装系统
- 在RecyclerView中使用Glide和CircleImageView加载圆形图片时出现图片错乱
- Gulp学习笔记
- Android-打开相册或拍照并裁剪
- 灰度共生矩阵的定义以及算法
- HDU-1698-Just a Hook(线段树 区间维护)
- Android的Root权限——su与adb
- 【BZOJ2055】80人环游世界【有上下界的最小费用最大流】
- APP开发实战94-Vector静态图的使用
- 大话运动DV产品系统设计 - 基于芯片商提供的SDK API开发之一
- 进程管理相关的系统调用
- grep
- php变量引用赋值一些知识
- SpringMVC类型转换器、属性编辑器