hdu 4107 Gangster 线段树 成段更新 最后求每一位的值

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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node
{
    int left,right;
    int _max,_min,lnc;//当前区间的最大值和最小值,当前区间需要加上的数(区间)
};
int n,m,p;
const int maxn=200000;
Node tree[maxn*4];
void buildtree(int id,int l,int r)
{
    tree[id].left=l,tree[id].right=r;
    tree[id]._max=tree[id]._min=tree[id].lnc=0;
    if(l!=r)
    {
        int mid=(l+r)>>1;
        buildtree(id<<1,l,mid);
        buildtree(id<<1|1,mid+1,r);
    }
}
void update(int id,int l,int r,int d)
{
    if(tree[id].left==l&&tree[id].right==r)
    {
        if(tree[id]._max<p)
        {
            tree[id].lnc+=d;
            tree[id]._max+=d;
            tree[id]._min+=d;
            return ;
        }
        if(tree[id]._min>=p)
        {
            tree[id].lnc+=d<<1;
            tree[id]._max+=d<<1;
            tree[id]._min+=d<<1;
            return ;
        }
    }
    if(tree[id].lnc)
    {
        tree[id<<1].lnc+=tree[id].lnc;
        tree[id<<1]._max+=tree[id].lnc;
        tree[id<<1]._min+=tree[id].lnc;
        tree[id<<1|1].lnc+=tree[id].lnc;
        tree[id<<1|1]._max+=tree[id].lnc;
        tree[id<<1|1]._min+=tree[id].lnc;
        tree[id].lnc=0;
    }
    int mid=(tree[id].left+tree[id].right)>>1;
    if(r<=mid) update(id<<1,l,r,d);
    else if(l>=mid+1) update(id<<1|1,l,r,d);
    else
    {
        update(id<<1,l,mid,d);
        update(id<<1|1,mid+1,r,d);
    }
    tree[id]._max=max(tree[id<<1]._max,tree[id<<1|1]._max);
    tree[id]._min=min(tree[id<<1]._min,tree[id<<1|1]._min);
}
int a[maxn];
void query(int id)
{
    if(tree[id].left==tree[id].right)
    {
        a[tree[id].left]=tree[id].lnc;
        return ;
    }
    if(tree[id].lnc)
    {
        tree[id<<1].lnc+=tree[id].lnc;
        tree[id<<1|1].lnc+=tree[id].lnc;
        tree[id].lnc=0;
    }
    query(id<<1);
    query(id<<1|1);
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&p)==3)
    {
        buildtree(1,1,n);
        while(m--)
        {
            int s,t,d;scanf("%d%d%d",&s,&t,&d);
            update(1,s,t,d);
        }
        query(1);
        printf("%d",a[1]);
        for(int i=2;i<=n;i++) printf(" %d",a[i]);printf("\n");
    }
    return 0;
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、5资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值