Submission #790822

# Submission time Handle Problem Language Result Execution time Memory
790822 2023-07-23T08:26:23 Z resting Comparing Plants (IOI20_plants) C++17
100 / 100
1201 ms 115940 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;

#define time fuck

const int mx = 2e5 + 5;

int n, k;
struct segtree {
    int l, r, lz = 0, mn = 0;
    segtree* lc = 0, * rc = 0;
    segtree* getmem();
    segtree() : segtree(-1, -1) {};
    segtree(int l, int r) : l(l), r(r) {
        if (l == r)return;
        int m = (l + r) / 2;
        lc = getmem(); *lc = segtree(l, m);
        rc = getmem(); *rc = segtree(m + 1, r);
    }
    void apply(int v) {
        mn += v; lz += v;
    }
    void push() {
        if (l == r) return;
        lc->apply(lz);rc->apply(lz);
        lz = 0;
    }
    void pull() {
        if (l == r) return;
        mn = min(lc->mn, rc->mn);
    }
    void upd(int ql, int qr, int qv) {
        if (qr < 0) { ql += n; qr += n; }
        if (ql < 0) {
            upd(0, qr, qv);
            upd((n)+ql, (n - 1), qv);
            return;
        }
        if (ql >= n) { ql -= n; qr -= n; }
        if (qr >= n) {
            upd(ql, (n - 1), qv);
            upd(0, qr - (n), qv);
            return;
        }
        if (l > qr || r < ql) return;
        if (ql <= l && r <= qr) { apply(qv); return; }
        push();
        lc->upd(ql, qr, qv);
        rc->upd(ql, qr, qv);
        pull();
    }
    int extract_min() {
        if (l == r) return l;
        push();
        if (lc->mn == mn) return lc->extract_min();
        return rc->extract_min();
    }
}mem[mx * 8]; int memsz = 0;
segtree* segtree::getmem() { return &mem[memsz++]; }

const int inf = 1e9 + 7;
namespace rmq {
    struct segtree {
        int l, r;
        segtree* lc = 0, * rc = 0;
        pair<int, int> v = { -1, -1 };
        segtree* getmem();
        segtree() : segtree(-1, -1) {};
        segtree(int l, int r) : l(l), r(r) {
            if (l == r)return;
            int m = (l + r) / 2;
            lc = getmem(); *lc = segtree(l, m);
            rc = getmem(); *rc = segtree(m + 1, r);
        }
        void upd(int qi, int qv) {
            if (l == r) { v = { qv, qi };return; }
            if (qi <= lc->r) lc->upd(qi, qv);
            else rc->upd(qi, qv);
            v = max(lc->v, rc->v);
        }
        pair<int, int> q(int ql, int qr) {
            if (qr < 0) { ql += n; qr += n; }
            if (ql < 0) {
                return max(q(0, qr), q((n)+ql, (n - 1)));
            }
            if (ql >= n) { ql -= n; qr -= n; }
            if (qr >= n) {
                return max(q(ql, (n - 1)), q(0, qr - (n)));
            }
            if (l > qr || r < ql)return { -1, -1 };
            if (ql <= l && r <= qr) return v;
            return max(lc->q(ql, qr), rc->q(ql, qr));
        }
    }mem[mx * 4]; int memsz = 0;
    segtree* segtree::getmem() { return &mem[memsz++]; }
}


vector<int> time, rtime;
int parl[mx][20], parr[mx][20];

int cheat[500][500]{};
void init(int kk, std::vector<int> r) {
    n = r.size();k = kk;k--;
    time = rtime = vector<int>(n, 0);
    segtree parta(0, n - 1), partb(0, n - 1); partb.upd(0, n - 1, 1);
    for (int i = 0; i < n; i++) parta.upd(i, i, r[i]);
    for (int i = 0; i < n; i++) {
        while (parta.mn == 0) {
            int t = parta.extract_min();
            parta.upd(t, t, mx);
            partb.upd(t, t, -1);
            partb.upd(t + 1, t + k, 1);
        }
        int t = partb.extract_min();
        time[t] = i;
        rtime[i] = t;
        partb.upd(t, t, mx);
        partb.upd(t + 1, t + k, -1);
        parta.upd(t - k, t, -1);
    }
    rmq::segtree tim(0, n - 1);
    for (int i = 0; i < n; i++) {
        int t = tim.q(rtime[i] - k, rtime[i]).second;
        if (t == -1) parl[rtime[i]][0] = rtime[i];
        else parl[rtime[i]][0] = t;
        //   cout << "#" << t << endl;
        t = tim.q(rtime[i], rtime[i] + k).second;
        // cout << "#" << t << endl;
        if (t == -1) parr[rtime[i]][0] = rtime[i];
        else parr[rtime[i]][0] = t;
        tim.upd(rtime[i], i);
    }
    for (int i = 1; i < 20; i++) {
        for (int j = 0; j < n; j++) {
            if (parl[j][0] > j) parl[j][i] = parl[j][i - 1];
            else if (parl[parl[j][i - 1]][0] > parl[j][i - 1]) parl[j][i] = parl[j][i - 1];
            else parl[j][i] = parl[parl[j][i - 1]][i - 1];
        }
    }
    for (int i = 1; i < 20; i++) {
        for (int j = 0; j < n; j++) {
            if (parr[j][0] < j) parr[j][i] = parr[j][i - 1];
            else if (parr[parr[j][i - 1]][0] < parr[j][i - 1]) parr[j][i] = parr[j][i - 1];
            else parr[j][i] = parr[parr[j][i - 1]][i - 1];

        }
    }

    if (n <= 300) {
        for (int i = 0; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                int j2 = (i + j) % n;
                if (time[j2] < time[i]) cheat[i][j2] = 1;
                else cheat[j2][i] = 1;
            }
        }
        for (int i = 0; i < n; i++) {
            for (int x = 0; x < n; x++) {
                for (int y = 0; y < n; y++) {
                    cheat[x][y] |= cheat[x][i] && cheat[i][y];
                }
            }
        }
    }
    for (int i = 0; i < n; i++)
        for (int j = 1; j < 20; j++)
            assert(parr[i][j] >= parr[i][j - 1] && (time[parr[i][j]] <= time[parr[i][j - 1]]));
    for (int i = 0; i < n; i++)
        for (int j = 1; j < 20; j++)
            assert(parl[i][j] <= parl[i][j - 1] && (time[parl[i][j]] <= time[parl[i][j - 1]]));
    return;
}

int compare_plants(int x, int y) {
    // if (n <= 300) {
    //     if (cheat[x][y]) return -1;
    //     if (cheat[y][x])return 1;
    //     return 0;
    // }
    //compare zero
    if (x + k >= y || y + k - n >= x) { return time[x] < time[y] ? 1 : -1; }
    // if (n <= 300) {
    //     //brute force


    // //compare one
    //     int t = x;
    //     for (int i = n; i >= 0; i--) {
    //         if (parr[t][0] + k < y) t = parr[t][0];
    //     }
    //     t = parr[t][0];
    //     if (t + k >= y && time[t] > time[y]) return -1;

    //     //compare two
    //     t = x;;
    //     while (parl[t][0] != t && parl[t][0] < t) t = parl[t][0];
    //     if (y + k - n >= t && time[t] > time[y]) return -1;
    //     if (t != parl[t][0]) {
    //         t = parl[t][0];
    //         for (int i = n; i >= 0; i--) {
    //             if (y + k < parl[t][0]) t = parl[t][0];
    //         }
    //         t = parl[t][0];
    //         if (y + k >= t && time[t] > time[y])return -1;
    //     }

    //     //compare three
    //     t = y;
    //     for (int i = n; i >= 0; i--) {
    //         if (x + k < parl[t][0]) t = parl[t][0];
    //     }
    //     t = parl[t][0];
    //     if (x + k >= t && time[t] > time[x]) return 1;

    //     //compare four
    //     t = y;
    //     while (parr[t][0] != t && parr[t][0] > t) t = parr[t][0];
    //     if (t + k - n >= x && time[t] > time[x]) return 1;
    //     if (t != parr[t][0]) {
    //         t = parr[t][0];
    //         for (int i = n; i >= 0; i--) {
    //             if (parr[t][0] + k < x) t = parr[t][0];
    //         }
    //         t = parr[t][0];
    //         if (t + k >= x && time[t] > time[x]) return 1;
    //     }
    // }

    //compare one
    int t = x;
    for (int i = 19; i >= 0; i--) {
        //assert(parr[t][i] >= t);
        if (parr[t][i] + k < y) t = parr[t][i];
    }
    t = parr[t][0];
    if (t + k >= y && time[t] >= time[y]) return -1;

    //compare two
    t = x;
    for (int i = 19; i >= 0; i--) if (parl[t][i] < t && time[parl[t][i]] >= time[y]) t = parl[t][i];
    if (y + k - n >= t && time[t] >= time[y]) return -1;
    if (t != parl[t][0]) {
        t = parl[t][0];
        for (int i = 19; i >= 0; i--) {
            if (y + k < parl[t][i]) t = parl[t][i];
        }
        t = parl[t][0];
        if (y + k >= t && time[t] >= time[y])return -1;
    }

    //compare three
    t = y;
    for (int i = 19; i >= 0; i--) {
        // assert(parl[t][i] <= t);
        if (x + k < parl[t][i]) t = parl[t][i];
    }
    t = parl[t][0];
    if (x + k >= t && time[t] >= time[x]) return 1;

    //compare four
    t = y;
    for (int i = 19; i >= 0; i--) {
        if (parr[t][i] > t && time[parr[t][i]] >= time[x]) t = parr[t][i];
    }
    if (t + k - n >= x && time[t] >= time[x]) return 1;
    if (t != parr[t][0]) {
        t = parr[t][0];
        if (t + k >= x && time[t] >= time[x]) return 1;
        for (int i = 19; i >= 0; i--) {
            if (parr[t][i] + k < x) t = parr[t][i];
        }
        t = parr[t][0];
        if (t + k >= x && time[t] >= time[x]) return 1;
    }

    return 0;
}

// int main() {
//     init(3, { 0, 1, 1, 2 });
//     cout << compare_plants(0, 2) << endl;
//     cout << compare_plants(1, 2) << endl;
// }
# Verdict Execution time Memory Grader output
1 Correct 27 ms 75348 KB Output is correct
2 Correct 27 ms 75416 KB Output is correct
3 Correct 30 ms 75344 KB Output is correct
4 Correct 27 ms 75476 KB Output is correct
5 Correct 27 ms 75476 KB Output is correct
6 Correct 95 ms 78252 KB Output is correct
7 Correct 157 ms 81728 KB Output is correct
8 Correct 613 ms 112252 KB Output is correct
9 Correct 630 ms 112176 KB Output is correct
10 Correct 639 ms 112160 KB Output is correct
11 Correct 645 ms 112256 KB Output is correct
12 Correct 672 ms 112180 KB Output is correct
13 Correct 700 ms 112220 KB Output is correct
14 Correct 580 ms 112264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 75348 KB Output is correct
2 Correct 27 ms 75432 KB Output is correct
3 Correct 27 ms 75476 KB Output is correct
4 Correct 28 ms 75364 KB Output is correct
5 Correct 35 ms 75668 KB Output is correct
6 Correct 31 ms 75676 KB Output is correct
7 Correct 79 ms 79104 KB Output is correct
8 Correct 30 ms 75688 KB Output is correct
9 Correct 30 ms 75680 KB Output is correct
10 Correct 79 ms 79128 KB Output is correct
11 Correct 77 ms 79036 KB Output is correct
12 Correct 77 ms 79268 KB Output is correct
13 Correct 81 ms 79076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 75348 KB Output is correct
2 Correct 27 ms 75432 KB Output is correct
3 Correct 27 ms 75476 KB Output is correct
4 Correct 28 ms 75364 KB Output is correct
5 Correct 35 ms 75668 KB Output is correct
6 Correct 31 ms 75676 KB Output is correct
7 Correct 79 ms 79104 KB Output is correct
8 Correct 30 ms 75688 KB Output is correct
9 Correct 30 ms 75680 KB Output is correct
10 Correct 79 ms 79128 KB Output is correct
11 Correct 77 ms 79036 KB Output is correct
12 Correct 77 ms 79268 KB Output is correct
13 Correct 81 ms 79076 KB Output is correct
14 Correct 131 ms 81736 KB Output is correct
15 Correct 1145 ms 112252 KB Output is correct
16 Correct 130 ms 81696 KB Output is correct
17 Correct 1189 ms 112256 KB Output is correct
18 Correct 647 ms 112256 KB Output is correct
19 Correct 686 ms 112180 KB Output is correct
20 Correct 980 ms 112308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 75340 KB Output is correct
2 Correct 27 ms 75404 KB Output is correct
3 Correct 126 ms 78548 KB Output is correct
4 Correct 772 ms 112260 KB Output is correct
5 Correct 767 ms 112204 KB Output is correct
6 Correct 1065 ms 112256 KB Output is correct
7 Correct 1201 ms 112272 KB Output is correct
8 Correct 1120 ms 112328 KB Output is correct
9 Correct 745 ms 112256 KB Output is correct
10 Correct 724 ms 112176 KB Output is correct
11 Correct 711 ms 112304 KB Output is correct
12 Correct 713 ms 112220 KB Output is correct
13 Correct 753 ms 112352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 75444 KB Output is correct
2 Correct 27 ms 75424 KB Output is correct
3 Correct 28 ms 75372 KB Output is correct
4 Correct 28 ms 75364 KB Output is correct
5 Correct 28 ms 75540 KB Output is correct
6 Correct 32 ms 75732 KB Output is correct
7 Correct 69 ms 76620 KB Output is correct
8 Correct 63 ms 76632 KB Output is correct
9 Correct 69 ms 76624 KB Output is correct
10 Correct 67 ms 76624 KB Output is correct
11 Correct 68 ms 76628 KB Output is correct
12 Correct 67 ms 76636 KB Output is correct
13 Correct 63 ms 76632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 75348 KB Output is correct
2 Correct 27 ms 75356 KB Output is correct
3 Correct 27 ms 75368 KB Output is correct
4 Correct 27 ms 75420 KB Output is correct
5 Correct 29 ms 75532 KB Output is correct
6 Correct 770 ms 114416 KB Output is correct
7 Correct 918 ms 114764 KB Output is correct
8 Correct 1143 ms 114796 KB Output is correct
9 Correct 1137 ms 114984 KB Output is correct
10 Correct 730 ms 114328 KB Output is correct
11 Correct 808 ms 114952 KB Output is correct
12 Correct 648 ms 114228 KB Output is correct
13 Correct 864 ms 114496 KB Output is correct
14 Correct 957 ms 114588 KB Output is correct
15 Correct 1086 ms 114872 KB Output is correct
16 Correct 698 ms 114340 KB Output is correct
17 Correct 792 ms 114472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 75348 KB Output is correct
2 Correct 27 ms 75416 KB Output is correct
3 Correct 30 ms 75344 KB Output is correct
4 Correct 27 ms 75476 KB Output is correct
5 Correct 27 ms 75476 KB Output is correct
6 Correct 95 ms 78252 KB Output is correct
7 Correct 157 ms 81728 KB Output is correct
8 Correct 613 ms 112252 KB Output is correct
9 Correct 630 ms 112176 KB Output is correct
10 Correct 639 ms 112160 KB Output is correct
11 Correct 645 ms 112256 KB Output is correct
12 Correct 672 ms 112180 KB Output is correct
13 Correct 700 ms 112220 KB Output is correct
14 Correct 580 ms 112264 KB Output is correct
15 Correct 27 ms 75348 KB Output is correct
16 Correct 27 ms 75432 KB Output is correct
17 Correct 27 ms 75476 KB Output is correct
18 Correct 28 ms 75364 KB Output is correct
19 Correct 35 ms 75668 KB Output is correct
20 Correct 31 ms 75676 KB Output is correct
21 Correct 79 ms 79104 KB Output is correct
22 Correct 30 ms 75688 KB Output is correct
23 Correct 30 ms 75680 KB Output is correct
24 Correct 79 ms 79128 KB Output is correct
25 Correct 77 ms 79036 KB Output is correct
26 Correct 77 ms 79268 KB Output is correct
27 Correct 81 ms 79076 KB Output is correct
28 Correct 131 ms 81736 KB Output is correct
29 Correct 1145 ms 112252 KB Output is correct
30 Correct 130 ms 81696 KB Output is correct
31 Correct 1189 ms 112256 KB Output is correct
32 Correct 647 ms 112256 KB Output is correct
33 Correct 686 ms 112180 KB Output is correct
34 Correct 980 ms 112308 KB Output is correct
35 Correct 27 ms 75340 KB Output is correct
36 Correct 27 ms 75404 KB Output is correct
37 Correct 126 ms 78548 KB Output is correct
38 Correct 772 ms 112260 KB Output is correct
39 Correct 767 ms 112204 KB Output is correct
40 Correct 1065 ms 112256 KB Output is correct
41 Correct 1201 ms 112272 KB Output is correct
42 Correct 1120 ms 112328 KB Output is correct
43 Correct 745 ms 112256 KB Output is correct
44 Correct 724 ms 112176 KB Output is correct
45 Correct 711 ms 112304 KB Output is correct
46 Correct 713 ms 112220 KB Output is correct
47 Correct 753 ms 112352 KB Output is correct
48 Correct 27 ms 75444 KB Output is correct
49 Correct 27 ms 75424 KB Output is correct
50 Correct 28 ms 75372 KB Output is correct
51 Correct 28 ms 75364 KB Output is correct
52 Correct 28 ms 75540 KB Output is correct
53 Correct 32 ms 75732 KB Output is correct
54 Correct 69 ms 76620 KB Output is correct
55 Correct 63 ms 76632 KB Output is correct
56 Correct 69 ms 76624 KB Output is correct
57 Correct 67 ms 76624 KB Output is correct
58 Correct 68 ms 76628 KB Output is correct
59 Correct 67 ms 76636 KB Output is correct
60 Correct 63 ms 76632 KB Output is correct
61 Correct 144 ms 78520 KB Output is correct
62 Correct 184 ms 83828 KB Output is correct
63 Correct 739 ms 115056 KB Output is correct
64 Correct 797 ms 115352 KB Output is correct
65 Correct 1083 ms 115592 KB Output is correct
66 Correct 1151 ms 115772 KB Output is correct
67 Correct 1123 ms 115940 KB Output is correct
68 Correct 843 ms 115284 KB Output is correct
69 Correct 871 ms 115896 KB Output is correct
70 Correct 819 ms 115096 KB Output is correct
71 Correct 938 ms 115340 KB Output is correct
72 Correct 1051 ms 115544 KB Output is correct
73 Correct 1187 ms 115740 KB Output is correct
74 Correct 738 ms 115268 KB Output is correct
75 Correct 755 ms 115348 KB Output is correct