线段树区间更新Thermal Death of the Universe

来源:互联网 发布:wow淘宝卖的宏怎么样 编辑:程序博客网 时间:2024/05/13 03:07

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 ofn integer numbers. The life of the universe is represented as the sequence of the following entropy operations: take elements fromith 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 andm --- the size of the array and the number of operations respectively (1 <= m, n <= 30,000 ). The second line containsn integer numbers --- the initial contents of the array, they do not exceed109 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.

<b< dd="">

Output

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

There should be am empty line after each case.

<b< dd="">

Sample Input

6 41 2 3 4 5 61 22 55 64 6

<b< dd="">

Sample Output

2 3 3 5 5 5
还是模板题 要注意用LONG LONG 还有就是 ceil 向上取整 floor向下取证

#include <iostream>

#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=30005;
long long  SUM;
struct node
{
    long long  l,r,val,mark;
}tree[maxn*4];
void build(long long i,long long l,long long r)
{
    tree[i].mark=0;
    tree[i].val=0;
    tree[i].l=l,tree[i].r=r;
    //cout<<l<<" "<<r<<endl;
    if(l==r)
    {
        scanf("%lld",&tree[i].val);
        SUM+=tree[i].val;
        return;
    }
    long long m=(l+r)/2;
    build(i*2,l,m);
    build(i*2+1,m+1,r);
    tree[i].val=tree[i*2].val+tree[i*2+1].val;
}
void pushdown(long long i,long long v)
{
     if(tree[i].mark!=0)
    {
    tree[i*2].mark=tree[i].mark;
    tree[i*2+1].mark=tree[i].mark;


    tree[i*2+1].val=tree[i].mark*(v/2);    //区间中的个数
    tree[i*2].val=tree[i].mark*(v-v/2);


    tree[i].mark=0;
    }
}
void update(long long i,long long l,long long r,long long  add)
{


    if(tree[i].l==l&&r==tree[i].r)    //要更新的区间比i所含的区间大 全部包含
    {
        tree[i].mark=add;
        tree[i].val=add*(tree[i].r-tree[i].l+1);
        //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
        return;
    }
    if(tree[i].mark!=0)pushdown(i,tree[i].r-tree[i].l+1);
    //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
    long long m=(tree[i].l+tree[i].r)/2;
    if(r<=m)update(i*2,l,r,add);
    else if(l>m)update(i*2+1,l,r,add);
    else
    {
        update(i*2,l,m,add);
        update(i*2+1,m+1,r,add);
    }
    tree[i].val=tree[i*2].val+tree[i*2+1].val;


}
long long  query(long long  i,long long  l,long long  r)
{
    //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
    if(tree[i].l>=l&&tree[i].r<=r)
    return tree[i].val;
    pushdown(i,tree[i].r-tree[i].l+1);
    long long  ans=0;
    long long m=(tree[i].l+tree[i].r)/2;
    if(l<=m)ans+=query(i*2,l,r);
    if(r>m)ans+=query(i*2+1,l,r);
    return ans;


}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        SUM=0;
        build(1,1,n);
        while(m--)
        {
            long long  a,b;
            scanf("%lld%lld",&a,&b);
            long long now;
            double tmp=query(1,a,b);
            //cout<<tmp<<endl;
            tmp/=(b-a+1)*1.0;
            //cout<<query(1,1,n)<<endl;
           // cout<<SUM<<endl;
            if(query(1,1,n)<=SUM)
            {
                 //cout<<"2"<<endl;
                 now=(long long )ceil(tmp);
            }
            else
            {
                 //cout<<"1"<<endl;
                 now=(long long )floor(tmp);
            }
            //cout<<now<<endl;
            update(1,a,b,now);
        }
        for(int i=1;i<n;i++)
        cout<<query(1,i,i)<<" ";
        cout<<query(1,n,n)<<endl;
        cout<<endl;
    }
    return 0;
}
原创粉丝点击