ZOJ2706-Thermal Death of the Universe

120 篇文章 1 订阅

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


Source:  Andrew Stankevich's Contest #10



题意:给n个数,m个操作,每次把区间[l,r]的数用它们都的它们的平均值替代,当它们的平均值不是整数时,若前n个数的和小于最初n个数的和就向上取整,不然就向下取整,最后输出每个数

解题思路:线段树


#include <iostream>  
#include <cstdio>  
#include <string>  
#include <cstring>  
#include <cmath> 
#include <algorithm>
#include <queue>  
#include <vector>  
#include <set>  
#include <stack>  
#include <map>  
#include <climits>  

using namespace std;

#define LL long long  
const int INF = 0x3f3f3f3f;

LL lazy[30009 << 2], sum[30009 << 2], res;
int n, q, x, y;
char ch[5];

void Push(int k, int l, int r)
{
	lazy[k << 1] = lazy[k << 1 | 1] = lazy[k];
	int mid = (l + r) >> 1;
	sum[k << 1] = lazy[k] * (mid - l + 1);
	sum[k << 1 | 1] = lazy[k] * (r - mid);
	lazy[k] = INF;
}

void build(int k, int l, int r)
{
	lazy[k] = INF;
	if (l == r) { scanf("%lld", &sum[k]); res += sum[k]; return; }
	int mid = (l + r) >> 1;
	build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
	sum[k] = sum[k << 1] + sum[k << 1 | 1];
}

void update(int k, int l, int r, int ll, int rr, LL val)
{
	if (ll <= l && r <= rr) { lazy[k] = val; sum[k] = 1LL * val * (r - l + 1); return; }
	int mid = (l + r) >> 1;
	if (lazy[k] != INF) Push(k, l, r);
	if (ll <= mid) update(k << 1, l, mid, ll, rr, val);
	if (mid < rr) update(k << 1 | 1, mid + 1, r, ll, rr, val);
	sum[k] = sum[k << 1] + sum[k << 1 | 1];
}

LL query(int k, int l, int r, int ll, int rr)
{
	if (ll <= l && r <= rr) return sum[k];
	int mid = (l + r) >> 1;
	if (lazy[k] != INF) Push(k, l, r);
	LL ans = 0;
	if (ll <= mid) ans += query(k << 1, l, mid, ll, rr);
	if (mid < rr) ans += query(k << 1 | 1, mid + 1, r, ll, rr);
	return ans;
}

int main()
{
	while (~scanf("%d%d", &n, &q))
	{
		res = 0;
		build(1, 1, n);
		while (q--)
		{
			scanf("%d%d", &x, &y);
			LL ans = query(1, 1, n, x, y);
			if (sum[1] <= res) ans = 1LL * ceil(1.0 * ans / (y - x + 1));
			else ans = 1LL * floor(1.0 * ans / (y - x + 1));
			update(1, 1, n, x, y, ans);
		}
		printf("%lld", query(1, 1, n, 1, 1));
		for (int i = 2; i <= n; i++) printf(" %lld", query(1, 1, n, i, i));
		printf("\n\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值