天天看點

ZOJ 2706 Thermal Death of the Universe

Description

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      
#include<cstdio>
#include<vector>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=500005;
typedef long long LL;
LL n,m,l,r;
LL sum,u;
struct poLL
{
  LL sum,ad,add;
  poLL(){sum=0; ad=0; add=0;}
}f[maxn];

void build(LL now,LL l,LL r)
{
  if (l==r)
  { 
    scanf("%lld",&f[now].sum);
    f[now].ad=f[now].add=0;
  }
  else 
  {
    LL mid=(l+r)>>1;
    build(now+now,l,mid);
    build(now+now+1,mid+1,r);
    f[now].sum=f[now+now].sum+f[now+now+1].sum;
    f[now].ad=f[now].add=0;
  }
}

void down(LL now,LL l,LL r)
{
  f[now].ad=0;
  f[now+now].ad=f[now+now+1].ad=1;
  f[now+now].add=f[now+now+1].add=f[now].add;
  LL mid=(l+r)>>1;
  f[now+now].sum=f[now].add*(mid-l+1);
  f[now+now+1].sum=f[now].add*(r-mid);
}

void add(LL now,LL l,LL r,LL ll,LL rr,LL u)
{
  if (f[now].ad) down(now,l,r);
  if (ll<=l&&rr>=r)
  {
    f[now].ad=1; f[now].add=u;
    f[now].sum=(r-l+1)*u;
  }
  else 
  {
    LL mid=(l+r)>>1;
    if (ll<=mid) add(now+now,l,mid,ll,rr,u);
    if (rr>mid) add(now+now+1,mid+1,r,ll,rr,u);
    f[now].sum=f[now+now].sum+f[now+now+1].sum;
  }
}

void get(LL now,LL l,LL r,LL ll,LL rr,LL ad,LL add)
{
  if (!ad) {ad=f[now].ad; add=f[now].add;}
  if (ll<=l&&rr>=r) 
  {
    if (ad) f[now].sum=add*(r-l+1);
    u+=f[now].sum;
  }
  else 
  {
    LL mid=(l+r)>>1;
    if (ll<=mid) get(now+now,l,mid,ll,rr,ad,add);
    if (rr>mid) get(now+now+1,mid+1,r,ll,rr,ad,add);
  }
}

void change(LL l,LL r)
{
  u=0;
  get(1,1,n,l,r,0,0);
  if (u>=0)
    if (sum<f[1].sum) u=u/(r-l+1);
    else u=(u-1)/(r-l+1)+1;
  else 
    if (sum>=f[1].sum) u=u/(r-l+1);
    else u=(u+1)/(r-l+1)-1;
  add(1,1,n,l,r,u);
}

int main()
{
  while (cin>>n>>m) 
  {
    build(1,1,n);
    sum=f[1].sum;
    while (m--) 
    {
      scanf("%d%d",&l,&r);
      change(l,r);
    }
    for (LL i=1;i<=n;i++) 
    {
      u=0;
      get(1,1,n,i,i,0,0);
      printf("%lld",u);
      if (i<n) printf(" ");
    }
    printf("\n\n");
  }
  return 0;
}