Luogu3527:[POI2011]MET-Meteors

题面

Luogu

Sol

整体二分
比较简单,当练手题
每次树状数组统计

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(3e5 + 5); 

IL ll Input(){
    RG ll x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, k, ql[_], qr[_], qv[_], ans[_];
vector <int> son[_];
struct Data{
    int id;
    ll v;
} p[_], q1[_], q2[_];
ll bit[_];

IL void Modify(RG int x, RG ll v){
    for(; x <= m; x += x & -x) bit[x] += v;
}

IL ll Query(RG int x){
    RG ll ret = 0;
    for(; x; x -= x & -x) ret += bit[x];
    return ret;
}

IL void Solve(RG int l, RG int r, RG int L, RG int R){
    if(L > R) return;
    if(l == r){
        for(RG int i = L; i <= R; ++i) ans[p[i].id] = l;
        return;
    }
    RG int mid = (l + r) >> 1, t1 = 0, t2 = 0;
    for(RG int i = l; i <= mid; ++i)
        if(ql[i] <= qr[i])
            Modify(ql[i], qv[i]), Modify(qr[i] + 1, -qv[i]);
        else Modify(1, qv[i]), Modify(qr[i] + 1, -qv[i]), Modify(ql[i], qv[i]);
    for(RG int i = L; i <= R; ++i){
        RG ll sum = 0;
        for(RG int j = 0, len = son[p[i].id].size(); j < len; ++j)
            sum += Query(son[p[i].id][j]);
        if(p[i].v <= sum) q1[++t1] = p[i];
        else p[i].v -= sum, q2[++t2] = p[i];
    }
    for(RG int i = l; i <= mid; ++i)
        if(ql[i] <= qr[i])
            Modify(ql[i], -qv[i]), Modify(qr[i] + 1, qv[i]);
        else Modify(1, -qv[i]), Modify(qr[i] + 1, qv[i]), Modify(ql[i], -qv[i]);
    for(RG int i = L, j = 1; j <= t1; ++i, ++j) p[i] = q1[j];
    for(RG int i = L + t1, j = 1; j <= t2; ++i, ++j) p[i] = q2[j];
    Solve(l, mid, L, L + t1 - 1); Solve(mid + 1, r, L + t1, R);
}

int main(RG int argc, RG char* argv[]){
    n = Input(); m = Input();
    for(RG int i = 1; i <= m; ++i) son[Input()].push_back(i);
    for(RG int i = 1; i <= n; ++i) p[i].id = i, p[i].v = Input();
    k = Input();
    for(RG int i = 1; i <= k; ++i)
        ql[i] = Input(), qr[i] = Input(), qv[i] = Input();
    ql[k + 1] = 1; qr[k + 1] = m; qv[k + 1] = 1e9;
    Solve(1, k + 1, 1, n);
    for(RG int i = 1; i <= n; ++i)
        if(ans[i] == k + 1) puts("NIE");
        else printf("%d\n", ans[i]);
    return 0;
}
posted @ 2018-02-06 18:25  Cyhlnj  阅读(246)  评论(0编辑  收藏  举报