답안 #361557

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
361557 2021-01-30T14:14:24 Z Sorting 식물 비교 (IOI20_plants) C++17
100 / 100
1535 ms 51612 KB
#include "plants.h"
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 3;

int r_arr[N], p[N], k, n;

struct Segment_Tree{
    struct Node{
        int mn;
        array<int, 2> prev_zero;
    
        Node(){}
        Node(int mn, int i): mn(mn), prev_zero({i, 0}){}

        friend Node merge(const Node &l, const Node &r){
            Node ret;
            ret.mn = min(l.mn, r.mn);
            if(r.prev_zero[1] >= k) ret.prev_zero = r.prev_zero;
            else if(l.prev_zero[1]) ret.prev_zero = l.prev_zero;
            else ret.prev_zero = r.prev_zero;
            return ret;
        }
    };
    Node nd[4 * N];
    int lp[4 * N];

    void init(int i = 0, int l = 0, int r = n - 1){
        if(l == r){
            nd[i] = Node(r_arr[l], l);
            return;
        }

        int mid = (l + r) >> 1;
        init(2 * i + 1, l, mid);
        init(2 * i + 2, mid + 1, r);
        nd[i] = merge(nd[2 * i + 1], nd[2 * i + 2]);
    }

    void push(int i, int l, int r){
        nd[i].mn += lp[i];
        if(l != r){
            lp[2 * i + 1] += lp[i];
            lp[2 * i + 2] += lp[i];
        }
        lp[i] = 0;
    }

    int query(int i = 0, int l = 0, int r = n - 1){
        push(i, l, r);
        return nd[i].prev_zero[0];
    }

    int get_next_zero(int s, int i = 0, int l = 0, int r = n - 1){
        if(l > r) return n;

        push(i, l, r);
        if(r < s || nd[i].mn) return n;
        if(l == r) return l;

        int mid = (l + r) >> 1;
        int l_val = get_next_zero(s, 2 * i + 1, l, mid);
        if(l_val != n) return l_val;
        return get_next_zero(s, 2 * i + 2, mid + 1, r);
    }

    int get_prev_zero(int s, int i = 0, int l = 0, int r = n - 1){
        if(l > r) return -1;

        push(i, l, r);
        if(l > s || nd[i].mn) return -1;
        if(l == r) return l;

        int mid = (l + r) >> 1;
        int r_val = get_prev_zero(s, 2 * i + 2, mid + 1, r);
        if(r_val != -1) return r_val;
        return get_prev_zero(s, 2 * i + 1, l, mid);
    }

    void update_prev_zero(int s, int i = 0, int l = 0, int r = n - 1){
        if(l > r) return;

        push(i, l, r);
        if(l > s || r < s) return;
        if(l == r){
            nd[i].prev_zero = {s, s - get_prev_zero(s - 1, 0, 0, n - 1)};
            return;
        }

        int mid = (l + r) >> 1;
        update_prev_zero(s, 2 * i + 1, l, mid);
        update_prev_zero(s, 2 * i + 2, mid + 1, r);

        nd[i] = merge(nd[2 * i + 1], nd[2 * i + 2]);
    }

    void update_zeroes(int l2, int r2, int i = 0, int l = 0, int r = n - 1){
        if(l > r) return;

        push(i, l, r);
        if(nd[i].mn) return;
        if(r < l2 || r2 < l) return;
        if(l == r){
            update_prev_zero(l, i, l, r);
            return;
        }

        int mid = (l + r) >> 1;
        update_zeroes(l2, r2, 2 * i + 1, l, mid);
        update_zeroes(l2, r2, 2 * i + 2, mid + 1, r);

        nd[i] = merge(nd[2 * i + 1], nd[2 * i + 2]);
    }

    void change_value(int s, int val, int i = 0, int l = 0, int r = n - 1){
        if(l > r) return;

        push(i, l, r);
        if(r < s || s < l) return;
        if(l == r){
            nd[i] = Node(val, s);
            return;
        }

        int mid = (l + r) >> 1;
        change_value(s, val, 2 * i + 1, l, mid);
        change_value(s, val, 2 * i + 2, mid + 1, r);

        nd[i] = merge(nd[2 * i + 1], nd[2 * i + 2]);
    }

    void update(int l2, int r2, int val, int i = 0, int l = 0, int r = n - 1){
        if(l > r) return;

        push(i, l, r);
        if(r < l2 || r2 < l) return;
        if(l2 <= l && r <= r2){
            lp[i]+=val;
            push(i, l, r);
            return;
        }

        int mid = (l + r) >> 1;
        update(l2, r2, val, 2 * i + 1, l, mid);
        update(l2, r2, val, 2 * i + 2, mid + 1, r);

        nd[i] = merge(nd[2 * i + 1], nd[2 * i + 2]);
    }
} st;

struct Fenwick{
    int a[N];

    Fenwick(){}

    void clear(){
        fill(a, a + N, 0);
    }

    void update(int i, int v){
        for(++i; i < N; i+=i&-i)
            a[i] += v;
    }

    int query(int i){
        int ans = 0;
        for(++i; i >= 1; i-=i&-i)
            ans += a[i];
        return ans;
    }

    int query(int l, int r){
        return query(r) - query(l - 1);
    }
} f;

const int LOGN = 18;
int nxt[2][LOGN][N];

void init(int _k, vector<int> _r) {
	k = _k;
    n = _r.size();
    for(int i = 0; i < _r.size(); ++i)
        r_arr[i] = _r[i];

    st.init();
    st.update_zeroes(0, n - 1);
    for(int i = n - 1; i >= 0; --i){
        int idx = st.query();
        p[idx] = i;
        //cout << idx << " idx" << endl;
        
        st.change_value(idx, n);
        if(idx){
            st.update(max(idx - (k - 1), 0), idx - 1, -1);
            st.update_zeroes(max(idx - (k - 1), 0), idx - 1);
        }
        if(idx < k - 1){
            st.update(n - ((k - 1) - idx), n - 1, -1);
            st.update_zeroes(n - ((k - 1) - idx), n - 1);
        }

        int nxt = st.get_next_zero(idx + 1);
        if(nxt != n) st.update_prev_zero(nxt);
    }

    set<array<int, 2>> s;
    for(int i = n - 1; i > n - k; --i)
        s.insert({p[i], i});
    for(int i = 0; i < n; ++i){
        auto it = s.lower_bound({p[i], 0});
        if(it == s.end()) nxt[0][0][i] = -1;
        else nxt[0][0][i] = (*it)[1];

        int prev_i = (i - (k - 1) + n) % n;
        s.erase({p[prev_i], prev_i});
        s.insert({p[i], i}); 
    }

    s.clear();
    for(int i = 0; i < k - 1; ++i)
        s.insert({p[i], i});
    for(int i = n - 1; i >= 0; --i){
        auto it = s.lower_bound({p[i], 0});
        if(it == s.end()) nxt[1][0][i] = -1;
        else nxt[1][0][i] = (*it)[1];
    
        int prev_i = (i + (k - 1)) % n;
        s.erase({p[prev_i], prev_i});
        s.insert({p[i], i});
    }

    for(int i = 1; i < LOGN; ++i)
        for(int j = 0; j < n; ++j){
            int &ans = nxt[0][i][j];
            int node_2 = nxt[0][i - 1][j];
            if(node_2 == -1){
                ans = -1;
                continue;
            }
            int node_3 = nxt[0][i - 1][node_2];
            if(node_3 == -1){
                ans = node_3;
                continue;
            }

            if(j < node_2){
                if(node_3 > node_2 || node_3 <= j) ans = -1;
                else ans = node_3;
            }
            else{
                if(node_3 > node_2 && node_3 <= j) ans = -1;
                else ans = node_3;
            }
        }

    for(int i = 1; i < LOGN; ++i)
        for(int j = 0; j < n; ++j){
            int &ans = nxt[1][i][j];
            int node_2 = nxt[1][i - 1][j];
            if(node_2 == -1){
                ans = -1;
                continue;
            }
            int node_3 = nxt[1][i - 1][node_2];
            if(node_3 == -1){
                ans = node_3;
                continue;
            }

            if(j < node_2){
                if(node_3 >= j && node_3 < node_2) ans = -1;
                else ans = node_3;
            }
            else{
                if(node_3 >= j || node_3 < node_2) ans = -1;
                else ans = node_3;
            }
        }
   //cout << "done preprocessing" << endl;
}

int get_dist(int x, int y){
    return min(abs(x - y), min(abs(x + n - y), abs(y + n - x)));
}

bool connected(int x, int y){
    if(get_dist(x, y) <= k - 1) return true;

    if(p[x] > p[y]) swap(x, y);

    int target = (y + (k - 1)) % n;
    int curr_x = x;
    for(int i = LOGN - 1; i >= 0; --i){
        int cand = nxt[0][i][curr_x];
        if(cand != -1){
            if(curr_x < target){
                if(cand < curr_x || target < cand)
                    curr_x = cand;
            }
            else{
                if(target < cand && cand < curr_x)
                    curr_x = cand;
            }
        }
    }
    curr_x = nxt[0][0][curr_x];
    if(curr_x != -1 && get_dist(y, curr_x) <= k - 1)
        if(p[curr_x] <= p[y])
            return true;

    target = (y - (k - 1) + n) % n;
    curr_x = x;
    for(int i = LOGN - 1; i >= 0; --i){
        int cand = nxt[1][i][curr_x];
        if(cand != -1){
            if(curr_x < target){
                if(curr_x < cand && cand < target)
                    curr_x = cand;
            }
            else{
                if(curr_x < cand || cand < target)
                    curr_x = cand;
            }
        }
    }
    curr_x = nxt[1][0][curr_x];
    if(curr_x != -1 && get_dist(y, curr_x) <= k - 1)
        if(p[curr_x] <= p[y])
            return true;

    return false;
}

int compare_plants(int x, int y) {
    if(!connected(x, y)) return 0;
    return (p[x] > p[y]) ? 1 : -1;
}
/*
4 3 2
0 1 1 2
0 2
1 2
*/
/*
4 2 2
0 1 0 1
0 3
1 3
*/
/*
4 3 2
1 1 0 2
0 1
0 2
*/

Compilation message

plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:185:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  185 |     for(int i = 0; i < _r.size(); ++i)
      |                    ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 71 ms 3436 KB Output is correct
7 Correct 146 ms 7532 KB Output is correct
8 Correct 665 ms 45112 KB Output is correct
9 Correct 691 ms 45136 KB Output is correct
10 Correct 705 ms 45292 KB Output is correct
11 Correct 753 ms 45232 KB Output is correct
12 Correct 756 ms 45164 KB Output is correct
13 Correct 799 ms 45164 KB Output is correct
14 Correct 763 ms 45164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 5 ms 876 KB Output is correct
7 Correct 84 ms 4736 KB Output is correct
8 Correct 2 ms 620 KB Output is correct
9 Correct 5 ms 876 KB Output is correct
10 Correct 82 ms 4588 KB Output is correct
11 Correct 80 ms 4460 KB Output is correct
12 Correct 76 ms 4736 KB Output is correct
13 Correct 82 ms 4676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 5 ms 876 KB Output is correct
7 Correct 84 ms 4736 KB Output is correct
8 Correct 2 ms 620 KB Output is correct
9 Correct 5 ms 876 KB Output is correct
10 Correct 82 ms 4588 KB Output is correct
11 Correct 80 ms 4460 KB Output is correct
12 Correct 76 ms 4736 KB Output is correct
13 Correct 82 ms 4676 KB Output is correct
14 Correct 152 ms 8152 KB Output is correct
15 Correct 1515 ms 48012 KB Output is correct
16 Correct 151 ms 8044 KB Output is correct
17 Correct 1535 ms 48116 KB Output is correct
18 Correct 896 ms 46848 KB Output is correct
19 Correct 745 ms 46956 KB Output is correct
20 Correct 1496 ms 51612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 109 ms 3692 KB Output is correct
4 Correct 953 ms 45164 KB Output is correct
5 Correct 1165 ms 45292 KB Output is correct
6 Correct 1229 ms 45528 KB Output is correct
7 Correct 1290 ms 46188 KB Output is correct
8 Correct 1389 ms 50144 KB Output is correct
9 Correct 963 ms 45388 KB Output is correct
10 Correct 1029 ms 45276 KB Output is correct
11 Correct 815 ms 45100 KB Output is correct
12 Correct 847 ms 45480 KB Output is correct
13 Correct 915 ms 48244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 3 ms 620 KB Output is correct
7 Correct 25 ms 1260 KB Output is correct
8 Correct 16 ms 1388 KB Output is correct
9 Correct 22 ms 1260 KB Output is correct
10 Correct 17 ms 1388 KB Output is correct
11 Correct 20 ms 1260 KB Output is correct
12 Correct 21 ms 1388 KB Output is correct
13 Correct 16 ms 1260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 5 ms 748 KB Output is correct
6 Correct 859 ms 42348 KB Output is correct
7 Correct 1093 ms 42476 KB Output is correct
8 Correct 1218 ms 42732 KB Output is correct
9 Correct 1384 ms 46400 KB Output is correct
10 Correct 758 ms 42348 KB Output is correct
11 Correct 1049 ms 45964 KB Output is correct
12 Correct 792 ms 42168 KB Output is correct
13 Correct 892 ms 42348 KB Output is correct
14 Correct 1116 ms 42252 KB Output is correct
15 Correct 1148 ms 42732 KB Output is correct
16 Correct 793 ms 42320 KB Output is correct
17 Correct 843 ms 42164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 71 ms 3436 KB Output is correct
7 Correct 146 ms 7532 KB Output is correct
8 Correct 665 ms 45112 KB Output is correct
9 Correct 691 ms 45136 KB Output is correct
10 Correct 705 ms 45292 KB Output is correct
11 Correct 753 ms 45232 KB Output is correct
12 Correct 756 ms 45164 KB Output is correct
13 Correct 799 ms 45164 KB Output is correct
14 Correct 763 ms 45164 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 620 KB Output is correct
20 Correct 5 ms 876 KB Output is correct
21 Correct 84 ms 4736 KB Output is correct
22 Correct 2 ms 620 KB Output is correct
23 Correct 5 ms 876 KB Output is correct
24 Correct 82 ms 4588 KB Output is correct
25 Correct 80 ms 4460 KB Output is correct
26 Correct 76 ms 4736 KB Output is correct
27 Correct 82 ms 4676 KB Output is correct
28 Correct 152 ms 8152 KB Output is correct
29 Correct 1515 ms 48012 KB Output is correct
30 Correct 151 ms 8044 KB Output is correct
31 Correct 1535 ms 48116 KB Output is correct
32 Correct 896 ms 46848 KB Output is correct
33 Correct 745 ms 46956 KB Output is correct
34 Correct 1496 ms 51612 KB Output is correct
35 Correct 1 ms 492 KB Output is correct
36 Correct 1 ms 492 KB Output is correct
37 Correct 109 ms 3692 KB Output is correct
38 Correct 953 ms 45164 KB Output is correct
39 Correct 1165 ms 45292 KB Output is correct
40 Correct 1229 ms 45528 KB Output is correct
41 Correct 1290 ms 46188 KB Output is correct
42 Correct 1389 ms 50144 KB Output is correct
43 Correct 963 ms 45388 KB Output is correct
44 Correct 1029 ms 45276 KB Output is correct
45 Correct 815 ms 45100 KB Output is correct
46 Correct 847 ms 45480 KB Output is correct
47 Correct 915 ms 48244 KB Output is correct
48 Correct 1 ms 492 KB Output is correct
49 Correct 1 ms 492 KB Output is correct
50 Correct 1 ms 492 KB Output is correct
51 Correct 1 ms 492 KB Output is correct
52 Correct 1 ms 492 KB Output is correct
53 Correct 3 ms 620 KB Output is correct
54 Correct 25 ms 1260 KB Output is correct
55 Correct 16 ms 1388 KB Output is correct
56 Correct 22 ms 1260 KB Output is correct
57 Correct 17 ms 1388 KB Output is correct
58 Correct 20 ms 1260 KB Output is correct
59 Correct 21 ms 1388 KB Output is correct
60 Correct 16 ms 1260 KB Output is correct
61 Correct 92 ms 5484 KB Output is correct
62 Correct 163 ms 9580 KB Output is correct
63 Correct 847 ms 45348 KB Output is correct
64 Correct 1053 ms 45488 KB Output is correct
65 Correct 1260 ms 45660 KB Output is correct
66 Correct 1290 ms 46108 KB Output is correct
67 Correct 1424 ms 50332 KB Output is correct
68 Correct 973 ms 45284 KB Output is correct
69 Correct 1085 ms 49596 KB Output is correct
70 Correct 996 ms 45292 KB Output is correct
71 Correct 1164 ms 45292 KB Output is correct
72 Correct 1268 ms 45584 KB Output is correct
73 Correct 1250 ms 46364 KB Output is correct
74 Correct 882 ms 45188 KB Output is correct
75 Correct 1022 ms 45420 KB Output is correct