hdu_4107Gangster_线段树_区间修改
2014-10-13 10:32
411 查看
GangsterTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2526 Accepted Submission(s): 656 Problem Description There 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 |
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
const int maxn = 200000+10;
struct node{
int l,r,_max,_min,lazy;
}a[maxn<<2];
int n,m,P;
void Get(int cur){
a[cur]._max = max(a[cur<<1]._max,a[cur<<1|1]._max);
a[cur]._min = min(a[cur<<1]._min,a[cur<<1|1]._min);
}
void build(int l,int r,int cur){
a[cur].l = l;
a[cur].r = r ;
a[cur].lazy = a[cur]._min = a[cur]._max = 0;
if(l == r)
return ;
int mid = ( l + r )>>1;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
}
void pushdown(int cur){
if(a[cur].lazy){
a[cur<<1].lazy += a[cur].lazy;
a[cur<<1]._min += a[cur].lazy;
a[cur<<1]._max += a[cur].lazy;
a[cur<<1|1].lazy += a[cur].lazy;
a[cur<<1|1]._min += a[cur].lazy;
a[cur<<1|1]._max += a[cur].lazy;
a[cur].lazy = 0 ;
}
}
void update(int l,int r,int v,int cur){
if(l == a[cur].l && r == a[cur].r ){
if(a[cur]._max < P){
a[cur].lazy += v;
a[cur]._min += v;
a[cur]._max += v;
return ;
}
else if(a[cur]._min >= P){
a[cur].lazy += 2*v;
a[cur]._min += 2*v;
a[cur]._max += 2*v;
return ;
}
}
pushdown(cur);
int mid = (a[cur].l+a[cur].r)>>1;
if(r <= mid)
update(l,r,v,cur<<1);
else if( l > mid)
update(l,r,v,cur<<1|1);
else {
update(l,mid,v,cur<<1);
update(mid+1,r,v,cur<<1|1);
}
Get(cur);
}
void query(int l,int r,int cur){
if( l == r){
if(l != 1)
printf(" ");
printf("%d",a[cur].lazy);
return ;
}
// if(a[cur].lazy != 0){
// a[cur<<1].lazy += a[cur].lazy;
// a[cur<<1|1].lazy +=a[cur].lazy;
// a[cur].lazy = 0;
// }
pushdown(cur);
int mid =( l + r)>>1;
query(l,mid,cur<<1);
query(mid+1,r,cur<<1|1);
}
int main()
{
while(scanf("%d%d%d",&n,&m,&P)!=EOF){
build(1,n,1);
while(m--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1);
}
query(1,n,1);
printf("\n");
}
return 0;
}
相关文章推荐
- hdu 4107 Gangster(线段树,时间卡得很严)
- HDU 4027 Can you answer these queries? (线段树+区间点修改)
- HDU 1698——Just a Hook(线段树,区间修改)
- 【hdu】Just a Hook(线段树区间修改)
- HDU 4107 Gangster (线段树)
- hdu2795线段树,点修改,区间最值
- HDU 4302(zkw线段树-单点修改区间最值)
- hdu 4107 Gangster 线段树
- hdu 4902 Nice boat(线段树区间修改,输出最终序列)
- hdu 4107 Gangster 线段树 成段更新
- hdu 4107 Gangster(区间更新)
- hdu 4107 Gangster(线段树)
- hdu 1698 线段树 区间修改
- 【hdu】I Hate It(线段树,结点修改求区间最大值)
- hdu-1698-Just a Hook(线段树-区间修改模版)
- HDU 1698 线段树区间修改
- HDU 4027 Can you answer these queries? 线段树 区间修改 区间查询
- hdu 1166 线段树 单点修改 + 询问区间求和 (线段树模板)
- 树套树:二维线段树初步:hdu1823——Luck and Love(单点修改,区间查询)
- hdu 1166 敌兵布阵 线段树求区间和和修改点的值