hdu_4107Gangster_线段树_区间修改


Gangster

Time 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[n] 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 <cstdio>
#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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值