hdoj 1698 Just a Hook 【线段树 区间修改】【线段树 + lazy思想】
来源:互联网 发布:php 获取数组某个元素 编辑:程序博客网 时间:2024/05/27 09:45
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20406 Accepted Submission(s): 10221
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.
区间修改(702ms):
#include <cstdio>#include <algorithm>#define INF 0x7f7f7f7f#define Max 600000#include <cstdlib>using namespace std;int c[Max];int setv[Max], sumv[Max];int _sum;int a, b, v;//记录查询区间 修改值 bool flagv[Max];//标记 void maintain(int o, int L, int R)//维护 { if (flagv[o]) { sumv[o] = (R-L+1)*setv[o]; } else if (R > L) { sumv[o] = sumv[o*2] + sumv[o*2+1]; } }void build(int o, int L, int R)//建树 { if (L == R) { //scanf("%lld", c+o);//输入n个数 sumv[o] = setv[o] = c[o] = 1; } else { int M = L + (R-L)/2; build(o<<1, L, M); build(o*2+1, M+1, R); maintain(o, L, R); } flagv[o] = false;//置0 }inline void pushdown(int o) { if (flagv[o]) { setv[o<<1] = setv[o<<1|1] = setv[o]; flagv[o<<1] = flagv[o<<1|1] = true; flagv[o] = false; }}void update(int o, int L, int R)//更新 { if (a <= L && b >= R) { setv[o] = v; flagv[o] = true; } else { int M = L + (R-L)/2; pushdown(o); if (a <= M) update(o<<1, L, M); else maintain(o<<1, L, M); if (b > M) update(o<<1|1, M+1, R); else maintain(o<<1|1, M+1, R); } maintain(o, L, R);}void query(int o, int L, int R) //查询 { if (flagv[o]) { _sum += setv[o]*(min(b, R) - max(a, L) + 1); } else if (a <= L && b >= R) { _sum += sumv[o]; } else { int M = L + (R-L)/2; if (a <= M) query(o<<1, L, M); if (b > M) query(o<<1|1, M+1, R); }}int main() { int t, n, m, j=1; scanf("%d",&t); while(t--) { scanf("%d", &n); build(1, 1, n); scanf("%d", &m); while(m--) { scanf("%d%d%d", &a, &b, &v); update(1, 1, n); } _sum=0; a = 1; b = n; query(1, 1, n); printf("Case %d: The total value of the hook is %d.\n", j++, _sum); } return 0;}
线段树 + lazy(764ms) :
#include <cstdio> #define MAX 100000+10int col[MAX<<2]; int sum[MAX<<2]; void PushUp(int o) { sum[o] = sum[o<<1] + sum[o<<1|1]; } void PushDown(int o, int m) { if (col[o]) { col[o<<1] = col[o<<1|1] = col[o]; sum[o<<1] = (m - (m >> 1)) * col[o]; sum[o<<1|1] = (m >> 1) * col[o]; col[o] = 0; } } void build(int o, int l, int r)//建树 { col[o] = 0; sum[o] = 1; if (l == r) return ; int mid = (l + r) >> 1; build(o<<1,l, mid); build(o<<1|1, mid+1, r); PushUp(o); } void update(int o, int l, int r, int L, int R, int v)//更新 { if (L <= l && r <= R) { col[o] = v; sum[o] = v * (r - l + 1); return ; } PushDown(o , r - l + 1); int mid = (l + r) >> 1; if (L <= mid) update(o<<1, l, mid, L , R , v); if (R > mid) update(o<<1|1, mid+1, r, L , R , v); PushUp(o); } int query(int o, int l, int r, int L, int R) //查询 { if(L <= l && R >= r) { return sum[o]; } PushDown(o, r-l+1); int mid = (r+l) >> 1; int res = 0; if(L <= mid) res += query(o<<1, l, mid, L, R); if(R > mid) res += query(o<<1|1, mid+1 , r, L, R); return res; }int main() { int t , n , m, j=1; int a, b, v; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); build(1 , 1 , n); while (m --) { scanf("%d%d%d", &a, &b, &v); update(1 , 1 , n, a, b, v); } printf("Case %d: The total value of the hook is %d.\n",j++ , query(1, 1, n, 1, n)); } return 0; }
还看到一个人用map 做的:
#include<cstdio>#include<map>#define N 100001using namespace std;map<int,int>m;map<int,int>::iterator it,jt;int a[N],x[N],y[N],num[N];int main(){int i,t,tt=0,n,q,ans;scanf("%d",&t);while(t--){scanf("%d%d",&n,&q);m.clear();for(i=1;i<=n;i++)m[i]=1;for(i=0;i<q;i++)scanf("%d%d%d",x+i,y+i,num+i);for(i=0;i<=n;i++)a[i]=1;for(i=q-1;i>=0;i--)for(it=m.lower_bound(x[i]);it!=m.end();)if(it->first<=y[i]){a[it->first]=num[i];jt=it++;m.erase(jt);}elsebreak;ans=0;for(i=1;i<=n;i++)ans+=a[i];printf("Case %d: The total value of the hook is %d.n",++tt,ans);}return 0;}
0 0
- hdoj 1698 Just a Hook 【线段树 区间修改】【线段树 + lazy思想】
- hdoj 1698 Just a Hook 【线段树区间修改】
- hdoj 1698 Just a Hook 【线段树 区间更新】
- HDU 1698 Just A Hook (线段树,区间修改)
- HDU - 1698 Just a Hook (线段树区间修改)
- HDU ACM 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 (线段树 区间修改查询)
- hdoj 1698 Just a Hook 【线段树】
- 【线段树】HDOJ 1698 Just a Hook
- hdoj 1698 Just a Hook【线段树】
- hdoj 1689 Just a Hook 【线段树区间更新求和】
- HDU 1698-Just a Hook(线段树_区间更新+lazy标记)
- HDU 1698 Just a Hook 线段树+lazy-target 区间刷新
- 【线段树(lazy)】hdu 1698 Just a Hook
- spring读取properties配置文件失败
- C语言程序设计提高篇应用程序设计-CSDN银行2.0
- Huawei Talk Band B2 Smart Bracelet Fitness Wearable Health Sports Compatible smart Mobile Phone
- Ubuntu14.04下安装Hadoop2.4.0 (伪分布模式)
- 算法分析之递归策略
- hdoj 1698 Just a Hook 【线段树 区间修改】【线段树 + lazy思想】
- C语言基础——笔记整理2
- python 视觉:基本的图像操作和处理
- Forstner算法的Matlab代码
- iterator的顶层const与底层const
- CodeForces 534C Polycarpus' Dice 【数学】
- UESTC 菲波拉契数制
- 编程之美---求首尾相连数组子数组之和的最大值
- Harris算法的Matlab代码