HDU1698 Just a Hook 线段树入门题复习
来源:互联网 发布:如何用python写爬虫 编辑:程序博客网 时间:2024/04/28 03:31
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27230 Accepted Submission(s): 13526
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.
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.
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
11021 5 25 9 3
Sample Output
Case 1: The total value of the hook is 24.
思路:第一次写的时候每一次操作都去更新一个sum数组。TLE了。。
所以要用个标记数组,记录最后一次更新的数值。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn=100005;int sum[4*maxn];int col[4*maxn];//延迟数组void buildtree(int l,int r,int pos){ sum[pos]=0; col[pos]=0; if(l==r){sum[pos]=1;return;} else { int mid=(l+r)>>1; buildtree(l,mid,pos<<1); buildtree(mid+1,r,pos<<1|1); } sum[pos]=sum[pos<<1]+sum[pos<<1|1];}void update(int l,int r,int resl,int resr,int pos,int value){ if(l==r){sum[pos]=value;return;} if(resl<=l&&r<=resr)//属于这个区间的话 直接打延迟标记 { col[pos]=value; sum[pos]=(r-l+1)*value; //col[pos<<1]=col[pos<<1|1]=col[pos];//延迟标记送给儿子们 return; } //延迟标记送给儿子们了 int mid=(l+r)>>1; if(col[pos]) { col[pos<<1]=col[pos<<1|1]=col[pos]; sum[pos<<1]=col[pos]*(mid-l+1); sum[pos<<1|1]=col[pos]*(r-mid); col[pos]=0;//儿子已经更新完毕 } if(resl<=mid)update(l,mid,resl,resr,pos<<1,value); if(resr>mid)update(mid+1,r,resl,resr,pos<<1|1,value); sum[pos]=sum[pos<<1]+sum[pos<<1|1];}int main(){ int T,n,m,a,b,c; scanf("%d",&T); for(int t=1;t<=T;t++) { scanf("%d",&n); buildtree(1,n,1); scanf("%d",&m); while(m--) { scanf("%d%d%d",&a,&b,&c); update(1,n,a,b,1,c); } //show(1,n,1); printf("Case %d: The total value of the hook is %d.\n",t,sum[1]); }}
0 0
- HDU1698 Just a Hook 线段树入门题复习
- 【线段树】 hdu1698 Just a Hook
- Just a Hook(hdu1698,线段树)
- hdu1698 Just a Hook(线段树)
- 线段树之HDU1698 Just a Hook
- hdu1698 Just a Hook(线段树)
- Just a Hook HDU1698 线段树
- hdu1698 Just a Hook 线段树
- HDU1698[JUST A HOOK] 线段树
- 线段树 hdu1698 Just a Hook
- HDU1698:Just a Hook(线段树)
- hdu1698 Just a Hook (线段树)
- hdu1698 Just a Hook 线段树区间更新,模板题
- 线段树成断更新裸题hdu1698 Just a Hook
- hdu1698 Just a Hook 线段树之经典
- HDU1698--Just a hook--线段树区间更新
- HDU1698 Just a Hook 解题报告--线段树
- HDU1698:Just a Hook(线段树区间更新)
- 算法基础篇(数论):Catalan数计算及应用
- Request.Content读取2次
- 1025. 反转链表 (25)-浙大PAT乙级真题
- private最常见的应用:
- java插入图片的方法
- HDU1698 Just a Hook 线段树入门题复习
- 容器
- 关于新建项目多出的v7包
- thread_threadLocal(多线程共享数据)
- Prototype.js的使用手册
- Multi-thread下对int进行原子操作
- 数据挖掘Apriori算法
- git基础入门
- 1003. 我要通过!(20)-浙大PAT乙级真题