답안 #807647

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
807647 2023-08-04T21:05:59 Z HaroldVemeno Abracadabra (CEOI22_abracadabra) C++17
100 / 100
660 ms 47288 KB
#include <bits/stdc++.h>

#ifdef GUDEB
    #define D(x) cerr << #x << ": " << (x) << '\n';
    #define ifdeb if(true)
#else
    #define D(x) ;
    #define ifdeb if(false)
#endif

#define all(x) begin(x), end(x)

using namespace std;
using ull = unsigned long long;
using ll = long long;
// #define int ll;

int as[200000];
int nbe[200000];

struct Node {
    int b;
    int e;
    int v;
    int s = e - b;
    int p = rand();
    Node* l = nullptr;
    Node* r = nullptr;

    void print(ostream& o, string bef = string{}, int pos = 0) const {
        if(l == nullptr) {
            o << bef;
            if(pos == -1) o << "  ";
            if(pos == 1)  o << "| ";
            o << ",-o\n";
        } else {
            if(pos == -1) l->print(o, bef + "  ", -1);
            if(pos == 1)  l->print(o, bef + "| ", -1);
            if(pos == 0)  l->print(o, bef, -1);
        }
        o << bef;
        if(pos == -1) o << ",-";
        if(pos == 1)  o << "`-";
        o << "(v: " << v << ", p: " << p << ", s:" << s << ", b: " << b << ", e: " << e << ")\n";
        o << bef;
        if(pos == -1) o << "| | ";
        if(pos == 1)  o << "  | ";
        for(int i = b; i < e; ++i) o << as[i] << ' ';
        o << '\n';
        if(r == nullptr) {
            o << bef;
            if(pos == -1) o << "| ";
            if(pos == 1)  o << "  ";
            o << "`-o\n";
        } else {
            if(pos == -1) r->print(o, bef + "| ", 1);
            if(pos == 1)  r->print(o, bef + "  ", 1);
            if(pos == 0)  r->print(o, bef, 1);
        }

    }

    friend ostream& operator<< (ostream& o, const Node* a) {
        o << '\n';
        if(!a) o << "o\n";
        else a->print(o);
        return o;
    }

};


int size(Node* a) {
    return a?a->s:0;
}

void pull(Node* a) {
    if(a) {
        a->s = a->e - a->b;
        a->s += size(a->r);
        a->s += size(a->l);
    }
}

Node* concat(Node* a, Node* b) {
    if(!a) return b;
    if(!b) return a;
    if(a->p > b->p) {
        a->r = concat(a->r, b);
        pull(a);
        return a;
    } else {
        b->l = concat(a, b->l);
        pull(b);
        return b;
    }
}

pair<Node*, Node*> splitbyv(Node* a, int v) {
    if(!a) return {a, a};
    if(a->v < v) {
        auto [rl, rr] = splitbyv(a->r, v);
        a->r = rl;
        pull(a);
        return {a, rr};
    } else {
        auto [ll, lr] = splitbyv(a->l, v);
        a->l = lr;
        pull(a);
        return {ll, a};
    }
}

Node* merge(Node* a, Node* b) {
    if(!a) return b;
    if(!b) return a;
    if(a->p < b->p) swap(a, b);
    auto[l, r] = splitbyv(b, a->v);
    a->l = merge(a->l, l);
    a->r = merge(a->r, r);
    pull(a);
    return a;

}

array<Node*, 3> splitons(Node* a, int s) {
    if(!a) return {a, a, a};
    if( s <= size(a->l) ) {
        auto[l, m, r] = splitons(a->l, s);
        a->l = r;
        pull(a);
        return {l, m, a};
    } else if(s >= size(a->l) + a->e - a->b ) {
        auto[l, m, r] = splitons(a->r, s - size(a->l) - a->e + a->b);
        a->r = l;
        pull(a);
        return {a, m, r};
    } else {
        Node* l = a->l;
        Node* r = a->r;
        a->l = nullptr;
        a->r = nullptr;
        pull(a);
        return {l, a, r};
    }
}


int valuebys(Node* a, int s) {
    D(size(a))
    if(s < size(a->l)) {
        return valuebys(a->l, s);
    } else if( size(a->l) + a->e - a->b <= s) {
        return valuebys(a->r, s - size(a->l) - a->e + a->b);
    } else {
        return as[a->b + s - size(a->l)];
    }
}

struct Q {
    int t;
    int ind;
    int i;
    int res;
};

void solve() {
    int n, q;
    cin >> n >> q;

    vector<Q> qs(q);
    for(int i = 0; i < n; ++i) {
        cin >> as[i];
    }
    for(int i = 0; i < q; ++i) {
        cin >> qs[i].t >> qs[i].ind;
        --qs[i].ind;
        qs[i].i = i;
    }
    sort(all(qs), [](Q a, Q b){return a.t < b.t;});

    // value -> index
    vector<int> st;
    for(int i = n-1; i >= 0; --i) {
        while(!st.empty() && as[st.back()] < as[i]) {
            st.pop_back();
        }
        if(st.empty()) nbe[i] = n;
        else nbe[i] = st.back();
        st.push_back(i);
    }

    int i = 0;
    int t = 0;

    Node* tr = nullptr;
    for(int i = 0; i < n; i = nbe[i]) {
        tr = concat(tr, new Node{i, nbe[i], as[i]});
    }
    D(tr)
    bool changed = true;

    for(int i = 0; i < q; ++i) {
        while(t < qs[i].t && changed) {
            auto[l, m, r] = splitons(tr, n/2);
            D(l)
            D(m)
            D(r)
            if(m == nullptr) {
                changed = false;
                tr = concat(l, r);
                break;
            }
            pull(m);
            Node* mrg = nullptr;
            for(int i = m->b + n/2 - size(l); i < m->e; i = nbe[i]) {
                mrg = concat(mrg, new Node{i, min(nbe[i], m->e), as[i]});
            }
            m->e = m->b + n/2 - size(l);
            pull(m);
            l = merge(l, mrg);
            tr = concat(l, concat(m, r));
            D(tr)
            assert(size(tr) == n);
            ++t;
        }
        qs[i].res = valuebys(tr, qs[i].ind);
    }


    sort(all(qs), [](Q a, Q b){return a.i < b.i;});
    for(auto q : qs) cout << q.res << '\n';
    return;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << setprecision(20);

    solve();
}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:193:9: warning: unused variable 'i' [-Wunused-variable]
  193 |     int i = 0;
      |         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 312 ms 19916 KB Output is correct
2 Correct 329 ms 27232 KB Output is correct
3 Correct 308 ms 26336 KB Output is correct
4 Correct 252 ms 25092 KB Output is correct
5 Correct 300 ms 26872 KB Output is correct
6 Correct 273 ms 25400 KB Output is correct
7 Correct 293 ms 27068 KB Output is correct
8 Correct 266 ms 25516 KB Output is correct
9 Correct 270 ms 25256 KB Output is correct
10 Correct 275 ms 25604 KB Output is correct
11 Correct 292 ms 25572 KB Output is correct
12 Correct 234 ms 24472 KB Output is correct
13 Correct 259 ms 25412 KB Output is correct
14 Correct 285 ms 26148 KB Output is correct
15 Correct 278 ms 26144 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 238 ms 24904 KB Output is correct
18 Correct 240 ms 24612 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 353 ms 31292 KB Output is correct
2 Correct 347 ms 46656 KB Output is correct
3 Correct 275 ms 36940 KB Output is correct
4 Correct 252 ms 31604 KB Output is correct
5 Correct 248 ms 32960 KB Output is correct
6 Correct 231 ms 30508 KB Output is correct
7 Correct 294 ms 38340 KB Output is correct
8 Correct 272 ms 36988 KB Output is correct
9 Correct 243 ms 32312 KB Output is correct
10 Correct 248 ms 34896 KB Output is correct
11 Correct 219 ms 28664 KB Output is correct
12 Correct 203 ms 28840 KB Output is correct
13 Correct 243 ms 33968 KB Output is correct
14 Correct 206 ms 29732 KB Output is correct
15 Correct 249 ms 35740 KB Output is correct
16 Correct 16 ms 3156 KB Output is correct
17 Correct 301 ms 41900 KB Output is correct
18 Correct 191 ms 26444 KB Output is correct
19 Correct 56 ms 8980 KB Output is correct
20 Correct 77 ms 10700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 7920 KB Output is correct
2 Correct 81 ms 9928 KB Output is correct
3 Correct 85 ms 9508 KB Output is correct
4 Correct 50 ms 5196 KB Output is correct
5 Correct 61 ms 7072 KB Output is correct
6 Correct 47 ms 5324 KB Output is correct
7 Correct 55 ms 6352 KB Output is correct
8 Correct 51 ms 5856 KB Output is correct
9 Correct 57 ms 6424 KB Output is correct
10 Correct 38 ms 4684 KB Output is correct
11 Correct 39 ms 4912 KB Output is correct
12 Correct 40 ms 4624 KB Output is correct
13 Correct 39 ms 4900 KB Output is correct
14 Correct 39 ms 4800 KB Output is correct
15 Correct 43 ms 4684 KB Output is correct
16 Correct 8 ms 1744 KB Output is correct
17 Correct 56 ms 9264 KB Output is correct
18 Correct 33 ms 4416 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 312 ms 19916 KB Output is correct
2 Correct 329 ms 27232 KB Output is correct
3 Correct 308 ms 26336 KB Output is correct
4 Correct 252 ms 25092 KB Output is correct
5 Correct 300 ms 26872 KB Output is correct
6 Correct 273 ms 25400 KB Output is correct
7 Correct 293 ms 27068 KB Output is correct
8 Correct 266 ms 25516 KB Output is correct
9 Correct 270 ms 25256 KB Output is correct
10 Correct 275 ms 25604 KB Output is correct
11 Correct 292 ms 25572 KB Output is correct
12 Correct 234 ms 24472 KB Output is correct
13 Correct 259 ms 25412 KB Output is correct
14 Correct 285 ms 26148 KB Output is correct
15 Correct 278 ms 26144 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 238 ms 24904 KB Output is correct
18 Correct 240 ms 24612 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 328 KB Output is correct
21 Correct 353 ms 31292 KB Output is correct
22 Correct 347 ms 46656 KB Output is correct
23 Correct 275 ms 36940 KB Output is correct
24 Correct 252 ms 31604 KB Output is correct
25 Correct 248 ms 32960 KB Output is correct
26 Correct 231 ms 30508 KB Output is correct
27 Correct 294 ms 38340 KB Output is correct
28 Correct 272 ms 36988 KB Output is correct
29 Correct 243 ms 32312 KB Output is correct
30 Correct 248 ms 34896 KB Output is correct
31 Correct 219 ms 28664 KB Output is correct
32 Correct 203 ms 28840 KB Output is correct
33 Correct 243 ms 33968 KB Output is correct
34 Correct 206 ms 29732 KB Output is correct
35 Correct 249 ms 35740 KB Output is correct
36 Correct 16 ms 3156 KB Output is correct
37 Correct 301 ms 41900 KB Output is correct
38 Correct 191 ms 26444 KB Output is correct
39 Correct 56 ms 8980 KB Output is correct
40 Correct 77 ms 10700 KB Output is correct
41 Correct 87 ms 7920 KB Output is correct
42 Correct 81 ms 9928 KB Output is correct
43 Correct 85 ms 9508 KB Output is correct
44 Correct 50 ms 5196 KB Output is correct
45 Correct 61 ms 7072 KB Output is correct
46 Correct 47 ms 5324 KB Output is correct
47 Correct 55 ms 6352 KB Output is correct
48 Correct 51 ms 5856 KB Output is correct
49 Correct 57 ms 6424 KB Output is correct
50 Correct 38 ms 4684 KB Output is correct
51 Correct 39 ms 4912 KB Output is correct
52 Correct 40 ms 4624 KB Output is correct
53 Correct 39 ms 4900 KB Output is correct
54 Correct 39 ms 4800 KB Output is correct
55 Correct 43 ms 4684 KB Output is correct
56 Correct 8 ms 1744 KB Output is correct
57 Correct 56 ms 9264 KB Output is correct
58 Correct 33 ms 4416 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 602 ms 47176 KB Output is correct
62 Correct 660 ms 47288 KB Output is correct
63 Correct 620 ms 45264 KB Output is correct
64 Correct 406 ms 37888 KB Output is correct
65 Correct 453 ms 39660 KB Output is correct
66 Correct 421 ms 37848 KB Output is correct
67 Correct 394 ms 37872 KB Output is correct
68 Correct 395 ms 37056 KB Output is correct
69 Correct 416 ms 37364 KB Output is correct
70 Correct 374 ms 35404 KB Output is correct
71 Correct 339 ms 34204 KB Output is correct
72 Correct 357 ms 35124 KB Output is correct
73 Correct 350 ms 34764 KB Output is correct
74 Correct 357 ms 35916 KB Output is correct
75 Correct 368 ms 35868 KB Output is correct
76 Correct 16 ms 3156 KB Output is correct
77 Correct 502 ms 42100 KB Output is correct
78 Correct 249 ms 32176 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 0 ms 212 KB Output is correct