ZOJ 2706 Thermal Death of the Universe(线段树区间更新)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1706

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 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 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

题意:

给n个数,m个操作,每次把区间[l,r]的数用它们都的它们的平均值替代,

如果它们的平均值不是整数,并且当前n个数的和小于最初n个数的和就向上取整,不然就向下取整;

代码如下:

#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define LL long long
const int maxn = 111111;
LL add[maxn<<2];//用来标记每个节点,为0则表示没有标记,否则为标记;
LL sum[maxn<<2];//求和
LL SUM = 0;
LL ans = 0;
void PushUp(LL rt) //把当前结点的信息更新到父结点
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(LL rt,LL len)//把当前结点的信息更新给儿子结点,len为分区间长度
{
    if (add[rt]) //已经标记过,该区间被改变过
    {
        add[rt<<1] = add[rt];
        add[rt<<1|1] = add[rt];
        sum[rt<<1] = add[rt] * (len - (len >> 1));//更新左儿子的和
        sum[rt<<1|1] = add[rt] * (len >> 1);//更新右儿子的和
        add[rt] = 0;//将标记向儿子节点移动后,父节点的延迟标记去掉
        //传递后,当前节点标记域清空
    }
}
void build(LL l,LL r,LL rt)
{
    add[rt] = 0;//初始化为所有结点未被标记
    if (l == r)
    {
        scanf("%lld",&sum[rt]);
        SUM += sum[rt];
        return ;
    }
    LL mid = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}
void update(LL L,LL R,LL c,LL l,LL r,LL rt)
{
    if (L <= l && r <= R)
    {
        add[rt] = c;
        sum[rt] = (LL)c * (r - l + 1);
        return ;
    }
    PushDown(rt , r - l + 1);//----延迟标志域向下传递
    LL mid = (l + r) >> 1;
    if (L <= mid)
        update(L , R , c , lson);
    if (mid < R)
        update(L , R , c , rson);
    PushUp(rt);//向上传递更新和
}
LL query(LL L,LL R,LL l,LL r,LL rt)
{
    if(L <= l && r <= R)
    {
        return sum[rt];
    }//要取rt子节点的值时,也要先把rt的延迟标记向下移动
    PushDown(rt , r - l + 1);
    LL mid = (l + r) >> 1;
    LL ret = 0;
    if (L <= mid)
        ret += query(L , R , lson);
    if (mid < R)
        ret += query(L , R , rson);
    return ret;
}
int main()
{
    LL N , Q;
    while(~scanf("%lld%lld",&N,&Q))//N为节点数
    {
        SUM = 0;
        build(1 , N , 1);
        while(Q--)
        {
            char op[2];
            LL a , b , c;
            scanf("%lld%lld",&a,&b);
            double tt = query(a , b , 1 , N , 1);
            tt /= (b-a+1)*1.0;
            if(query(1 , N , 1 , N , 1) <= SUM)
            {
                ans = (LL)ceil(tt);
            }
            else
            {
                ans = (LL)floor(tt);
            }
            update(a , b , ans , 1 , N , 1);
        }
        for(int i = 1; i < N; i++)
        {
            printf("%lld ",query(i, i, 1, N , 1));
        }
        printf("%lld\n\n",query(N, N, 1, N , 1));
    }
    return 0;
}
时间: 2024-10-29 10:45:42

ZOJ 2706 Thermal Death of the Universe(线段树区间更新)的相关文章

zoj 2706 Thermal Death of the Universe (线段树区间更新,区间求和)

/* 题意:给n个数,m个操作,每次把区间[l,r]的数用它们的平均值替代, 如果平均值不是整数,且当前n个数的和小于原先的和就向上round,不然就向下round; */ #include <cstdio> # include <algorithm> using namespace std; #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 ///lson和rson分别表示结

ZOJ 2706 Thermal Death of the Universe (线段树)

题目链接:ZOJ 2706 Thermal Death of the Universe (线段树) 题意:n个数.m个操作. 每一个操作(a,b)表示(a,b)全部值更新为这个区间的平均数:1.当前的数列总和小于等于原数列总和.取平均值的上界,反之.取下界. 注意有负数的情况. AC代码: #include<stdio.h> #include <math.h> #define LL long long #define lson l,m,rt<<1 #define rso

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

ZOJ 1610 Count the Colors【题意+线段树区间更新&amp;&amp;单点查询】

任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent

ZOJ 1610 Count the Colors (线段树区间更新)

题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头输出. //ZOJ 1610 #include <cstdio> #include <cstring> #include <iostream> using namespace std ; int p[8010*4],lz[8010*4] ,hashh[8010*4],has

ZOJ 3632 Watermelon Full of Water (线段树 区间更新 + dp)

题目大意: 让每天都能吃到西瓜.最少需要花多少钱. 思路分析: dp[pos] 就表示  要让 前i天每天都有西瓜吃,最少需要花多少钱. 那么如果你买这个西瓜的话.那么这个西瓜能吃的持续时间都要更新一下. 然后再在每个西瓜的更新部分取最小的,就可以是这个点所能得到的最小值. 其实就是 dp[i] = min (dp[i] , dp[ j - k +1] + a[j]); 但是枚举前面的时候会超时,就用线段树维护. 5 1 2 3 4 5 1 2 2 2 2 给出这组数据是说,每次买西瓜的时候,都

POJ 2777 &amp;&amp; ZOJ 1610 &amp;&amp;HDU 1698 --线段树--区间更新

直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 -- 区间更新的话 分为 增减 或者 修改 主要就是个 laze 标记 就是延迟更新 对于区间更新的写法 一般是有2种 其一 仔细划分到每个细小的区间    另一 粗略划分 反正 ==我的代码里会给出2种写法 看自己喜好 hdu 1 //线段树 成段更新 ---> 替换 根结点的查询 2 3 #i

ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况就需要处理一下,代码中把所有的左端点都+1,避免了这种情况. 3. #include<iostream> #include<stdio.h> #include<string.h> using namespace std; #define L(root) ((root) &l

HDU 1689 Just a Hook 线段树区间更新求和

点击打开链接 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18894    Accepted Submission(s): 9483 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible