답안 #528052

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
528052 2022-02-19T04:48:46 Z qwerasdfzxcl 식물 비교 (IOI20_plants) C++14
100 / 100
1016 ms 58868 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 && dist(s, y)<k && 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 && dist(y, s)<k && 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;
}
# 결과 실행 시간 메모리 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 69 ms 12460 KB Output is correct
7 Correct 122 ms 16964 KB Output is correct
8 Correct 551 ms 55048 KB Output is correct
9 Correct 566 ms 54952 KB Output is correct
10 Correct 606 ms 55028 KB Output is correct
11 Correct 651 ms 55116 KB Output is correct
12 Correct 634 ms 55012 KB Output is correct
13 Correct 684 ms 55016 KB Output is correct
14 Correct 592 ms 55020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 5 ms 9696 KB Output is correct
4 Correct 4 ms 9720 KB Output is correct
5 Correct 5 ms 9676 KB Output is correct
6 Correct 8 ms 9932 KB Output is correct
7 Correct 87 ms 13592 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Correct 12 ms 9908 KB Output is correct
10 Correct 95 ms 13624 KB Output is correct
11 Correct 109 ms 13596 KB Output is correct
12 Correct 89 ms 13764 KB Output is correct
13 Correct 82 ms 13588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 5 ms 9696 KB Output is correct
4 Correct 4 ms 9720 KB Output is correct
5 Correct 5 ms 9676 KB Output is correct
6 Correct 8 ms 9932 KB Output is correct
7 Correct 87 ms 13592 KB Output is correct
8 Correct 6 ms 9804 KB Output is correct
9 Correct 12 ms 9908 KB Output is correct
10 Correct 95 ms 13624 KB Output is correct
11 Correct 109 ms 13596 KB Output is correct
12 Correct 89 ms 13764 KB Output is correct
13 Correct 82 ms 13588 KB Output is correct
14 Correct 141 ms 16952 KB Output is correct
15 Correct 995 ms 55096 KB Output is correct
16 Correct 146 ms 16992 KB Output is correct
17 Correct 984 ms 55020 KB Output is correct
18 Correct 818 ms 54996 KB Output is correct
19 Correct 702 ms 58752 KB Output is correct
20 Correct 995 ms 58868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9676 KB Output is correct
2 Correct 4 ms 9676 KB Output is correct
3 Correct 96 ms 12852 KB Output is correct
4 Correct 723 ms 55020 KB Output is correct
5 Correct 788 ms 58204 KB Output is correct
6 Correct 954 ms 58252 KB Output is correct
7 Correct 1016 ms 58400 KB Output is correct
8 Correct 970 ms 58728 KB Output is correct
9 Correct 738 ms 57928 KB Output is correct
10 Correct 713 ms 58020 KB Output is correct
11 Correct 714 ms 57864 KB Output is correct
12 Correct 635 ms 58056 KB Output is correct
13 Correct 828 ms 58032 KB Output is correct
# 결과 실행 시간 메모리 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 5 ms 9676 KB Output is correct
6 Correct 7 ms 9804 KB Output is correct
7 Correct 23 ms 10324 KB Output is correct
8 Correct 20 ms 10368 KB Output is correct
9 Correct 24 ms 10376 KB Output is correct
10 Correct 28 ms 10444 KB Output is correct
11 Correct 24 ms 10384 KB Output is correct
12 Correct 22 ms 10384 KB Output is correct
13 Correct 19 ms 10436 KB Output is correct
# 결과 실행 시간 메모리 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 713 ms 55112 KB Output is correct
7 Correct 763 ms 54928 KB Output is correct
8 Correct 928 ms 55036 KB Output is correct
9 Correct 1002 ms 54964 KB Output is correct
10 Correct 652 ms 55116 KB Output is correct
11 Correct 860 ms 57760 KB Output is correct
12 Correct 622 ms 57124 KB Output is correct
13 Correct 732 ms 57348 KB Output is correct
14 Correct 845 ms 57564 KB Output is correct
15 Correct 920 ms 57708 KB Output is correct
16 Correct 714 ms 57100 KB Output is correct
17 Correct 743 ms 57192 KB Output is correct
# 결과 실행 시간 메모리 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 69 ms 12460 KB Output is correct
7 Correct 122 ms 16964 KB Output is correct
8 Correct 551 ms 55048 KB Output is correct
9 Correct 566 ms 54952 KB Output is correct
10 Correct 606 ms 55028 KB Output is correct
11 Correct 651 ms 55116 KB Output is correct
12 Correct 634 ms 55012 KB Output is correct
13 Correct 684 ms 55016 KB Output is correct
14 Correct 592 ms 55020 KB Output is correct
15 Correct 4 ms 9676 KB Output is correct
16 Correct 4 ms 9676 KB Output is correct
17 Correct 5 ms 9696 KB Output is correct
18 Correct 4 ms 9720 KB Output is correct
19 Correct 5 ms 9676 KB Output is correct
20 Correct 8 ms 9932 KB Output is correct
21 Correct 87 ms 13592 KB Output is correct
22 Correct 6 ms 9804 KB Output is correct
23 Correct 12 ms 9908 KB Output is correct
24 Correct 95 ms 13624 KB Output is correct
25 Correct 109 ms 13596 KB Output is correct
26 Correct 89 ms 13764 KB Output is correct
27 Correct 82 ms 13588 KB Output is correct
28 Correct 141 ms 16952 KB Output is correct
29 Correct 995 ms 55096 KB Output is correct
30 Correct 146 ms 16992 KB Output is correct
31 Correct 984 ms 55020 KB Output is correct
32 Correct 818 ms 54996 KB Output is correct
33 Correct 702 ms 58752 KB Output is correct
34 Correct 995 ms 58868 KB Output is correct
35 Correct 4 ms 9676 KB Output is correct
36 Correct 4 ms 9676 KB Output is correct
37 Correct 96 ms 12852 KB Output is correct
38 Correct 723 ms 55020 KB Output is correct
39 Correct 788 ms 58204 KB Output is correct
40 Correct 954 ms 58252 KB Output is correct
41 Correct 1016 ms 58400 KB Output is correct
42 Correct 970 ms 58728 KB Output is correct
43 Correct 738 ms 57928 KB Output is correct
44 Correct 713 ms 58020 KB Output is correct
45 Correct 714 ms 57864 KB Output is correct
46 Correct 635 ms 58056 KB Output is correct
47 Correct 828 ms 58032 KB Output is correct
48 Correct 4 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 5 ms 9676 KB Output is correct
53 Correct 7 ms 9804 KB Output is correct
54 Correct 23 ms 10324 KB Output is correct
55 Correct 20 ms 10368 KB Output is correct
56 Correct 24 ms 10376 KB Output is correct
57 Correct 28 ms 10444 KB Output is correct
58 Correct 24 ms 10384 KB Output is correct
59 Correct 22 ms 10384 KB Output is correct
60 Correct 19 ms 10436 KB Output is correct
61 Correct 88 ms 14632 KB Output is correct
62 Correct 132 ms 19200 KB Output is correct
63 Correct 643 ms 57864 KB Output is correct
64 Correct 746 ms 58020 KB Output is correct
65 Correct 932 ms 58228 KB Output is correct
66 Correct 1014 ms 58560 KB Output is correct
67 Correct 998 ms 58784 KB Output is correct
68 Correct 714 ms 58020 KB Output is correct
69 Correct 865 ms 58612 KB Output is correct
70 Correct 721 ms 57944 KB Output is correct
71 Correct 895 ms 58096 KB Output is correct
72 Correct 995 ms 58472 KB Output is correct
73 Correct 980 ms 58704 KB Output is correct
74 Correct 696 ms 58052 KB Output is correct
75 Correct 705 ms 58148 KB Output is correct