zoj 2706 Thermal Death of the Universe(线段树区间更新+懒惰标记)

Thermal Death of the Universe

Time Limit: 10 Seconds       Memory Limit: 32768 KB

Johnie has recently learned about the thermal death concept. Given that the Global Entropy always increases, it will end in the thermal death of the Universe. The idea has impressed him extremely. Johnie does not want the universe to die this way.

So he decided to emulate the process to check how soon the thermal death can occur. He has created the mathematical model of the process in the following way. The universe is represented as an array of n integer numbers. The life of the universe is represented as the sequence of the following entropy operations: take elements from ith to jth and replace them with their average value. Since their average is not necessarily integer, it is rounded.

To keep balance, rounding is performed either up, or down depending on the current sum of all elements of the array. If their sum is less or equal to the sum of the original array, the rounding is performed up, in the other case --- down.

Given the initial array and the sequence of the entropy operations, find the contents of the resulting array.

Input

There are mutiple cases in the input file.

The first line of each case contains n and m --- the size of the array and the number of operations respectively (1 <= m, n <= 30,000 ). The second line contains n integer numbers --- the initial contents of the array, they do not exceed 109 by their absolute value. The following m lines contain two integer numbers each and describe entropy operations.

There is an empty line after each case.

Output

Output n integer numbers --- the contents of the array after all operations.

There should be am empty line after each case.

Sample Input

6 4
1 2 3 4 5 6
1 2
2 5
5 6
4 6

Sample Output

2 3 3 5 5 5


Source:  Andrew Stankevich's Contest #10


分析:注意这里要分类讨论一下正负。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=31000;
long long tr[4*N];
long long add[4*N];
int n,m,x,y;
void PushUp(int i)
{
    tr[i]=tr[2*i]+tr[2*i+1];
}
void PushDown(int i,int len)
{
    if(add[i])
    {
        add[i<<1]=add[i<<1|1]=add[i];
        tr[i<<1]=(len-(len>>1))*add[i];
        tr[i<<1|1]=(len>>1)*add[i];
        add[i]=0;
    }
}
void build(int i,int l,int r)
{
    tr[i]=0;
    add[i]=0;
    if(l==r)
    {
        scanf("%lld",&tr[i]);
        return ;
    }
    int mid=(l+r)/2;
    build(2*i,l,mid);
    build(2*i+1,mid+1,r);
    PushUp(i);
}
void update(int i,int l,int r,int x,int y,long long c)
{
    if(x<=l&&r<=y)
    {
        tr[i]=(r-l+1)*c;
        add[i]=c;
        return;
    }
    PushDown(i,r-l+1);
    int mid=(l+r)/2;
    if(x<=mid) update(2*i,l,mid,x,y,c);
    if(y>mid) update(2*i+1,mid+1,r,x,y,c);
    PushUp(i);
}
long long query(int i,int l,int r,int x,int y)
{
    long long sum=0;
    if(x<=l&&r<=y)
    {
        return tr[i];
    }
    PushDown(i,r-l+1);
    int mid=(l+r)/2;
    if(x<=mid) sum+=query(2*i,l,mid,x,y);
    if(y>mid) sum+=query(2*i+1,mid+1,r,x,y);
    return sum;
}
void print(int i,int l,int r)
{
    PushDown(i,r-l+1);
    if(l==r)
    {
        if(l!=n) printf("%lld ",tr[i]);
        else printf("%lld\n",tr[i]);
        return;
    }
    int mid=(l+r)/2;
    print(2*i,l,mid);
    print(2*i+1,mid+1,r);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        build(1,1,n);
        long long org=tr[1];
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            long long sum=query(1,1,n,x,y);
            long long dat=sum/(y-x+1);
                if(sum>0&&query(1,1,n,1,n)<=org)
                    if(sum%(y-x+1)!=0) dat++;
                if(sum<0&&query(1,1,n,1,n)>org)
                    if(sum%(y-x+1)!=0) dat--;
            update(1,1,n,x,y,dat);
        }
        print(1,1,n);
        printf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值