洛谷P3586 [POI2015]LOG(貪心 權值線段樹)

自為風月馬前卒發表於2019-01-31

題意

題目連結

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 << `
`;}
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
*/

相關文章