hdu 4107 Gangster(线段树。时间卡的很紧)

来源:互联网 发布:hadoop做数据分析 编辑:程序博客网 时间:2024/05/03 14:20

Gangster

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2013    Accepted Submission(s): 520


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 11 2 12 3 1
 

Sample Output
1 3 1
 

Source
2011 Alibaba-Cup Campus Contest
 

Recommend
lcy
 

题意:

给你一个数组和一个p。开始数组的每一个元素都为0.然后进行m次操作。

每次操作给l,r和c。如果l和r区间的数小于p就加c否则加2*c。

思路:

不好区间操作。区间更新必须满足一定条件。如果区间元素的最大值ma[k]<p的话可以直接给区间加上c。

如果区间的最小值都大于等于p的话。那么可以直接给区间加上2*c了。

其它情况就不能lazy了。比赛时写了一半了。就差一点。。。。

详细见代码:

#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;}


原创粉丝点击