Submission #130173

# Submission time Handle Problem Language Result Execution time Memory
130173 2019-07-14T07:24:47 Z choikiwon None (JOI16_worst_reporter2) C++17
100 / 100
632 ms 71388 KB
#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

const int MN = 200010;

int N;
vector<int> V[MN], X[MN], L[MN];
priority_queue<int> freeV;
multiset<int> freeX;

struct BIT {
    int Z;
    vector<pii> tree;
    vector<int> lazy;
    void init(int Z_) {
        Z = Z_;
        tree = vector<pii>(4 * Z);
        lazy = vector<int>(4 * Z, 0);
        build(0, Z - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n] = {0, -l};
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = min(tree[2*n], tree[2*n + 1]);
    }
    void prop(int l, int r, int n) {
        if(l != r) {
            tree[2*n].first += lazy[n];
            lazy[2*n] += lazy[n];
            tree[2*n + 1].first += lazy[n];
            lazy[2*n + 1] += lazy[n];
            lazy[n] = 0;
        }
    }
    void upd(int a, int b, int d, int l, int r, int n) {
        if(b < l || r < a) return;
        if(a <= l && r <= b) {
            tree[n].first += d;
            lazy[n] += d;
            return;
        }
        prop(l, r, n);
        int m = (l + r)>>1;
        upd(a, b, d, l, m, 2*n);
        upd(a, b, d, m + 1, r, 2*n + 1);
        tree[n] = min(tree[2*n], tree[2*n + 1]);
    }
    pii quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return pii(1e9, 1e9);
        if(a <= l && r <= b) return tree[n];
        prop(l, r, n);
        int m = (l + r)>>1;
        pii L = quer(a, b, l, m, 2*n);
        pii R = quer(a, b, m + 1, r, 2*n + 1);
        return min(L, R);
    }
    void upd(int a, int b, int d) { upd(a, b, d, 0, Z - 1, 1); }
    pii quer(int a, int b) { return quer(a, b, 0, Z - 1, 1); }
};

struct RMQ {
    int Z;
    vector<int> tree;
    void init(int Z_) {
        Z = Z_;
        tree = vector<int>(4 * Z);
        build(0, Z - 1, 1);
    }
    void build(int l, int r, int n) {
        if(l == r) {
            tree[n] = l;
            return;
        }
        int m = (l + r)>>1;
        build(l, m, 2*n);
        build(m + 1, r, 2*n + 1);
        tree[n] = max(tree[2*n], tree[2*n + 1]);
    }
    void upd(int x, int v, int l, int r, int n) {
        if(x < l || r < x) return;
        if(l == r) {
            tree[n] = v;
            return;
        }
        int m = (l + r)>>1;
        upd(x, v, l, m, 2*n);
        upd(x, v, m + 1, r, 2*n + 1);
        tree[n] = max(tree[2*n], tree[2*n + 1]);
    }
    void upd(int x, int v) { upd(x, v, 0, Z - 1, 1); }
};

BIT bit[MN];
RMQ rmq[MN];

struct Info {
    int v, i, j, l, x, y;
    bool operator <(const Info &o) const {
        if(v != o.v) return v < o.v;
        if(i != o.i) return i < o.i;
        return j < o.j;
    }
};
priority_queue<Info> cand, pq, P;
int la[MN];

void relax() {
    while(!cand.empty()) {
        Info t = cand.top();

        int x = t.j;
        if(t.l != la[x] || t.y != bit[x].quer(la[x], la[x]).first) {
            cand.pop();
            continue;
        }
        break;
    }
}

int main() {
    std::ios::sync_with_stdio(false);

    cin >> N;
    //N = 50;

    for(int i = 0; i < N; i++) {
        int a, b; cin >> a >> b;
        //int a = rand() % 20 + 1;
        //int b = (i + 1) * (i + 1);

        V[--a].push_back(b);
    }
    for(int i = 0; i < N; i++) {
        int c, d; cin >> c >> d;
        //int c = rand() % 20 + 1;
        //int d = rand() % 10 == 0? (i + 1) * (i + 2) : (i + 1) * (i + 1);

        X[--c].push_back(d);
    }

    int ans = 0;
    for(int i = 0; i < N; i++) {
        sort(V[i].begin(), V[i].end());
        sort(X[i].begin(), X[i].end());

        vector<int> chk(V[i].size(), 0);
        vector<int> tmp;

        int pos = (int)X[i].size() - 1;
        for(int j = (int)V[i].size() - 1; j >= 0; j--) {
            if(pos >= 0 && V[i][j] <= X[i][pos]) {
                pos--;
                continue;
            }
            freeV.push(V[i][j]);
            chk[j] = 1;
        }
        for(int j = 0; j < V[i].size(); j++) if(!chk[j]) tmp.push_back(V[i][j]);
        V[i] = tmp;

        if(V[i].size() == 0) {
            for(int j = 0; j < X[i].size(); j++) {
                freeX.insert(X[i][j]);
            }
            la[i] = -1;
            continue;
        }

        bit[i].init(V[i].size());
        rmq[i].init(V[i].size());

        chk = vector<int>(X[i].size(), 0);

        pos = 0;
        for(int j = 0; j < V[i].size(); j++) {
            while(pos < X[i].size() && X[i][pos] < V[i][j]) pos++;
            chk[pos] = 1;
            pos++;

            P.push({V[i][j], i, j});
        }

        tmp.clear();
        for(int j = 0; j < X[i].size(); j++) {
            if(chk[j]) tmp.push_back(X[i][j]);
            else freeX.insert(X[i][j]);
        }
        X[i] = tmp;

        L[i].resize(V[i].size());

        pos = (int)X[i].size() - 1;
        for(int j = (int)V[i].size() - 1; j >= 0; j--) {
            while(pos >= 0 && X[i][pos] >= V[i][j]) pos--;
            bit[i].upd(j, j, j - pos - 1);
            L[i][j] = pos + 1;
        }

        pii t = bit[i].tree[1];
        la[i] = (int)V[i].size() - 1;
        pq.push({ X[i].back(), V[i][-t.second], i, la[i], -t.second, bit[i].quer(la[i], la[i]).first });
    }

    ans += freeV.size();

    while(!freeV.empty()) {
        int v = freeV.top(); freeV.pop();

        //cout << v << endl;

        while(!P.empty() && P.top().v >= v) {
            Info t = P.top(); P.pop();
            int x = t.i;
            if(t.j != la[x]) {
                continue;
            }

            bit[x].upd(la[x], la[x], 1e9);
            rmq[x].upd(la[x], -1);
            la[x] = rmq[x].tree[1];

            if(la[x] >= 0) {
                pii t = bit[x].tree[1];
                pii d = bit[x].quer(la[x], la[x]);
                pq.push({ X[x][ L[x][ la[x] ] + d.first ], V[x][-t.second], x, la[x], -t.second, d.first });
            }
        }
        while(!pq.empty() && pq.top().v >= v) {
            Info t = pq.top(); pq.pop();
            cand.push({ -t.i, t.v, t.j, t.l, t.x, t.y });
        }

        auto it = freeX.lower_bound(v);
        if(it != freeX.end()) {
            freeX.erase(it);
            continue;
        }

        ans++;
        relax();

        Info t = cand.top(); cand.pop();

        int x = t.j;
        int s = t.x;
        int e = la[x];

        bit[x].upd(s, e, -1);
        bit[x].upd(s, s, 1e9);
        rmq[x].upd(s, -1);

        if(s != e) {
            pii d = bit[x].quer(e, e);
            pq.push({ X[x][ L[x][e] + d.first ], V[x][ -bit[x].tree[1].second ], x, la[x], -bit[x].tree[1].second, d.first });

            //cout << X[x][ L[x][e] + d.first ] << ' ' << V[x][ -bit[x].tree[1].second ] << endl;
        }

        freeV.push(V[x][s]);

        //cout << "push : " << x << ' ' << s << ' ' << e << ' ' << V[x][s] << endl;
    }

    cout << ans;
}

Compilation message

worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:165:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < V[i].size(); j++) if(!chk[j]) tmp.push_back(V[i][j]);
                        ~~^~~~~~~~~~~~~
worst_reporter2.cpp:169:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int j = 0; j < X[i].size(); j++) {
                            ~~^~~~~~~~~~~~~
worst_reporter2.cpp:182:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < V[i].size(); j++) {
                        ~~^~~~~~~~~~~~~
worst_reporter2.cpp:183:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(pos < X[i].size() && X[i][pos] < V[i][j]) pos++;
                   ~~~~^~~~~~~~~~~~~
worst_reporter2.cpp:191:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < X[i].size(); j++) {
                        ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 31 ms 31608 KB Output is correct
2 Correct 30 ms 31608 KB Output is correct
3 Correct 31 ms 31608 KB Output is correct
4 Correct 30 ms 31608 KB Output is correct
5 Correct 30 ms 31608 KB Output is correct
6 Correct 30 ms 31608 KB Output is correct
7 Correct 30 ms 31608 KB Output is correct
8 Correct 31 ms 31736 KB Output is correct
9 Correct 30 ms 31608 KB Output is correct
10 Correct 30 ms 31612 KB Output is correct
11 Correct 30 ms 31608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 31608 KB Output is correct
2 Correct 30 ms 31608 KB Output is correct
3 Correct 31 ms 31608 KB Output is correct
4 Correct 30 ms 31608 KB Output is correct
5 Correct 30 ms 31608 KB Output is correct
6 Correct 30 ms 31608 KB Output is correct
7 Correct 30 ms 31608 KB Output is correct
8 Correct 31 ms 31736 KB Output is correct
9 Correct 30 ms 31608 KB Output is correct
10 Correct 30 ms 31612 KB Output is correct
11 Correct 30 ms 31608 KB Output is correct
12 Correct 30 ms 31608 KB Output is correct
13 Correct 35 ms 31736 KB Output is correct
14 Correct 36 ms 31628 KB Output is correct
15 Correct 30 ms 31676 KB Output is correct
16 Correct 30 ms 31608 KB Output is correct
17 Correct 31 ms 31608 KB Output is correct
18 Correct 30 ms 31684 KB Output is correct
19 Correct 30 ms 31736 KB Output is correct
20 Correct 29 ms 31608 KB Output is correct
21 Correct 30 ms 31736 KB Output is correct
22 Correct 31 ms 31736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 31608 KB Output is correct
2 Correct 30 ms 31608 KB Output is correct
3 Correct 31 ms 31608 KB Output is correct
4 Correct 30 ms 31608 KB Output is correct
5 Correct 30 ms 31608 KB Output is correct
6 Correct 30 ms 31608 KB Output is correct
7 Correct 30 ms 31608 KB Output is correct
8 Correct 31 ms 31736 KB Output is correct
9 Correct 30 ms 31608 KB Output is correct
10 Correct 30 ms 31612 KB Output is correct
11 Correct 30 ms 31608 KB Output is correct
12 Correct 30 ms 31608 KB Output is correct
13 Correct 35 ms 31736 KB Output is correct
14 Correct 36 ms 31628 KB Output is correct
15 Correct 30 ms 31676 KB Output is correct
16 Correct 30 ms 31608 KB Output is correct
17 Correct 31 ms 31608 KB Output is correct
18 Correct 30 ms 31684 KB Output is correct
19 Correct 30 ms 31736 KB Output is correct
20 Correct 29 ms 31608 KB Output is correct
21 Correct 30 ms 31736 KB Output is correct
22 Correct 31 ms 31736 KB Output is correct
23 Correct 41 ms 32468 KB Output is correct
24 Correct 38 ms 32504 KB Output is correct
25 Correct 38 ms 32376 KB Output is correct
26 Correct 38 ms 32504 KB Output is correct
27 Correct 39 ms 32760 KB Output is correct
28 Correct 38 ms 32504 KB Output is correct
29 Correct 38 ms 32376 KB Output is correct
30 Correct 38 ms 32412 KB Output is correct
31 Correct 39 ms 32264 KB Output is correct
32 Correct 37 ms 32644 KB Output is correct
33 Correct 38 ms 32504 KB Output is correct
34 Correct 39 ms 32476 KB Output is correct
35 Correct 39 ms 32516 KB Output is correct
36 Correct 41 ms 32376 KB Output is correct
37 Correct 37 ms 32620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 31608 KB Output is correct
2 Correct 30 ms 31608 KB Output is correct
3 Correct 31 ms 31608 KB Output is correct
4 Correct 30 ms 31608 KB Output is correct
5 Correct 30 ms 31608 KB Output is correct
6 Correct 30 ms 31608 KB Output is correct
7 Correct 30 ms 31608 KB Output is correct
8 Correct 31 ms 31736 KB Output is correct
9 Correct 30 ms 31608 KB Output is correct
10 Correct 30 ms 31612 KB Output is correct
11 Correct 30 ms 31608 KB Output is correct
12 Correct 30 ms 31608 KB Output is correct
13 Correct 35 ms 31736 KB Output is correct
14 Correct 36 ms 31628 KB Output is correct
15 Correct 30 ms 31676 KB Output is correct
16 Correct 30 ms 31608 KB Output is correct
17 Correct 31 ms 31608 KB Output is correct
18 Correct 30 ms 31684 KB Output is correct
19 Correct 30 ms 31736 KB Output is correct
20 Correct 29 ms 31608 KB Output is correct
21 Correct 30 ms 31736 KB Output is correct
22 Correct 31 ms 31736 KB Output is correct
23 Correct 41 ms 32468 KB Output is correct
24 Correct 38 ms 32504 KB Output is correct
25 Correct 38 ms 32376 KB Output is correct
26 Correct 38 ms 32504 KB Output is correct
27 Correct 39 ms 32760 KB Output is correct
28 Correct 38 ms 32504 KB Output is correct
29 Correct 38 ms 32376 KB Output is correct
30 Correct 38 ms 32412 KB Output is correct
31 Correct 39 ms 32264 KB Output is correct
32 Correct 37 ms 32644 KB Output is correct
33 Correct 38 ms 32504 KB Output is correct
34 Correct 39 ms 32476 KB Output is correct
35 Correct 39 ms 32516 KB Output is correct
36 Correct 41 ms 32376 KB Output is correct
37 Correct 37 ms 32620 KB Output is correct
38 Correct 632 ms 64928 KB Output is correct
39 Correct 603 ms 63464 KB Output is correct
40 Correct 610 ms 64288 KB Output is correct
41 Correct 562 ms 63316 KB Output is correct
42 Correct 563 ms 71388 KB Output is correct
43 Correct 488 ms 65756 KB Output is correct
44 Correct 493 ms 62284 KB Output is correct
45 Correct 485 ms 62356 KB Output is correct
46 Correct 600 ms 56472 KB Output is correct
47 Correct 462 ms 70564 KB Output is correct
48 Correct 475 ms 65200 KB Output is correct
49 Correct 450 ms 65048 KB Output is correct
50 Correct 465 ms 65176 KB Output is correct
51 Correct 594 ms 55960 KB Output is correct
52 Correct 462 ms 69472 KB Output is correct