洛谷P3586 [POI2015]LOG(贪心 权值线段树)

题意

题目链接

Sol

显然整个序列的形态对询问没什么影响

设权值\(>=s\)的有\(k\)个。

我们可以让这些数每次都被选择

那么剩下的数,假设值为\(a_i\)次,则可以\(a_i\)次被选择

一个显然的思路是每次选最大的C个

那么只需要判断\(\sum a_i >=(c - k)*s\)即可

权值线段树维护一下

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int a[MAXN], N, M;
const int SS = MAXN * 10 + 10, Mx = 1e9 + 10;
int ls[SS], rs[SS], num[SS], tot, root;
LL sum[SS];
void update(int k) {
	sum[k] = sum[ls[k]] + sum[rs[k]];
	num[k] = num[ls[k]] + num[rs[k]];
}
void Modify(int &k, int l, int r, int p, int v) {
	if(!k) k = ++tot;
	if(l == r) {
		num[k] += (v < 0 ? -1 : 1);
		sum[k] += v;
		return ;
	}
	int mid = l + r >> 1;
	if(p <= mid) Modify(ls[k], l, mid, p, v);
	else Modify(rs[k], mid + 1, r, p, v);
	update(k);
}
int QueryNum(int k, int l, int r, int ql, int qr) {
	if(!k) return 0;
	if(ql <= l && r <= qr) 
		return num[k];
	int mid = l + r >> 1;
	if(ql > mid) return QueryNum(rs[k], mid + 1, r, ql, qr);
	else if(qr <= mid) return QueryNum(ls[k], l, mid, ql, qr);
	else return QueryNum(ls[k], l, mid, ql, qr) + QueryNum(rs[k], mid + 1, r, ql, qr);
}
LL QuerySum(int k, int l, int r, int ql, int qr) {
	if(!k) return 0;
	if(ql <= l && r <= qr) return sum[k];
	int mid = l + r >> 1;
	if(ql > mid) return QuerySum(rs[k], mid + 1, r, ql, qr);
	else if(qr <= mid) return QuerySum(ls[k], l, mid, ql, qr);
	else return QuerySum(ls[k], l, mid, ql, qr) + QuerySum(rs[k], mid + 1, r, ql, qr);
}
signed main() {
    N = read(); M = read();
    while(M--) {
    	char s[3]; scanf("%s", s);
    	int x = read(), y = read();
    	if(s[0] == 'U') {//a[x] = y
			if(a[x]) Modify(root, 1, Mx, a[x], -a[x]);
			a[x] = y;
			if(y) Modify(root, 1, Mx, y, y);
		} else {//choose x = c   turn y = s
			int k = QueryNum(1, 1, Mx, y, Mx);
			LL sum = QuerySum(1, 1, Mx, 1, y - 1);
			puts((sum >= 1ll * (x - k) * y) ? "TAK" : "NIE");
		}
	}
	
    
    return 0;
}
/*
7
-1 160 -2000
14 82 61 85 41 10 34
*/
posted @ 2019-01-31 09:27  自为风月马前卒  阅读(351)  评论(0编辑  收藏  举报

Contact with me