【BZOJ4378】[POI2015]Logistyka【树状数组】【结论题】

【题目链接】

题解:

首先得有一个结论:设个数大于s的数字有k个(如果k大于c,显然是TAK。这里讨论k <= c),那么如果个数小于s的数字和不小于(c - k) * s,那么一定有解。

并不会证明...

用树状数组记录一下数字的个数以及数字的数字和,然后判断就可以了。


复杂度:

时间复杂度:O(mlogm),空间复杂度:O(m)。


1A。


GET:

结论题...

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const int maxn = 1000005;

int n, m, num[maxn], disc[maxn], tot, trnum[maxn];
LL trsum[maxn];

struct _que {
	int opt, a, b;
} que[maxn];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();	
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int find(int x) {
	int l = 1, r = tot;
	while(l <= r) {
		int mid = l + r >> 1;
		if(disc[mid] < x) l = mid + 1;
		else if(disc[mid] == x) return mid;
		else r = mid - 1;
	}
}

inline void addnum(int x, int c) {
	for(; x <= tot; x += x & -x) trnum[x] += c;
}

inline int querynum(int x) {
	int res = 0;
	for(; x; x -= x & -x) res += trnum[x];
	return res;
}

inline void addsum(int x, int c) {
	for(; x <= tot; x += x & -x) trsum[x] += c;
}

inline LL querysum(int x) {
	LL res = 0;
	for(; x; x -= x & -x) res += trsum[x];
	return res;
}

int main() {
	iread(); m = iread();
	for(int i = 1; i <= m; i++) {
		char ch = getchar(); for(; ch != 'U' && ch != 'Z'; ch = getchar());
		que[i].a = iread(); que[i].b = disc[++tot] = iread(); 
		que[i].opt = (ch == 'U');
	}

	sort(disc + 1, disc + 1 + tot);
	tot = unique(disc + 1, disc + 1 + tot) - (disc + 1);

	for(int i = 1; i <= m; i++)
		if(que[i].opt == 1) {
			int x = que[i].a, y = find(que[i].b);
			if(!num[x] && que[i].b) addnum(y, 1), n++;
			else if(num[x] && !que[i].b) addnum(y, -1), n--;
			else if(num[x] && que[i].b) addnum(find(num[x]), -1), addnum(y, 1);
			if(num[x]) addsum(find(num[x]), -num[x]);
			if(que[i].b) addsum(y, que[i].b);
			num[x] = que[i].b;
		} else {
			int x = que[i].a, y = find(que[i].b);
			int k = n - querynum(y);
			if(k > x) printf("TAK\n");
			else {
				LL sum = querysum(y);
				printf(sum >= (LL)(x - k) * que[i].b ? "TAK\n" : "NIE\n");
			}
		}

	return 0;
}


你可能感兴趣的:(【BZOJ4378】[POI2015]Logistyka【树状数组】【结论题】)