Zoj 2706 thermal death of the universe

Source: Internet
Author: User
Tags integer numbers

 

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 arrayNInteger numbers. The life of the universe is represented as the sequence of the following entropy operations: take elements fromIThJTh and replace them with their average value. Since their average is not necessarily integer, It is rounded.

To keep balance, rounding is performed med 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 timed 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 containsNAndM--- The size of the array and the number of operations respectively (1 <= m, n <= 30,000). The second line containsNInteger numbers --- the initial contents of the array, they do not exceed109By their absolute value. The followingMLines contain two integer numbers each and describe entropy operations.

There is an empty line after each case.

Output

OutputNInteger numbers --- the contents of the array after all operations.

There shoshould be am empty line after each case.

Sample Input

6 41 2 3 4 5 61 22 55 64 6

Sample output

2 3 3 5 5 5

 

A naked line segment tree needs to be updated in a delayed manner ~~ Note that the details are strongly converted to (LL) (R-l + 1) * V when the values are evaluated.

 

#include <bits/stdc++.h>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lr rt<<1#define rr rt<<1|1using namespace std;typedef long long LL;const int N = 30005;LL osum;int n,q;struct ST{    int l,r;    LL lazy,date;    int mid(){        return (l+r)>>1;    }}st[N<<2];void push_down(int rt){    if( st[rt].lazy )    {      st[lr].lazy =  st[rr].lazy = st[rt].lazy;       st[lr].date = (LL)( st[lr].r - st[lr].l +1 ) * st[lr].lazy;       st[rr].date = (LL)( st[rr].r - st[rr].l +1 ) * st[rr].lazy;      st[rt].lazy=0;    }}void build(int l,int r,int rt){    st[rt].l=l,st[rt].r=r;    st[rt].lazy=0;    if(l==r)    {        scanf("%lld",&st[rt].date);        osum += st[rt].date;        return ;    }    int m=st[rt].mid();    build(lson);    build(rson);    st[rt].date = st[rr].date + st[lr].date;}void update(int l,int r,int rt,LL v){    if( l == st[rt].l && st[rt].r == r )    {        st[rt].date= (LL) ( r - l + 1 ) * v ;        st[rt].lazy=v;        return;    }    push_down(rt);    int m=st[rt].mid();    if( l > m )        update(l,r,rr,v);    else if(r <= m)        update(l,r,lr,v);    else{            update(lson,v);            update(rson,v);        }    st[rt].date = st[rr].date + st[lr].date;}LL query(int l,int r,int rt){    if( l==st[rt].l && r==st[rt].r  )    {        return st[rt].date;    }    push_down(rt);        int m=st[rt].mid();    if(l > m)        return query(l,r,rr);    else if( r <= m)        return query(l,r,lr);    else        return query(lson)+query(rson);}void show(int rt){    if(st[rt].l == st[rt].r)    {        printf("%lld",st[rt].date);        if(st[rt].r != n)printf(" ");        else printf("\n");        return;    }    int m=st[rt].mid();    push_down(rt);    show(lr);    show(rr);}int main(){    int x,y;   // freopen("in.txt","r",stdin);    while(~scanf("%d%d",&n,&q)){        osum=0;        build(1,n,1);        while(q--)        {            scanf("%d%d",&x,&y);            if(x>y)swap(x,y);            LL tmp = query(x,y,1);            double ave = (long double)tmp/(y-x+1);            if( st[1].date <= osum ) update(x, y,1,(LL)ceil(ave));            else update( x, y, 1,(LL)floor(ave) );          }        show(1);        puts("");    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.