您的位置:首页 > 其它

【线段树】 hdu4107 Gangster

2012-07-31 13:57 211 查看


Gangster


http://acm.hdu.edu.cn/showproblem.php?pid=4107

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


题意:对方可以在同时给[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: