Submission #837066

# Submission time Handle Problem Language Result Execution time Memory
837066 2023-08-24T20:49:44 Z kamelfanger83 The Big Prize (IOI17_prize) C++14
97.94 / 100
50 ms 8508 KB
#include <vector>
#include <cassert>

#include "prize.h"

using namespace std;

struct Segtree{
    vector<int> cache;
    Segtree(int n){
        cache.resize(4 * n);
    }
    void buildao(int l, int r, int i){
        cache[i] = r-l;
        if (l + 1 < r){
            int m = l + (r-l)/2;
            buildao(l, m, i*2+1);
            buildao(m, r, i*2+2);
        }
    }
    int update(int l, int r, int i, int ui, int ux){
        if (ui < l || r <= ui) return cache[i];
        if (l + 1 == r) return cache[i] = ux;
        int m = l + (r-l)/2;
        return cache[i] = update(l, m, i*2+1, ui, ux) + update(m, r, i*2+2, ui, ux);
    }
    int query(int l, int r, int i, int ql, int qr){
        if (qr <= l || r <= ql) return 0;
        if (ql <= l && r <= qr) return cache[i];
        int m = l + (r-l)/2;
        return query(l, m, i*2+1, ql, qr) + query(m, r, i*2+2, ql, qr);
    }
    int findi (int l, int r, int i, int sum){
        if (l + 1 == r) {
            if (cache[i] > sum) return l;
            return r;
        }
        int m = l + (r-l)/2;
        if (cache[i*2+1] > sum) return findi(l, m, i*2+1, sum);
        else return findi(m, r, i*2+2, sum - cache[i*2+1]);
    }
};

vector<int> queried; int maxsum = 0;
Segtree segtree (1);
int N;

vector<vector<int>> ans;

vector<int> myask(int i){
    if (ans[i].size()) return ans[i];
    return ans[i] = ask(i);
}

int find(int fake_l, int fake_r, int suml, int sumr, int sumh){
    if (fake_l >= fake_r) {
        //assert(sumh == 0);
        return -1;
    }
    if (sumh == 0) return -1;
    int fake_m = (fake_l + fake_r) / 2;
    int real_m = segtree.findi(0, N, 0, fake_m);
    auto ans = myask(real_m);
    int l = ans[0], r = ans[1];
    if (l + r == 0) return real_m;
    else if (l + r > maxsum){
        for (int i : queried) segtree.update(0, N, 0, i, 0);
        queried = {real_m};
        maxsum = l + r;
        return -2;
    }
    else if (l + r < maxsum){
        segtree.update(0, N, 0, real_m, 0);
        int middle = 1;
        fake_r--;
        for (int dev = 1;; ++dev) {
            if (fake_m < fake_r) {
                int real_m = segtree.findi(0, N, 0, fake_m);
                auto ans = myask(real_m);
                int l = ans[0], r = ans[1];
                if (l + r == 0) return real_m;
                if (l + r > maxsum) {
                    for (int i : queried) segtree.update(0, N, 0, i, 0);
                    queried = {real_m};
                    maxsum = l + r;
                    return -2;
                }
                if (l + r == maxsum){
                    int r1 = find(fake_l, fake_m, suml, r + middle, l - suml - middle);
                    int r2 = find(fake_m + 1 - (l - suml - middle), fake_r - (l - suml - middle), l, sumr, r - sumr);
                    if (r1 != -1) return r1;
                    return r2;
                }
                segtree.update(0, N, 0, real_m, 0);
                middle++;
                fake_r--;
            }
            else {
                return find(fake_l, fake_m, suml, sumr + middle, sumh - middle);
            }
        }
    }
    int real_l = segtree.findi(0, N, 0, fake_l), real_r = segtree.findi(0, N, 0, fake_r - 1) + 1;
    int donel = real_m - real_l - segtree.query(0, N, 0, real_l, real_m), doner = real_r - real_m - 1 - segtree.query(0, N, 0, real_m + 1, real_r);
    int al = l - suml - donel;
    int ar = r - sumr - doner;
    if (al > 0){
        int ret = find(fake_l, fake_m, suml, r, al);
        if (ret != -1) return ret;
    }
    if (ar > 0){
        int ret = find(fake_m - al + 1, fake_r - al, l, sumr, ar);
        if (ret != -1) return ret;
    }
    return -1;
}

int find_best(int n){
    ans.resize(n);
    maxsum = 0;
    for (int i = 0; i * i < n; ++i) {
        auto ans = myask(i);
        int l = ans[0], r = ans[1];
        if (l + r > maxsum) maxsum = l + r;
    }
    segtree = Segtree (n);
    N = n;
    queried.clear();
    segtree.buildao(0, n, 0);
    int ret = -2;
    while (ret == -2){
        ret = find(0, segtree.cache[0], segtree.findi(0, N, 0, 0), N - segtree.findi(0, N, 0, segtree.cache[0] - 1) - 1, -1);
    }
    //assert(segtree.cache[0] + maxsum == N);
    return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8076 KB Output is correct
2 Correct 8 ms 8136 KB Output is correct
3 Correct 5 ms 8088 KB Output is correct
4 Correct 6 ms 8088 KB Output is correct
5 Correct 6 ms 8104 KB Output is correct
6 Correct 5 ms 8092 KB Output is correct
7 Correct 6 ms 8136 KB Output is correct
8 Correct 5 ms 8096 KB Output is correct
9 Correct 5 ms 8116 KB Output is correct
10 Correct 7 ms 8080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 8084 KB Output is correct
2 Correct 7 ms 8068 KB Output is correct
3 Correct 6 ms 8072 KB Output is correct
4 Correct 7 ms 8144 KB Output is correct
5 Correct 7 ms 8076 KB Output is correct
6 Correct 5 ms 8096 KB Output is correct
7 Correct 6 ms 8136 KB Output is correct
8 Correct 7 ms 8092 KB Output is correct
9 Correct 7 ms 8096 KB Output is correct
10 Correct 7 ms 8144 KB Output is correct
11 Correct 9 ms 8144 KB Output is correct
12 Correct 6 ms 8096 KB Output is correct
13 Correct 12 ms 8076 KB Output is correct
14 Correct 3 ms 1104 KB Output is correct
15 Correct 9 ms 8132 KB Output is correct
16 Correct 38 ms 8200 KB Output is correct
17 Correct 6 ms 8104 KB Output is correct
18 Correct 35 ms 8228 KB Output is correct
19 Correct 8 ms 8136 KB Output is correct
20 Correct 10 ms 4260 KB Output is correct
21 Correct 19 ms 8184 KB Output is correct
22 Correct 10 ms 8144 KB Output is correct
23 Correct 5 ms 8080 KB Output is correct
24 Correct 7 ms 8092 KB Output is correct
25 Correct 14 ms 8216 KB Output is correct
26 Correct 24 ms 8144 KB Output is correct
27 Correct 9 ms 8136 KB Output is correct
28 Correct 30 ms 8372 KB Output is correct
29 Correct 30 ms 8204 KB Output is correct
30 Correct 38 ms 8172 KB Output is correct
31 Correct 6 ms 8084 KB Output is correct
32 Correct 7 ms 8160 KB Output is correct
33 Correct 0 ms 208 KB Output is correct
34 Correct 15 ms 8168 KB Output is correct
35 Correct 7 ms 8072 KB Output is correct
36 Correct 11 ms 8140 KB Output is correct
37 Correct 6 ms 8092 KB Output is correct
38 Correct 7 ms 8096 KB Output is correct
39 Correct 24 ms 8080 KB Output is correct
40 Correct 23 ms 8384 KB Output is correct
41 Correct 21 ms 8212 KB Output is correct
42 Correct 29 ms 8148 KB Output is correct
43 Correct 24 ms 8184 KB Output is correct
44 Correct 15 ms 8188 KB Output is correct
45 Correct 13 ms 8104 KB Output is correct
46 Correct 7 ms 8264 KB Output is correct
47 Correct 12 ms 8160 KB Output is correct
48 Correct 35 ms 8264 KB Output is correct
49 Correct 7 ms 8156 KB Output is correct
50 Correct 44 ms 8264 KB Output is correct
51 Correct 24 ms 8392 KB Output is correct
52 Correct 9 ms 8140 KB Output is correct
53 Correct 7 ms 8148 KB Output is correct
54 Correct 21 ms 8168 KB Output is correct
55 Correct 6 ms 8092 KB Output is correct
56 Correct 33 ms 8352 KB Output is correct
57 Correct 36 ms 8216 KB Output is correct
58 Correct 37 ms 8204 KB Output is correct
59 Correct 30 ms 8184 KB Output is correct
60 Correct 21 ms 8228 KB Output is correct
61 Correct 6 ms 8164 KB Output is correct
62 Correct 6 ms 8256 KB Output is correct
63 Correct 8 ms 8068 KB Output is correct
64 Correct 6 ms 8136 KB Output is correct
65 Correct 8 ms 8100 KB Output is correct
66 Correct 10 ms 8088 KB Output is correct
67 Correct 13 ms 8140 KB Output is correct
68 Correct 7 ms 8088 KB Output is correct
69 Correct 7 ms 8164 KB Output is correct
70 Correct 7 ms 8092 KB Output is correct
71 Partially correct 47 ms 8180 KB Partially correct - number of queries: 5206
72 Correct 12 ms 8144 KB Output is correct
73 Partially correct 41 ms 8328 KB Partially correct - number of queries: 5143
74 Partially correct 30 ms 8508 KB Partially correct - number of queries: 5170
75 Correct 8 ms 8152 KB Output is correct
76 Correct 42 ms 8348 KB Output is correct
77 Correct 50 ms 8148 KB Output is correct
78 Correct 11 ms 8144 KB Output is correct
79 Correct 28 ms 8144 KB Output is correct
80 Correct 49 ms 8180 KB Output is correct
81 Correct 50 ms 8300 KB Output is correct
82 Correct 31 ms 8264 KB Output is correct
83 Correct 8 ms 8144 KB Output is correct
84 Correct 41 ms 8156 KB Output is correct
85 Correct 45 ms 8280 KB Output is correct
86 Correct 9 ms 8144 KB Output is correct
87 Correct 10 ms 8188 KB Output is correct
88 Correct 9 ms 8152 KB Output is correct
89 Correct 7 ms 8100 KB Output is correct
90 Correct 9 ms 8080 KB Output is correct
91 Correct 6 ms 8084 KB Output is correct
92 Correct 8 ms 8144 KB Output is correct
93 Correct 11 ms 8144 KB Output is correct
94 Correct 11 ms 8164 KB Output is correct
95 Correct 10 ms 8088 KB Output is correct
96 Correct 10 ms 8144 KB Output is correct
97 Correct 7 ms 8144 KB Output is correct