ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

zoj2706 Thermal Death of the Universe 线段树正常模拟

2019-09-20 21:04:52  阅读:328  来源: 互联网

标签:operations case Death Universe sum zoj2706 array include ll


传送门

题面

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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

const ll N = 3e4 + 50;

ll a[N], n, m, init_sum;

struct Node {
    ll l, r, val, lz;
} c[N << 2];

inline void pushup(ll p) {
    c[p].val = c[p << 1].val + c[p << 1 | 1].val;
}

inline void pushdown(ll p) {
    if (c[p].lz != 0) {
        c[p << 1].lz = c[p << 1 | 1].lz = c[p].lz;
        c[p << 1].val = (c[p << 1].r - c[p << 1].l + 1) * c[p].lz;
        c[p << 1 | 1].val = (c[p << 1 | 1].r - c[p << 1 | 1].l + 1) * c[p].lz;
        c[p].lz = 0;
    }
}

void build(ll p, ll l, ll r) {
    c[p].l = l, c[p].r = r;
    c[p].lz = 0;
    if (l == r) {
        c[p].val = a[c[p].l];
        return;
    }
    ll mid = (c[p].l + c[p].r) >> 1;
    build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r);
    pushup(p);
}

void update(ll p, ll l, ll r, ll val) {
    if (l <= c[p].l && c[p].r <= r) {
        c[p].val = (c[p].r - c[p].l + 1) * val;
        c[p].lz = val;
        return;
    }
    pushdown(p);
    ll mid = (c[p].l + c[p].r) >> 1;
    if (l <= mid) update(p << 1, l, r, val);
    if (r > mid) update(p << 1 | 1, l, r, val);
    pushup(p);
}

ll query(ll p, ll l, ll r) {
    if (l <= c[p].l && c[p].r <= r)  return c[p].val;
    pushdown(p);
    ll mid = (c[p].l + c[p].r) >> 1;
    ll ans = 0;
    if (l <= mid) ans += query(p << 1, l, r);
    if (r > mid) ans += query(p << 1 | 1, l, r);
    return ans;
}

void print(ll p) {
    if (c[p].l == c[p].r) {
        if (c[p].l != n) cout << c[p].val << ' ';
        else cout << c[p].val << endl;
        return;
    }
    pushdown(p);
    print(p << 1);
    print(p << 1 | 1);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    while (cin >> n >> m) {
        init_sum = 0;
        for (ll i = 1; i <= n; i++) cin >> a[i], init_sum += a[i];
        build(1, 1, n);
        for (ll i = 1, l, r; i <= m; i++) {
            cin >> l >> r;
            ll sum = query(1, l, r);
            ll now_sum = query(1, 1, n), k = sum / (r - l + 1);
            if (now_sum <= init_sum) {
                if (sum % (r - l + 1)) k += (sum >= 0);
            } else {
                if (sum % (r - l + 1)) k -= (sum < 0);
            }
            update(1, l, r, k);
        }
        print(1);
        cout << endl;
    }
    return 0;
}

标签:operations,case,Death,Universe,sum,zoj2706,array,include,ll
来源: https://blog.csdn.net/qq_43279710/article/details/101077368

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有