Submission #304503

# Submission time Handle Problem Language Result Execution time Memory
304503 2020-09-21T12:33:29 Z icypiggy Comparing Plants (IOI20_plants) C++14
100 / 100
1882 ms 26540 KB
#include "plants.h"
#include <bits/stdc++.h>
#define low(i) (i<<(__builtin_clz(i)-31+n_bits))-(1<<n_bits)
#define high(i) ((i+1)<<(__builtin_clz(i)-31+n_bits))-(1<<n_bits)-1
using namespace std;
vector<int> tallest;
vector<int> shortest;
int n_global;
const int n_bits=20;
const int inf = 1e9;
int arr[1<<(n_bits+1)];
int lazyadd[1<<(n_bits+1)];

struct segtree {
    segtree(){}
    void build(vector<int> v) {
        assert(v.size()<(1<<n_bits));
        for(int i=0; i<(1<<n_bits); i++) {
            arr[i+(1<<n_bits)] = (i<(int)v.size() ? v[i] : inf);
        }
        for(int i=(1<<n_bits)-1; i>=1; i--) {
            arr[i] = min(arr[2*i], arr[2*i+1]);
        }
        for(int i=0; i<(1<<(n_bits+1)); i++) {
            lazyadd[i] = 0;
        }
    }
    int value(int node) {
        arr[node] += lazyadd[node];
        if(node<(1<<n_bits)) {
            lazyadd[2*node] += lazyadd[node];
            lazyadd[2*node+1] += lazyadd[node];
        }
        lazyadd[node] = 0;
        return arr[node];
    }
    void update(int node, int left, int right, int change) {
        if(right>=high(node) && left<=low(node)) {
            lazyadd[node] += change;
        } else if(right<low(node) || left>high(node)) {
            return;
        } else {
            update(2*node, left, right, change);
            update(2*node+1, left, right, change);
            arr[node] = min(value(node*2), value(node*2+1));
        }
    }

    void decr(int left, int right) {
        update(1, left, right, -1);
    }

    int find_zero(int node) {
        if(value(node)!=0) {
            return -1;
        }
        if(node >= (1<<n_bits)) {
            return arr[node]==0 ? node-(1<<n_bits) : -1;
        }
        int x = find_zero(node*2);
        if(x!=-1) return x;
        return find_zero(node*2+1);
    }
};

// gives you the lexi smallest ordering given the doubled array r
vector<int> lexi_smallest(int k, vector<int> r) {
    segtree s;
    s.build(r);
    vector<int> ret;
    ret.resize(r.size());
    queue<int> stash;
    for(int i=0; i<(int)r.size(); i++) {
        int p = s.find_zero(1);
        while(p==-1) {
            s.decr(stash.front(), 2*n_global-1);
            p = s.find_zero(1);
            stash.pop();
        }
        ret[p] = i;
        s.update(1, p, p, 1e9);

        s.decr(max(0, p-k+1), p);
        if(p<k-1) {
            stash.push(p-k+1+2*n_global);
        }
    }
    return ret;
}
void init(int k, std::vector<int> r) {
    n_global = r.size();
    for(int i=0; i<n_global; i++) {
        r.push_back(r[i]);
    }
    tallest = lexi_smallest(k,r);
    for(int &i: r) i = k-1-i;
    shortest = lexi_smallest(k,r);
}

int compare_plants(int x, int y) {
    if(x>y) return -compare_plants(y,x);
    if(tallest[x]>tallest[y] || shortest[y]>shortest[x+n_global]) return -1;
    if(shortest[x]>shortest[y] || tallest[y]>tallest[x+n_global]) return 1;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 16768 KB Output is correct
2 Correct 16 ms 16768 KB Output is correct
3 Correct 16 ms 16768 KB Output is correct
4 Correct 16 ms 16768 KB Output is correct
5 Correct 16 ms 16768 KB Output is correct
6 Correct 79 ms 19576 KB Output is correct
7 Correct 166 ms 20120 KB Output is correct
8 Correct 841 ms 26156 KB Output is correct
9 Correct 824 ms 26156 KB Output is correct
10 Correct 835 ms 26156 KB Output is correct
11 Correct 830 ms 26156 KB Output is correct
12 Correct 845 ms 26160 KB Output is correct
13 Correct 825 ms 26156 KB Output is correct
14 Correct 822 ms 26160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 16768 KB Output is correct
2 Correct 16 ms 16768 KB Output is correct
3 Correct 16 ms 16768 KB Output is correct
4 Correct 17 ms 16672 KB Output is correct
5 Correct 17 ms 16768 KB Output is correct
6 Correct 23 ms 16888 KB Output is correct
7 Correct 115 ms 19832 KB Output is correct
8 Correct 18 ms 16768 KB Output is correct
9 Correct 23 ms 16896 KB Output is correct
10 Correct 116 ms 19832 KB Output is correct
11 Correct 117 ms 19704 KB Output is correct
12 Correct 107 ms 19960 KB Output is correct
13 Correct 119 ms 19832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 16768 KB Output is correct
2 Correct 16 ms 16768 KB Output is correct
3 Correct 16 ms 16768 KB Output is correct
4 Correct 17 ms 16672 KB Output is correct
5 Correct 17 ms 16768 KB Output is correct
6 Correct 23 ms 16888 KB Output is correct
7 Correct 115 ms 19832 KB Output is correct
8 Correct 18 ms 16768 KB Output is correct
9 Correct 23 ms 16896 KB Output is correct
10 Correct 116 ms 19832 KB Output is correct
11 Correct 117 ms 19704 KB Output is correct
12 Correct 107 ms 19960 KB Output is correct
13 Correct 119 ms 19832 KB Output is correct
14 Correct 208 ms 20116 KB Output is correct
15 Correct 1630 ms 26156 KB Output is correct
16 Correct 208 ms 20248 KB Output is correct
17 Correct 1644 ms 26288 KB Output is correct
18 Correct 1058 ms 26492 KB Output is correct
19 Correct 1058 ms 26540 KB Output is correct
20 Correct 1588 ms 26160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 16768 KB Output is correct
2 Correct 17 ms 16768 KB Output is correct
3 Correct 95 ms 19704 KB Output is correct
4 Correct 862 ms 26160 KB Output is correct
5 Correct 979 ms 26160 KB Output is correct
6 Correct 1262 ms 26324 KB Output is correct
7 Correct 1510 ms 26160 KB Output is correct
8 Correct 1621 ms 26156 KB Output is correct
9 Correct 917 ms 26156 KB Output is correct
10 Correct 912 ms 26192 KB Output is correct
11 Correct 836 ms 26160 KB Output is correct
12 Correct 910 ms 26160 KB Output is correct
13 Correct 1043 ms 26408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 16768 KB Output is correct
2 Correct 16 ms 16768 KB Output is correct
3 Correct 16 ms 16768 KB Output is correct
4 Correct 16 ms 16768 KB Output is correct
5 Correct 16 ms 16768 KB Output is correct
6 Correct 18 ms 16768 KB Output is correct
7 Correct 32 ms 17400 KB Output is correct
8 Correct 33 ms 17400 KB Output is correct
9 Correct 32 ms 17400 KB Output is correct
10 Correct 32 ms 17408 KB Output is correct
11 Correct 32 ms 17400 KB Output is correct
12 Correct 33 ms 17408 KB Output is correct
13 Correct 33 ms 17400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 16768 KB Output is correct
2 Correct 16 ms 16768 KB Output is correct
3 Correct 16 ms 16768 KB Output is correct
4 Correct 16 ms 16660 KB Output is correct
5 Correct 23 ms 16768 KB Output is correct
6 Correct 960 ms 26156 KB Output is correct
7 Correct 1275 ms 26280 KB Output is correct
8 Correct 1574 ms 26160 KB Output is correct
9 Correct 1704 ms 26156 KB Output is correct
10 Correct 941 ms 26104 KB Output is correct
11 Correct 1213 ms 26344 KB Output is correct
12 Correct 893 ms 26136 KB Output is correct
13 Correct 963 ms 26132 KB Output is correct
14 Correct 1293 ms 26412 KB Output is correct
15 Correct 1582 ms 26136 KB Output is correct
16 Correct 903 ms 26148 KB Output is correct
17 Correct 968 ms 26156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 16768 KB Output is correct
2 Correct 16 ms 16768 KB Output is correct
3 Correct 16 ms 16768 KB Output is correct
4 Correct 16 ms 16768 KB Output is correct
5 Correct 16 ms 16768 KB Output is correct
6 Correct 79 ms 19576 KB Output is correct
7 Correct 166 ms 20120 KB Output is correct
8 Correct 841 ms 26156 KB Output is correct
9 Correct 824 ms 26156 KB Output is correct
10 Correct 835 ms 26156 KB Output is correct
11 Correct 830 ms 26156 KB Output is correct
12 Correct 845 ms 26160 KB Output is correct
13 Correct 825 ms 26156 KB Output is correct
14 Correct 822 ms 26160 KB Output is correct
15 Correct 17 ms 16768 KB Output is correct
16 Correct 16 ms 16768 KB Output is correct
17 Correct 16 ms 16768 KB Output is correct
18 Correct 17 ms 16672 KB Output is correct
19 Correct 17 ms 16768 KB Output is correct
20 Correct 23 ms 16888 KB Output is correct
21 Correct 115 ms 19832 KB Output is correct
22 Correct 18 ms 16768 KB Output is correct
23 Correct 23 ms 16896 KB Output is correct
24 Correct 116 ms 19832 KB Output is correct
25 Correct 117 ms 19704 KB Output is correct
26 Correct 107 ms 19960 KB Output is correct
27 Correct 119 ms 19832 KB Output is correct
28 Correct 208 ms 20116 KB Output is correct
29 Correct 1630 ms 26156 KB Output is correct
30 Correct 208 ms 20248 KB Output is correct
31 Correct 1644 ms 26288 KB Output is correct
32 Correct 1058 ms 26492 KB Output is correct
33 Correct 1058 ms 26540 KB Output is correct
34 Correct 1588 ms 26160 KB Output is correct
35 Correct 16 ms 16768 KB Output is correct
36 Correct 17 ms 16768 KB Output is correct
37 Correct 95 ms 19704 KB Output is correct
38 Correct 862 ms 26160 KB Output is correct
39 Correct 979 ms 26160 KB Output is correct
40 Correct 1262 ms 26324 KB Output is correct
41 Correct 1510 ms 26160 KB Output is correct
42 Correct 1621 ms 26156 KB Output is correct
43 Correct 917 ms 26156 KB Output is correct
44 Correct 912 ms 26192 KB Output is correct
45 Correct 836 ms 26160 KB Output is correct
46 Correct 910 ms 26160 KB Output is correct
47 Correct 1043 ms 26408 KB Output is correct
48 Correct 16 ms 16768 KB Output is correct
49 Correct 16 ms 16768 KB Output is correct
50 Correct 16 ms 16768 KB Output is correct
51 Correct 16 ms 16768 KB Output is correct
52 Correct 16 ms 16768 KB Output is correct
53 Correct 18 ms 16768 KB Output is correct
54 Correct 32 ms 17400 KB Output is correct
55 Correct 33 ms 17400 KB Output is correct
56 Correct 32 ms 17400 KB Output is correct
57 Correct 32 ms 17408 KB Output is correct
58 Correct 32 ms 17400 KB Output is correct
59 Correct 33 ms 17408 KB Output is correct
60 Correct 33 ms 17400 KB Output is correct
61 Correct 96 ms 19576 KB Output is correct
62 Correct 170 ms 19988 KB Output is correct
63 Correct 893 ms 26140 KB Output is correct
64 Correct 1045 ms 26140 KB Output is correct
65 Correct 1543 ms 26148 KB Output is correct
66 Correct 1882 ms 26144 KB Output is correct
67 Correct 1661 ms 26200 KB Output is correct
68 Correct 950 ms 26156 KB Output is correct
69 Correct 1296 ms 26480 KB Output is correct
70 Correct 878 ms 26156 KB Output is correct
71 Correct 1004 ms 26156 KB Output is correct
72 Correct 1326 ms 26156 KB Output is correct
73 Correct 1524 ms 26320 KB Output is correct
74 Correct 886 ms 26156 KB Output is correct
75 Correct 1046 ms 26136 KB Output is correct