【线段树】 hdu4107 Gangster
2012-07-31 13:57
211 查看
Gangster
http://acm.hdu.edu.cn/showproblem.php?pid=4107
Problem DescriptionThere are two groups of gangsters fighting with each other. The first group stands in a line, but the other group has a magic gun that can shoot a range [a, b], and everyone in that range will take a damage of c points. When a gangster is taking damage, if
he has already taken at least P point of damage, then the damage will be doubled. You are required to calculate the damage that each gangster in the first group toke.
To simplify the problem, you are given an array A of length N and a magic number P. Initially, all the elements in this array are 0.
Now, you have to perform a sequence of operation. Each operation is represented as (a, b, c), which means: For each A[i] (a <= i <= b), if A[i] < P, then A[i] will be A[i] + c, else A[i] will be A[i] + c * 2.
Compute all the elements in this array when all the operations finish.
Input
The input consists several testcases.
The first line contains three integers n, m, P (1 <= n, m, P <= 200000), denoting the size of the array, the number of operations and the magic number.
Next m lines represent the operations. Each operation consists of three integers a; b and c (1 <= a <= b <= n, 1 <= c <= 20).
Output
Print A[1] to A
in one line. All the numbers are separated by a space.
Sample Input
3 2 1 1 2 1 2 3 1
Sample Output
1 3 1
题意:对方可以在同时给[a,b]区间内每个点c的伤害,而且当该点已受伤害大于等于p时就会受到2*c的伤害,问题最后每个点受到多少伤害。
明显的线段树题目,维护每段的最大值和最小值,当该段的最大值小于p时确定该段受1倍的伤害,当该段的最小值大于等于p时受2倍伤害,同时更新时也不需每次都更新到底。
ps:时间卡的很紧,加了输入的优化才c++ 900+ms过的,g++还不能过。
#include<cstdio> #include<cstring> using namespace std; #define MAX 200005 #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) int sum[MAX<<2]; int maxx[MAX<<2]; int minn[MAX<<2]; int p; void add(int v,int idx) { maxx[idx]+=v; minn[idx]+=v; } void push_up(int l,int r,int idx) { minn[idx]=min(minn[idx<<1],minn[idx<<1|1]); maxx[idx]=max(maxx[idx<<1],maxx[idx<<1|1]); } void push_down(int l,int r,int idx) { if(sum[idx]) { sum[idx<<1]+=sum[idx]; add(sum[idx],idx<<1); sum[idx<<1|1]+=sum[idx]; add(sum[idx],idx<<1|1); sum[idx]=0; } } void build(int l,int r,int idx) { sum[idx]=maxx[idx]=minn[idx]=0; if(l==r) return; int mid=(l+r)>>1; build(l,mid,idx<<1); build(mid+1,r,idx<<1|1); } void update(int a,int b,int c,int l,int r,int idx) { if(a<=l&&r<=b) { if(maxx[idx]<p) { sum[idx]+=c; add(c,idx); return ; } else if(minn[idx]>=p) { sum[idx]+=(c*2); add(c*2,idx); return; } } push_down(l,r,idx); int mid=(l+r)>>1; if(a<=mid) update(a,b,c,l,mid,idx<<1); if(b>mid) update(a,b,c,mid+1,r,idx<<1|1); push_up(l,r,idx); } void query(int l,int r,int idx) { if(minn[idx]==maxx[idx]) { for(int i=l; i<=r; ++i) if(i==1) printf("%d",minn[idx]); else printf(" %d",minn[idx]); return; } push_down(l,r,idx); int mid=(l+r)>>1; query(l,mid,idx<<1); query(mid+1,r,idx<<1|1); } inline void scan(int &n) { char cc ; for(;cc=getchar(),cc<'0'||cc>'9';); n=cc-'0'; for(;cc=getchar(),cc>='0'&&cc<='9';) n=n*10+cc-'0'; } int main() { int n,m,a,b,c; for(; ~scanf("%d%d%d",&n,&m,&p);) { build(1,n,1); for(; m--;) { //scanf("%d%d%d",&a,&b,&c); scan(a); scan(b); scan(c); update(a,b,c,1,n,1); } query(1,n,1); puts(""); } return 0; }
相关文章推荐
- hdu4107 Gangster (线段树,段更新完后求数组内的值)
- HDU4107 Gangster 线段树 段更新
- HDOJ 题目4107 Gangster(线段树)
- hdu_4107Gangster_线段树_区间修改
- hdu 4107 Gangster(线段树)
- HDU 4107 Gangster(线段树 特殊懒惰标记)
- hdu 4107 Gangster 线段树 成段更新 最后求每一位的值
- hdu 4107 Gangster(线段树,时间卡得很严)
- 线段树-hdu-Gangster
- HDOJ 4107 Gangster 线段树 成段更新变化
- hdu 4107 Gangster 线段树 成段更新
- hdu 4107 Gangster(线段树,时间卡得很严)
- HDU 4107 Gangster (线段树)
- hdu4107 线段树水题
- hdu 4107 Gangster 线段树
- [poj 1166]敌兵布阵[线段树基础]
- hdu 1540 & poj 2892 Tunnel Warfare 线段树区间合并
- [BZOJ3110][Zjoi2013]K大数查询(主席树套线段树||整体二分 )
- POJ 2763 Housewife Wind(树链剖分+线段树点更新-区间查询+入边)
- [JSOI2008]Blue Mary开公司(李超线段树)