P3527 [POI2011]MET-Meteors

(color{#0066ff}{ 题目描述 })

Byteotian Interstellar Union有N个成员国。现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站。 这个星球经常会下陨石雨。BIU已经预测了接下来K场陨石雨的情况。 BIU的第i个成员国希望能够收集Pi单位的陨石样本。你的任务是判断对于每个国家,它需要在第几次陨石雨之后,才能收集足够的陨石。

(color{#0066ff}{输入格式})

第一行是两个数N,M。 第二行有M个数,第i个数Oi表示第i段轨道上有第Oi个国家的太空站。 第三行有N个数,第i个数Pi表示第i个国家希望收集的陨石数量。 第四行有一个数K,表示BIU预测了接下来的K场陨石雨。 接下来K行,每行有三个数Li,Ri,Ai,表示第K场陨石雨的发生地点在从Li顺时针到Ri的区间中(如果Li<=Ri,就是Li,Li+1,…,Ri,否则就是Ri,Ri+1,…,m-1,m,1,…,Li),向区间中的每个太空站提供Ai单位的陨石样本。

(color{#0066ff}{输出格式})

N行。第i行的数Wi表示第i个国家在第Wi波陨石雨之后能够收集到足够的陨石样本。如果到第K波结束后仍然收集不到,输出NIE。

(color{#0066ff}{输入样例})

3 5
1 3 2 1 3
10 5 7
3
4 2 4
1 3 1
3 5 2

(color{#0066ff}{输出样例})

3
NIE
1

(color{#0066ff}{数据范围与提示})

(1<=n,m,k<=3*10^5 1<=Pi<=10^9 1<=Ai<10^9)

(color{#0066ff}{题解})

整体二分

答案与操作次数有关,于是修改与查询可以分开搞,查询的区间进行二分,修改的东西单独开一个数组,对一段区间的修改操作进行修改即可

// luogu-judger-enable-o2
#include <bits/stdc++.h>
#define LL long long
inline LL in() {
	LL x = 0, f = 1; char ch;
	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return x * f;
}
const int maxn = 3e5 + 10;
struct Tree {
protected:
	LL st[maxn], n;
	int low(int x) { return x & (-x); }
public:
	void resize(int len) { n = len; }
	void add(int pos, LL k) { while(pos <= n) st[pos] += k, pos += low(pos); }
	LL query(int pos) { LL re = 0; while(pos) re += st[pos], pos -= low(pos); return re; }
}s;
struct node {
	int id, l, r;
	LL a;
	node(int id = 0, int l = 0, int r = 0, LL a = 0): id(id), l(l), r(r), a(a) {}
}u[maxn], q[maxn], ql[maxn], qr[maxn];
int n, m, k, ans[maxn];
std::vector<int> ct[maxn];
void work(int l, int r, int nl, int nr) {
	if(l == r) {
		for(int i = nl; i <= nr; i++) ans[u[i].id] = l;
		return;
	}
	int mid = (l + r) >> 1, cntl = 0, cntr = 0;
	for(int i = l; i <= mid; i++) {
		s.add(q[i].l, q[i].a);
		if(q[i].l > q[i].r) s.add(1, q[i].a);
		s.add(q[i].r + 1, -q[i].a);
	}
	for(int i = nl; i <= nr; i++) {
		LL tot = 0;
		for(int j = 0; j < (int)ct[u[i].id].size(); j++) {
			tot += s.query(ct[u[i].id][j]);
			if(tot >= u[i].a) break;
		}
		if(tot >= u[i].a) ql[++cntl] = u[i];
		else u[i].a -= tot, qr[++cntr] = u[i];
	}
	for(int i = l; i <= mid; i++) {
		s.add(q[i].l, -q[i].a);
		if(q[i].l > q[i].r) s.add(1, -q[i].a);
		s.add(q[i].r + 1, q[i].a);
	}
	for(int i = 1; i <= cntl; i++) u[nl + i - 1] = ql[i];
	for(int i = 1; i <= cntr; i++) u[nl + cntl + i - 1] = qr[i];
	work(l, mid, nl, nl + cntl - 1), work(mid + 1, r, nl + cntl, nr);
}
int main() {
	n = in(), m = in();
	s.resize(m);
	for(int i = 1; i <= m; i++) ct[in()].push_back(i);
	for(int i = 1; i <= n; i++) u[i] = node(i, 0, 0, in());
	k = in();
	for(int i = 1; i <= k; i++) {
		q[i].l = in(), q[i].r = in(), q[i].a = in();
		q[i].id = i;
	}
	work(1, k + 1, 1, n);
	for(int i = 1; i <= n; i++) ans[i] == k + 1? puts("NIE") : printf("%d
", ans[i]);
	return 0;
}
原文地址:https://www.cnblogs.com/olinr/p/10414432.html