【題解】Codeforces 1252G. Performance Review

【題目敘述】http://codeforces.com/contest/1252/problem/G
【解題想法】線段樹,懶標記

#include <iostream>
#include <vector>
using namespace std;
 
int n, m, q, t[400000], lazy[400000], a, b, c, d, rnk[100005];
vector <vector<int> > v;
 
void pull(int x){
    t[x] = max(t[x<<1], t[x<<1|1]);
}
void push(int x){
    t[x<<1] += lazy[x];
    t[x<<1|1] += lazy[x];
    lazy[x<<1] += lazy[x];
    lazy[x<<1|1] += lazy[x];
    lazy[x] = 0;
}
void build(int x, int l, int r){
    if (l == r){
        t[x] = rnk[l-1]+v[l].size()-1;
        return;
    }
    int mid = (l+r) >> 1;
    build(x<<1, l, mid);
    build(x<<1|1, mid+1, r);
    pull(x);
}
void update(int x, int l, int r, int ul, int d){
    if (ul <= l){
        t[x] += d;
        lazy[x] += d;
        return;
    }
    int mid = (l+r) >> 1;
    push(x);
    if (ul <= mid) update(x<<1, l, mid, ul, d);
    update(x<<1|1, mid+1, r, ul, d);
    pull(x);
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> q;
    cin >> a;
    rnk[0] = 1;
    for (int i = 2; i <= n; i++){
        cin >> b;
        if (b > a) rnk[0]++;
    }
    v.push_back({});
    for (int i = 1; i <= m; i++){
        rnk[i] = rnk[i-1];
        cin >> b;
        v.push_back({});
        v[i].push_back(0);
        for (int j = 0; j < b; j++){
            cin >> c;
            v[i].push_back(c);
            if (c > a) rnk[i]++;
        }
    }
    build(1, 1, m);
    for (int i = 0; i < q; i++){
        cin >> b >> c >> d;
        if (b != m){
            if (v[b][c] < a && d > a){
                update(1, 1, m, b+1, 1);
            }
            else if (v[b][c] > a && d < a){
                update(1, 1, m, b+1, -1);
            }
        }
        v[b][c] = d;
        if (t[1] > n) cout << 0 << "\n";
        else cout << 1 << "\n";
    }
}
分享本文 Share with friends