Submission #431850

# Submission time Handle Problem Language Result Execution time Memory
431850 2021-06-17T16:05:45 Z Peti Comparing Plants (IOI20_plants) C++14
100 / 100
2201 ms 137844 KB
#include <bits/stdc++.h>
#include "plants.h"

using namespace std;

const int maxn = 1<<18, logn = 18;

pair<int, int> st[maxn<<1];
int add[maxn<<1] = {};

void prop(int x){
    if(x < maxn){
        add[2*x] += add[x];
        st[2*x].first += add[x];
        add[2*x+1] += add[x];
        st[2*x+1].first += add[x];
    }
    add[x] = 0;
}

inline void upd(int &x){
    if(x < maxn) st[x] = min(st[2*x], st[2*x+1]);
}

void build(vector<int> &v){
    for(int i = 0; i < (int)v.size(); i++)
        st[maxn+i] = make_pair(v[i], i);
    for(int i = maxn-1; i > 0; i--)
        upd(i);
}

void kivesz(int ind, int x, int l, int r){
    prop(x);
    if(l+1 == r){
        st[x].first = (int)1e9;
        return;
    }
    int m = (l+r)/2;
    if(ind < m)
        kivesz(ind, 2*x, l, m);
    else
        kivesz(ind, 2*x+1, m, r);
    upd(x);
}

void update(int s, int e, int x, int l, int r){
    prop(x);
    if(e <= l || r <= s)
        return;
    if(s <= l && r <= e){
        st[x].first--;
        add[x]--;
        return;
    }
    int m = (l+r)/2;
    update(s, e, 2*x, l, m);
    update(s, e, 2*x+1, m, r);
    upd(x);
}

void update(int l, int r, int n){
    if(l > r){
        update(l, n, 1, 0, maxn);
        update(0, r, 1, 0, maxn);
    } else
        update(l, r, 1, 0, maxn);
}

pair<int, int> keres(int s, int e, int x, int l, int r){
    prop(x);
    if(e <= l || r <= s)
        return make_pair((int)1e9, 0);
    if(s <= l && r <= e)
        return st[x];
    int m = (l+r)/2;
    return min(keres(s, e, 2*x, l, m), keres(s, e, 2*x+1, m, r));
}

int keres(int l, int r, int n){
    if(r < l){
        pair<int, int> temp = keres(l, n, 1, 0, maxn);
        if(temp.first == 0)
            return temp.second;
        temp = keres(0, r, 1, 0, maxn);
        if(temp.first == 0)
            return temp.second;
        return -1;
    }
    pair<int, int> temp = keres(l, r, 1, 0, maxn);
    if(temp.first == 0)
        return temp.second;
    return -1;
}

struct node{
    int l = -1, r = -1;
    long long lc, rc;
    vector<int> indL, indR;
    vector<long long> hL, hR;
};

vector<node> g;
vector<int> vh;
int n, K;

void init(int k, std::vector<int> r) {
    n = (int)r.size();
    K = k;
    g.resize(n);
    vh.resize(n);
    for(int &x : r)
        x = k-x-1;
    build(r);

    int x = keres(0, n, n), h = 0;
    vector<int> sor;
    while(x != -1){
        int y = keres((x-k+1 < 0 ? n+x-k+1 : x-k+1), x, n);
        while(y != -1){
            x = y;
            y = keres((x-k+1 < 0 ? n+x-k+1 : x-k+1), x, n);
        }
        vh[x] = h++;
        sor.push_back(x);
        update((x-k+1 < 0 ? n+x-k+1 : x-k+1), x, n);
        kivesz(x, 1, 0, maxn);
        x = keres(x+1, x, n);
    }

    set<pair<int, int>, greater<pair<int, int>>> nums;
    for(int i = n-k+1; i < n; i++)
        nums.insert(make_pair(vh[i], i));
    for(int i = 0; i < n; i++){
        auto it = nums.lower_bound(make_pair(vh[i], i));
        if(it != nums.end())
            g[i].l = it->second;

        int j = i-k+1;
        if(j < 0) j += n;
        nums.erase(make_pair(vh[j], j));
        nums.insert(make_pair(vh[i], i));
    }
    nums.clear();
    for(int i = 0; i < k-1; i++)
        nums.insert(make_pair(vh[i], i));
    for(int i = n-1; i >= 0; i--){
        auto it = nums.lower_bound(make_pair(vh[i], i));
        if(it != nums.end())
            g[i].r = it->second;

        int j = i+k-1;
        if(j >= n) j -= n;
        nums.erase(make_pair(vh[j], j));
        nums.insert(make_pair(vh[i], i));
    }

    for(int x : sor){
        g[x].lc = x - g[x].l;
        if(g[x].lc < 0) g[x].lc += n;
        g[x].rc = g[x].r - x;
        if(g[x].rc < 0) g[x].rc += n;
        if(g[x].l == -1)
            g[x].lc = 0;
        if(g[x].r == -1)
            g[x].rc = 0;
        g[x].indL.assign(logn, -1);
        g[x].indR.assign(logn, -1);
        g[x].hL.assign(logn, (long long)1e9);
        g[x].hR.assign(logn, (long long)1e9);
        g[x].indL[0] = g[x].l;
        g[x].hL[0] = (g[x].l == -1 ? (long long)1e9 : g[x].lc);
        g[x].indR[0] = g[x].r;
        g[x].hR[0] = (g[x].r == -1 ? (long long)1e9 : g[x].rc);
        for(int i = 1; i < logn; i++){
            if(g[x].indL[i-1] == -1)
                break;
            g[x].indL[i] = g[g[x].indL[i-1]].indL[i-1];
            if(g[x].indL[i] != -1)
                g[x].hL[i] = g[x].hL[i-1] + g[g[x].indL[i-1]].hL[i-1];
        }
        for(int i = 1; i < logn; i++){
            if(g[x].indR[i-1] == -1)
                break;
            g[x].indR[i] = g[g[x].indR[i-1]].indR[i-1];
            if(g[x].indR[i] != -1)
                g[x].hR[i] = g[x].hR[i-1] + g[g[x].indR[i-1]].hR[i-1];
        }
    }

    /*cout<<"vh: ";
    for(int x : vh)
        cout<<x<<" ";
    cout<<"\n";

    for(int i = 0; i < n; i++){
        cout<<"pont "<<i<<": "<<g[i].l<<" "<<g[i].r<<" | "<<g[i].lc<<" "<<g[i].rc<<"\n";
        cout<<"inds: ";
        for(int x : g[i].indL)
            cout<<x<<" ";
        cout<<"\n";
        cout<<"h: ";
        for(int x : g[i].hL)
            cout<<x<<" ";
        cout<<"\n";
    }*/
	return;
}

int compare_plants(int x, int y) {
    int ret = 1;
    if(vh[x] < vh[y]){
        ret = -1;
        swap(x, y);
    }

    long long d = x-y;
    if(d < 0) d += n;
    int a = x;
    for(int i = logn-1; i >= 0; i--){
        if(d >= g[a].hL[i]){
            d -= g[a].hL[i];
            a = g[a].indL[i];
        }
    }

    if(d < K && vh[a] >= vh[y])
        return ret;

    d = y-x;
    if(d < 0) d += n;
    //cout<<"disR: "<<d<<"\n";
    a = x;
    for(int i = logn-1; i >= 0; i--){
        if(d >= g[a].hR[i]){
            d -= g[a].hR[i];
            a = g[a].indR[i];
        }
    }
    //cout<<a<<" a\n";

    if(d < K && vh[a] >= vh[y])
        return ret;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2380 KB Output is correct
2 Correct 2 ms 2380 KB Output is correct
3 Correct 2 ms 2380 KB Output is correct
4 Correct 3 ms 2380 KB Output is correct
5 Correct 2 ms 2380 KB Output is correct
6 Correct 88 ms 5212 KB Output is correct
7 Correct 219 ms 17780 KB Output is correct
8 Correct 811 ms 129396 KB Output is correct
9 Correct 845 ms 129392 KB Output is correct
10 Correct 934 ms 129372 KB Output is correct
11 Correct 1036 ms 129504 KB Output is correct
12 Correct 1146 ms 129400 KB Output is correct
13 Correct 1322 ms 129400 KB Output is correct
14 Correct 1186 ms 129460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2380 KB Output is correct
2 Correct 2 ms 2380 KB Output is correct
3 Correct 2 ms 2380 KB Output is correct
4 Correct 2 ms 2380 KB Output is correct
5 Correct 3 ms 2380 KB Output is correct
6 Correct 9 ms 3020 KB Output is correct
7 Correct 103 ms 8560 KB Output is correct
8 Correct 4 ms 2508 KB Output is correct
9 Correct 9 ms 3020 KB Output is correct
10 Correct 101 ms 8424 KB Output is correct
11 Correct 126 ms 8296 KB Output is correct
12 Correct 133 ms 8516 KB Output is correct
13 Correct 104 ms 8468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2380 KB Output is correct
2 Correct 2 ms 2380 KB Output is correct
3 Correct 2 ms 2380 KB Output is correct
4 Correct 2 ms 2380 KB Output is correct
5 Correct 3 ms 2380 KB Output is correct
6 Correct 9 ms 3020 KB Output is correct
7 Correct 103 ms 8560 KB Output is correct
8 Correct 4 ms 2508 KB Output is correct
9 Correct 9 ms 3020 KB Output is correct
10 Correct 101 ms 8424 KB Output is correct
11 Correct 126 ms 8296 KB Output is correct
12 Correct 133 ms 8516 KB Output is correct
13 Correct 104 ms 8468 KB Output is correct
14 Correct 216 ms 17908 KB Output is correct
15 Correct 2037 ms 134240 KB Output is correct
16 Correct 231 ms 18024 KB Output is correct
17 Correct 2077 ms 134168 KB Output is correct
18 Correct 1569 ms 133016 KB Output is correct
19 Correct 1530 ms 133048 KB Output is correct
20 Correct 2201 ms 137844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2380 KB Output is correct
2 Correct 2 ms 2380 KB Output is correct
3 Correct 114 ms 6376 KB Output is correct
4 Correct 1516 ms 129424 KB Output is correct
5 Correct 1651 ms 128472 KB Output is correct
6 Correct 1491 ms 128560 KB Output is correct
7 Correct 1512 ms 128912 KB Output is correct
8 Correct 1846 ms 132628 KB Output is correct
9 Correct 1468 ms 131552 KB Output is correct
10 Correct 1317 ms 131420 KB Output is correct
11 Correct 1281 ms 131404 KB Output is correct
12 Correct 1261 ms 131512 KB Output is correct
13 Correct 1562 ms 134316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2380 KB Output is correct
2 Correct 2 ms 2380 KB Output is correct
3 Correct 2 ms 2380 KB Output is correct
4 Correct 3 ms 2380 KB Output is correct
5 Correct 2 ms 2380 KB Output is correct
6 Correct 4 ms 2508 KB Output is correct
7 Correct 21 ms 3532 KB Output is correct
8 Correct 19 ms 3460 KB Output is correct
9 Correct 22 ms 3532 KB Output is correct
10 Correct 19 ms 3448 KB Output is correct
11 Correct 21 ms 3404 KB Output is correct
12 Correct 21 ms 3512 KB Output is correct
13 Correct 19 ms 3524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2380 KB Output is correct
2 Correct 2 ms 2380 KB Output is correct
3 Correct 2 ms 2380 KB Output is correct
4 Correct 2 ms 2380 KB Output is correct
5 Correct 6 ms 3020 KB Output is correct
6 Correct 1287 ms 130616 KB Output is correct
7 Correct 1290 ms 130940 KB Output is correct
8 Correct 1267 ms 131500 KB Output is correct
9 Correct 1867 ms 135424 KB Output is correct
10 Correct 1255 ms 130512 KB Output is correct
11 Correct 1519 ms 134840 KB Output is correct
12 Correct 1276 ms 130452 KB Output is correct
13 Correct 1281 ms 130660 KB Output is correct
14 Correct 1278 ms 130752 KB Output is correct
15 Correct 1415 ms 131636 KB Output is correct
16 Correct 1022 ms 130480 KB Output is correct
17 Correct 941 ms 130800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2380 KB Output is correct
2 Correct 2 ms 2380 KB Output is correct
3 Correct 2 ms 2380 KB Output is correct
4 Correct 3 ms 2380 KB Output is correct
5 Correct 2 ms 2380 KB Output is correct
6 Correct 88 ms 5212 KB Output is correct
7 Correct 219 ms 17780 KB Output is correct
8 Correct 811 ms 129396 KB Output is correct
9 Correct 845 ms 129392 KB Output is correct
10 Correct 934 ms 129372 KB Output is correct
11 Correct 1036 ms 129504 KB Output is correct
12 Correct 1146 ms 129400 KB Output is correct
13 Correct 1322 ms 129400 KB Output is correct
14 Correct 1186 ms 129460 KB Output is correct
15 Correct 2 ms 2380 KB Output is correct
16 Correct 2 ms 2380 KB Output is correct
17 Correct 2 ms 2380 KB Output is correct
18 Correct 2 ms 2380 KB Output is correct
19 Correct 3 ms 2380 KB Output is correct
20 Correct 9 ms 3020 KB Output is correct
21 Correct 103 ms 8560 KB Output is correct
22 Correct 4 ms 2508 KB Output is correct
23 Correct 9 ms 3020 KB Output is correct
24 Correct 101 ms 8424 KB Output is correct
25 Correct 126 ms 8296 KB Output is correct
26 Correct 133 ms 8516 KB Output is correct
27 Correct 104 ms 8468 KB Output is correct
28 Correct 216 ms 17908 KB Output is correct
29 Correct 2037 ms 134240 KB Output is correct
30 Correct 231 ms 18024 KB Output is correct
31 Correct 2077 ms 134168 KB Output is correct
32 Correct 1569 ms 133016 KB Output is correct
33 Correct 1530 ms 133048 KB Output is correct
34 Correct 2201 ms 137844 KB Output is correct
35 Correct 2 ms 2380 KB Output is correct
36 Correct 2 ms 2380 KB Output is correct
37 Correct 114 ms 6376 KB Output is correct
38 Correct 1516 ms 129424 KB Output is correct
39 Correct 1651 ms 128472 KB Output is correct
40 Correct 1491 ms 128560 KB Output is correct
41 Correct 1512 ms 128912 KB Output is correct
42 Correct 1846 ms 132628 KB Output is correct
43 Correct 1468 ms 131552 KB Output is correct
44 Correct 1317 ms 131420 KB Output is correct
45 Correct 1281 ms 131404 KB Output is correct
46 Correct 1261 ms 131512 KB Output is correct
47 Correct 1562 ms 134316 KB Output is correct
48 Correct 2 ms 2380 KB Output is correct
49 Correct 2 ms 2380 KB Output is correct
50 Correct 2 ms 2380 KB Output is correct
51 Correct 3 ms 2380 KB Output is correct
52 Correct 2 ms 2380 KB Output is correct
53 Correct 4 ms 2508 KB Output is correct
54 Correct 21 ms 3532 KB Output is correct
55 Correct 19 ms 3460 KB Output is correct
56 Correct 22 ms 3532 KB Output is correct
57 Correct 19 ms 3448 KB Output is correct
58 Correct 21 ms 3404 KB Output is correct
59 Correct 21 ms 3512 KB Output is correct
60 Correct 19 ms 3524 KB Output is correct
61 Correct 99 ms 8112 KB Output is correct
62 Correct 208 ms 19524 KB Output is correct
63 Correct 985 ms 131256 KB Output is correct
64 Correct 1357 ms 131412 KB Output is correct
65 Correct 1421 ms 131748 KB Output is correct
66 Correct 1423 ms 132428 KB Output is correct
67 Correct 1832 ms 136304 KB Output is correct
68 Correct 1360 ms 131464 KB Output is correct
69 Correct 1580 ms 136036 KB Output is correct
70 Correct 1484 ms 131320 KB Output is correct
71 Correct 1738 ms 131484 KB Output is correct
72 Correct 1507 ms 131660 KB Output is correct
73 Correct 1475 ms 132320 KB Output is correct
74 Correct 1022 ms 131456 KB Output is correct
75 Correct 1210 ms 131596 KB Output is correct