Submission #528053

# Submission time Handle Problem Language Result Execution time Memory
528053 2022-02-19T04:51:37 Z qwerasdfzxcl Comparing Plants (IOI20_plants) C++14
100 / 100
1103 ms 55220 KB
#include "plants.h"
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int INF = 1e9;
int n, a[200200], idxa[200200];
struct Seg2{
    pair<int, int> tree[800800];
    int lazy[800800];
    void init(int i, int l, int r){
        lazy[i] = 0;
        if (l==r) {tree[i] = {INF, l}; return;}

        int m = (l+r)>>1;
        init(i<<1, l, m); init(i<<1|1, m+1, r);
        tree[i] = min(tree[i<<1], tree[i<<1|1]);
    }
    void propagate(int i, int l, int r){
        tree[i].first += lazy[i];
        if (l!=r) lazy[i<<1] += lazy[i], lazy[i<<1|1] += lazy[i];
        lazy[i] = 0;
    }
    void update(int i, int l, int r, int s, int e, int x){
        propagate(i, l, r);
        if (r<s || e<l) return;
        if (s<=l && r<=e){
            lazy[i] += x;
            propagate(i, l, r);
            return;
        }
        int m = (l+r)>>1;
        update(i<<1, l, m, s, e, x); update(i<<1|1, m+1, r, s, e, x);
        tree[i] = min(tree[i<<1], tree[i<<1|1]);
    }
    void update2(int i, int l, int r, int s, int x){
        propagate(i, l, r);
        if (r<s || s<l) return;
        if (l==r) {tree[i].second = x; return;}
        int m = (l+r)>>1;
        update2(i<<1, l, m, s, x); update2(i<<1|1, m+1, r, s, x);
        tree[i] = min(tree[i<<1], tree[i<<1|1]);
    }
    void update(int l, int r, int x){
        if (r<=n) update(1, 1, n, l, r, x);
        else{
            update(1, 1, n, l, n, x);
            update(1, 1, n, 1, r-n, x);
        }
    }
}tree2;
struct Seg1{
    int tree[800800], lazy[800800];
    void init(int i, int l, int r, vector<int> &vec){
        lazy[i] = 0;
        if (l==r) {tree[i] = vec[l-1]; return;}

        int m = (l+r)>>1;
        init(i<<1, l, m, vec); init(i<<1|1, m+1, r, vec);
        tree[i] = max(tree[i<<1], tree[i<<1|1]);
    }
    void propagate(int i, int l, int r){
        tree[i] += lazy[i];
        if (l!=r) lazy[i<<1] += lazy[i], lazy[i<<1|1] += lazy[i];
        lazy[i] = 0;
    }
    void update(int i, int l, int r, int s, int e, int x){
        propagate(i, l, r);
        if (r<s || e<l) return;
        if (s<=l && r<=e){
            lazy[i] += x;
            propagate(i, l, r);
            return;
        }

        int m = (l+r)>>1;
        update(i<<1, l, m, s, e, x); update(i<<1|1, m+1, r, s, e, x);
        tree[i] = max(tree[i<<1], tree[i<<1|1]);
    }
    void find(int i, int l, int r, int x){
        propagate(i, l, r);
        if (tree[i]<x) return;
        if (l==r){
            assert(tree[i]==x);
            //printf("YES: %d\n", l);
            tree2.update(l, l, -INF);
            tree2.update(l+1, l+x, 1);

            update(1, 1, n, l, l, -INF);
            return;
        }

        int m = (l+r)>>1;
        find(i<<1, l, m, x); find(i<<1|1, m+1, r, x);
        tree[i] = max(tree[i<<1], tree[i<<1|1]);
    }
    void update(int s, int e, int x){
        if (s>0) update(1, 1, n, s, e, x);
        else{
            update(1, 1, n, s+n, n, x);
            update(1, 1, n, 1, e, x);
        }
    }
}tree1;

struct Seg3{
    pair<int, int> tree[400400];
    int sz;
    void init(int n, int *a){
        sz = n+1;
        for (int i=sz;i<sz*2;i++) tree[i] = {a[i-sz], i-sz};
        for (int i=sz-1;i;i--) tree[i] = min(tree[i<<1], tree[i<<1|1]);
    }
    void update(int p, int x){
        for (tree[p+=sz].first=x;p>1;p>>=1) tree[p>>1] = min(tree[p], tree[p^1]);
    }
    pair<int, int> query(int l, int r){
        pair<int, int> ret = {INF, -1};
        for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
            if (l&1) ret = min(ret, tree[l++]);
            if (r&1) ret = min(ret, tree[--r]);
        }
        return ret;
    }
    int queryc(int l, int r){
        if (l<=0){
            auto p = min(query(l+n, sz), query(1, r+1));
            return p.second;
        }
        if (r>n){
            auto p = min(query(l, sz), query(1, r-n+1));
            return p.second;
        }
        return query(l, r+1).second;
    }
}tree3;

vector<int> r;
int k, adj[200200][2][20], nxt[200200][2];

void init(int K, vector<int> R) {
    k = K, r = R;
    n = r.size();
    tree1.init(1, 1, n, r);
    tree2.init(1, 1, n);
    tree2.update2(1, 1, n, 1, INF+1);

    for (int i=1;i<=n;i++){
        tree1.find(1, 1, n, k-1);
        assert(tree2.tree[1].first==0);
        a[i] = tree2.tree[1].second;
        if (a[i]>=INF) a[i] -= INF;
        //printf("a[%d] = %d\n", i, a[i]);

        tree2.update(a[i], a[i], INF);
        tree2.update(a[i]+1, a[i]+k-1, -1);
        tree1.update(a[i]-k+1, a[i]-1, 1);
    }

    for (int i=1;i<=n;i++) idxa[a[i]] = i;

    //printf("idxa: ");
    //for (int i=1;i<=n;i++) printf("%d ", idxa[i]);
    //printf("\n");

    tree3.init(n, idxa);
    for (int i=1;i<=n;i++){
        int pos = a[i];
        int l = tree3.queryc(pos-k+1, pos-1), r = tree3.queryc(pos+1, pos+k-1);
        adj[pos][0][0] = l, adj[pos][1][0] = r;
        nxt[pos][0] = nxt[pos][1] = -1;

        if (pos<l) adj[pos][0][0] = -1, nxt[pos][0] = l;
        if (pos>r) adj[pos][1][0] = -1, nxt[pos][1] = r;

        //printf("%d: %d %d %d %d\n", pos, adj[pos][0][0], adj[pos][1][0], nxt[pos][0], nxt[pos][1]);

        tree3.update(pos, INF+1);
    }

    for (int k=1;k<20;k++){
        for (int i=1;i<=n;i++){
            for (int j=0;j<2;j++){
                if (adj[i][j][k-1]==-1) {adj[i][j][k] = -1; continue;}
                adj[i][j][k] = adj[adj[i][j][k-1]][j][k-1];
            }
        }
    }
}

int dist(int x, int y){
    if (x<y) return y - x;
    return n + y - x;
}

bool is_reachable(int x, int y){
    int s = x;
    if (y<x){
        for (int t=19;t>=0;t--) if (adj[s][1][t]!=-1) s = adj[s][1][t];
        if (dist(s, y)<k && idxa[s] < idxa[y]) return 1;

        s = nxt[s][1];
    }
    if (s!=-1){
        for (int t=19;t>=0;t--) if (adj[s][1][t]!=-1 && adj[s][1][t] < y && dist(adj[s][1][t], y) >= k) s = adj[s][1][t];
        if (s!=-1 && dist(s, y) >= k) s = adj[s][1][0];
        if (s!=-1 && idxa[s] < idxa[y]) return 1;
    }

    s = x;
    if (x<y){
        for (int t=19;t>=0;t--) if (adj[s][0][t]!=-1) s = adj[s][0][t];
        if (dist(y, s)<k && idxa[s] < idxa[y]) return 1;

        s = nxt[s][0];
    }
    if (s!=-1){
        for (int t=19;t>=0;t--) if (adj[s][0][t]!=-1 && adj[s][0][t] > y && dist(y, adj[s][0][t]) >= k) s = adj[s][0][t];
        if (s!=-1 && dist(y, s) >= k) s = adj[s][0][0];
        if (s!=-1 && idxa[s] < idxa[y]) return 1;
    }

    return 0;
}

int compare_plants(int x, int y) {
    ++x, ++y;

    if (is_reachable(x, y)) return -1;
    if (is_reachable(y, x)) return 1;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 4 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 70 ms 12436 KB Output is correct
7 Correct 131 ms 16920 KB Output is correct
8 Correct 528 ms 54964 KB Output is correct
9 Correct 566 ms 54912 KB Output is correct
10 Correct 631 ms 54980 KB Output is correct
11 Correct 604 ms 54988 KB Output is correct
12 Correct 661 ms 54940 KB Output is correct
13 Correct 692 ms 55048 KB Output is correct
14 Correct 559 ms 54980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 4 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 10 ms 9928 KB Output is correct
7 Correct 86 ms 13616 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Correct 8 ms 9932 KB Output is correct
10 Correct 87 ms 13640 KB Output is correct
11 Correct 105 ms 13504 KB Output is correct
12 Correct 82 ms 13764 KB Output is correct
13 Correct 86 ms 13592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 4 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 10 ms 9928 KB Output is correct
7 Correct 86 ms 13616 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Correct 8 ms 9932 KB Output is correct
10 Correct 87 ms 13640 KB Output is correct
11 Correct 105 ms 13504 KB Output is correct
12 Correct 82 ms 13764 KB Output is correct
13 Correct 86 ms 13592 KB Output is correct
14 Correct 153 ms 17092 KB Output is correct
15 Correct 1034 ms 55068 KB Output is correct
16 Correct 142 ms 16968 KB Output is correct
17 Correct 1025 ms 55072 KB Output is correct
18 Correct 780 ms 55084 KB Output is correct
19 Correct 681 ms 54944 KB Output is correct
20 Correct 925 ms 55004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 93 ms 12960 KB Output is correct
4 Correct 743 ms 55076 KB Output is correct
5 Correct 779 ms 54944 KB Output is correct
6 Correct 959 ms 54928 KB Output is correct
7 Correct 1072 ms 54992 KB Output is correct
8 Correct 1103 ms 55020 KB Output is correct
9 Correct 743 ms 54952 KB Output is correct
10 Correct 710 ms 55016 KB Output is correct
11 Correct 701 ms 55220 KB Output is correct
12 Correct 661 ms 54972 KB Output is correct
13 Correct 854 ms 55028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 4 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 7 ms 9788 KB Output is correct
7 Correct 29 ms 10380 KB Output is correct
8 Correct 21 ms 10372 KB Output is correct
9 Correct 23 ms 10380 KB Output is correct
10 Correct 20 ms 10400 KB Output is correct
11 Correct 22 ms 10400 KB Output is correct
12 Correct 23 ms 10408 KB Output is correct
13 Correct 20 ms 10444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 4 ms 9676 KB Output is correct
5 Correct 7 ms 9932 KB Output is correct
6 Correct 747 ms 55048 KB Output is correct
7 Correct 773 ms 55140 KB Output is correct
8 Correct 903 ms 55024 KB Output is correct
9 Correct 976 ms 54908 KB Output is correct
10 Correct 706 ms 55044 KB Output is correct
11 Correct 788 ms 55024 KB Output is correct
12 Correct 629 ms 54932 KB Output is correct
13 Correct 719 ms 54912 KB Output is correct
14 Correct 896 ms 55036 KB Output is correct
15 Correct 939 ms 55092 KB Output is correct
16 Correct 676 ms 55140 KB Output is correct
17 Correct 669 ms 54984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 4 ms 9676 KB Output is correct
4 Correct 4 ms 9676 KB Output is correct
5 Correct 4 ms 9676 KB Output is correct
6 Correct 70 ms 12436 KB Output is correct
7 Correct 131 ms 16920 KB Output is correct
8 Correct 528 ms 54964 KB Output is correct
9 Correct 566 ms 54912 KB Output is correct
10 Correct 631 ms 54980 KB Output is correct
11 Correct 604 ms 54988 KB Output is correct
12 Correct 661 ms 54940 KB Output is correct
13 Correct 692 ms 55048 KB Output is correct
14 Correct 559 ms 54980 KB Output is correct
15 Correct 4 ms 9676 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 4 ms 9676 KB Output is correct
18 Correct 4 ms 9676 KB Output is correct
19 Correct 4 ms 9676 KB Output is correct
20 Correct 10 ms 9928 KB Output is correct
21 Correct 86 ms 13616 KB Output is correct
22 Correct 6 ms 9804 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 87 ms 13640 KB Output is correct
25 Correct 105 ms 13504 KB Output is correct
26 Correct 82 ms 13764 KB Output is correct
27 Correct 86 ms 13592 KB Output is correct
28 Correct 153 ms 17092 KB Output is correct
29 Correct 1034 ms 55068 KB Output is correct
30 Correct 142 ms 16968 KB Output is correct
31 Correct 1025 ms 55072 KB Output is correct
32 Correct 780 ms 55084 KB Output is correct
33 Correct 681 ms 54944 KB Output is correct
34 Correct 925 ms 55004 KB Output is correct
35 Correct 4 ms 9676 KB Output is correct
36 Correct 4 ms 9676 KB Output is correct
37 Correct 93 ms 12960 KB Output is correct
38 Correct 743 ms 55076 KB Output is correct
39 Correct 779 ms 54944 KB Output is correct
40 Correct 959 ms 54928 KB Output is correct
41 Correct 1072 ms 54992 KB Output is correct
42 Correct 1103 ms 55020 KB Output is correct
43 Correct 743 ms 54952 KB Output is correct
44 Correct 710 ms 55016 KB Output is correct
45 Correct 701 ms 55220 KB Output is correct
46 Correct 661 ms 54972 KB Output is correct
47 Correct 854 ms 55028 KB Output is correct
48 Correct 5 ms 9676 KB Output is correct
49 Correct 4 ms 9676 KB Output is correct
50 Correct 4 ms 9676 KB Output is correct
51 Correct 4 ms 9676 KB Output is correct
52 Correct 4 ms 9676 KB Output is correct
53 Correct 7 ms 9788 KB Output is correct
54 Correct 29 ms 10380 KB Output is correct
55 Correct 21 ms 10372 KB Output is correct
56 Correct 23 ms 10380 KB Output is correct
57 Correct 20 ms 10400 KB Output is correct
58 Correct 22 ms 10400 KB Output is correct
59 Correct 23 ms 10408 KB Output is correct
60 Correct 20 ms 10444 KB Output is correct
61 Correct 92 ms 12896 KB Output is correct
62 Correct 152 ms 16884 KB Output is correct
63 Correct 637 ms 54976 KB Output is correct
64 Correct 733 ms 54976 KB Output is correct
65 Correct 943 ms 55020 KB Output is correct
66 Correct 1006 ms 54968 KB Output is correct
67 Correct 968 ms 54956 KB Output is correct
68 Correct 778 ms 55044 KB Output is correct
69 Correct 892 ms 54900 KB Output is correct
70 Correct 733 ms 55140 KB Output is correct
71 Correct 859 ms 55136 KB Output is correct
72 Correct 989 ms 54944 KB Output is correct
73 Correct 1020 ms 55024 KB Output is correct
74 Correct 688 ms 55028 KB Output is correct
75 Correct 723 ms 55072 KB Output is correct