hdu 4107 Gangster(線段樹。時間卡的很緊)

來源:互聯網
上載者:User

Total Submission(s): 2013    Accepted Submission(s): 520


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. 
InputThe 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). 
OutputPrint A[1] to A[n] in one line. All the numbers are separated by a space. 
Sample Input

3 2 11 2 12 3 1
 
Sample Output
1 3 1
 
Source2011 Alibaba-Cup Campus Contest 
Recommendlcy 

#include <iostream>#include<stdio.h>#include<string.h>using namespace std;const int maxn=200010;int addv[maxn<<2],ma[maxn<<2],mi[maxn<<2],ans[maxn],pp;//開始mi開小了居然TLE坑呀。。。void btree(int L,int R,int k){    int ls,rs,mid;    addv[k]=mi[k]=ma[k]=0;    if(L==R)        return ;    ls=k<<1;    rs=ls|1;    mid=(L+R)>>1;    btree(L,mid,ls);    btree(mid+1,R,rs);}void update(int L,int R,int l,int r,int k,int d){    int ls,rs,mid;    if(L==l&&R==r)    {        if(mi[k]>=pp)        {            addv[k]+=2*d;            mi[k]+=2*d;            ma[k]+=2*d;            return ;        }        else if(ma[k]<pp)        {            addv[k]+=d;            mi[k]+=d;            ma[k]+=d;            return ;        }    }    ls=k<<1;    rs=ls|1;    mid=(L+R)>>1;    if(addv[k])    {        addv[ls]+=addv[k];        addv[rs]+=addv[k];        mi[ls]+=addv[k];        ma[ls]+=addv[k];        mi[rs]+=addv[k];        ma[rs]+=addv[k];        addv[k]=0;    }    if(r<=mid)        update(L,mid,l,r,ls,d);    else if(l>mid)        update(mid+1,R,l,r,rs,d);    else    {        update(L,mid,l,mid,ls,d);        update(mid+1,R,mid+1,r,rs,d);    }    mi[k]=min(mi[ls],mi[rs])+addv[k];    ma[k]=max(ma[ls],ma[rs])+addv[k];}void getans(int L,int R,int k,int d){    int ls,rs,mid;    if(L==R)    {        ans[L]=addv[k]+d;        return;    }    ls=k<<1;    rs=ls|1;    mid=(L+R)>>1;    getans(mid+1,R,rs,d+addv[k]);    getans(L,mid,ls,d+addv[k]);}int main(){    int n,m,a,b,c,i;    while(~scanf("%d%d%d",&n,&m,&pp))    {        btree(1,n,1);        while(m--)        {            scanf("%d%d%d",&a,&b,&c);            update(1,n,a,b,1,c);        }        getans(1,n,1,0);        for(i=1;i<n;i++)            printf("%d ",ans[i]);        printf("%d\n",ans[n]);    }    return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.