답안 #820599

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
820599 2023-08-11T04:57:33 Z dashka Abracadabra (CEOI22_abracadabra) C++14
100 / 100
672 ms 47188 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 'std::pair<Node*, Node*> splitbyv(Node*, int)':
Main.cpp:102:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  102 |         auto [rl, rr] = splitbyv(a->r, v);
      |              ^
Main.cpp:107:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  107 |         auto [ll, lr] = splitbyv(a->l, v);
      |              ^
Main.cpp: In function 'Node* merge(Node*, Node*)':
Main.cpp:118:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  118 |     auto[l, r] = splitbyv(b, a->v);
      |         ^
Main.cpp: In function 'std::array<Node*, 3> splitons(Node*, int)':
Main.cpp:129:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  129 |         auto[l, m, r] = splitons(a->l, s);
      |             ^
Main.cpp:134:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  134 |         auto[l, m, r] = splitons(a->r, s - size(a->l) - a->e + a->b);
      |             ^
Main.cpp: In function 'void solve()':
Main.cpp:205:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  205 |             auto[l, m, r] = splitons(tr, n/2);
      |                 ^
Main.cpp:193:9: warning: unused variable 'i' [-Wunused-variable]
  193 |     int i = 0;
      |         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 325 ms 27568 KB Output is correct
2 Correct 336 ms 27252 KB Output is correct
3 Correct 312 ms 26316 KB Output is correct
4 Correct 254 ms 25112 KB Output is correct
5 Correct 295 ms 26796 KB Output is correct
6 Correct 265 ms 25420 KB Output is correct
7 Correct 291 ms 27016 KB Output is correct
8 Correct 303 ms 25472 KB Output is correct
9 Correct 271 ms 25208 KB Output is correct
10 Correct 273 ms 25696 KB Output is correct
11 Correct 297 ms 25600 KB Output is correct
12 Correct 238 ms 24456 KB Output is correct
13 Correct 266 ms 25440 KB Output is correct
14 Correct 291 ms 26172 KB Output is correct
15 Correct 282 ms 26144 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 256 ms 24920 KB Output is correct
18 Correct 230 ms 24652 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 351 ms 45608 KB Output is correct
2 Correct 357 ms 46704 KB Output is correct
3 Correct 274 ms 36920 KB Output is correct
4 Correct 234 ms 31608 KB Output is correct
5 Correct 259 ms 32940 KB Output is correct
6 Correct 229 ms 30552 KB Output is correct
7 Correct 307 ms 38292 KB Output is correct
8 Correct 286 ms 37036 KB Output is correct
9 Correct 238 ms 32240 KB Output is correct
10 Correct 255 ms 34876 KB Output is correct
11 Correct 208 ms 28748 KB Output is correct
12 Correct 209 ms 28804 KB Output is correct
13 Correct 274 ms 34000 KB Output is correct
14 Correct 208 ms 29740 KB Output is correct
15 Correct 252 ms 35648 KB Output is correct
16 Correct 16 ms 3156 KB Output is correct
17 Correct 319 ms 41924 KB Output is correct
18 Correct 175 ms 26428 KB Output is correct
19 Correct 56 ms 9028 KB Output is correct
20 Correct 67 ms 10652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 9644 KB Output is correct
2 Correct 81 ms 10036 KB Output is correct
3 Correct 85 ms 9476 KB Output is correct
4 Correct 42 ms 5204 KB Output is correct
5 Correct 62 ms 7008 KB Output is correct
6 Correct 44 ms 5280 KB Output is correct
7 Correct 56 ms 6356 KB Output is correct
8 Correct 54 ms 5880 KB Output is correct
9 Correct 59 ms 6392 KB Output is correct
10 Correct 44 ms 4688 KB Output is correct
11 Correct 40 ms 4896 KB Output is correct
12 Correct 37 ms 4676 KB Output is correct
13 Correct 39 ms 4784 KB Output is correct
14 Correct 39 ms 4964 KB Output is correct
15 Correct 36 ms 4640 KB Output is correct
16 Correct 8 ms 1772 KB Output is correct
17 Correct 55 ms 9292 KB Output is correct
18 Correct 28 ms 4328 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 325 ms 27568 KB Output is correct
2 Correct 336 ms 27252 KB Output is correct
3 Correct 312 ms 26316 KB Output is correct
4 Correct 254 ms 25112 KB Output is correct
5 Correct 295 ms 26796 KB Output is correct
6 Correct 265 ms 25420 KB Output is correct
7 Correct 291 ms 27016 KB Output is correct
8 Correct 303 ms 25472 KB Output is correct
9 Correct 271 ms 25208 KB Output is correct
10 Correct 273 ms 25696 KB Output is correct
11 Correct 297 ms 25600 KB Output is correct
12 Correct 238 ms 24456 KB Output is correct
13 Correct 266 ms 25440 KB Output is correct
14 Correct 291 ms 26172 KB Output is correct
15 Correct 282 ms 26144 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 256 ms 24920 KB Output is correct
18 Correct 230 ms 24652 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 351 ms 45608 KB Output is correct
22 Correct 357 ms 46704 KB Output is correct
23 Correct 274 ms 36920 KB Output is correct
24 Correct 234 ms 31608 KB Output is correct
25 Correct 259 ms 32940 KB Output is correct
26 Correct 229 ms 30552 KB Output is correct
27 Correct 307 ms 38292 KB Output is correct
28 Correct 286 ms 37036 KB Output is correct
29 Correct 238 ms 32240 KB Output is correct
30 Correct 255 ms 34876 KB Output is correct
31 Correct 208 ms 28748 KB Output is correct
32 Correct 209 ms 28804 KB Output is correct
33 Correct 274 ms 34000 KB Output is correct
34 Correct 208 ms 29740 KB Output is correct
35 Correct 252 ms 35648 KB Output is correct
36 Correct 16 ms 3156 KB Output is correct
37 Correct 319 ms 41924 KB Output is correct
38 Correct 175 ms 26428 KB Output is correct
39 Correct 56 ms 9028 KB Output is correct
40 Correct 67 ms 10652 KB Output is correct
41 Correct 88 ms 9644 KB Output is correct
42 Correct 81 ms 10036 KB Output is correct
43 Correct 85 ms 9476 KB Output is correct
44 Correct 42 ms 5204 KB Output is correct
45 Correct 62 ms 7008 KB Output is correct
46 Correct 44 ms 5280 KB Output is correct
47 Correct 56 ms 6356 KB Output is correct
48 Correct 54 ms 5880 KB Output is correct
49 Correct 59 ms 6392 KB Output is correct
50 Correct 44 ms 4688 KB Output is correct
51 Correct 40 ms 4896 KB Output is correct
52 Correct 37 ms 4676 KB Output is correct
53 Correct 39 ms 4784 KB Output is correct
54 Correct 39 ms 4964 KB Output is correct
55 Correct 36 ms 4640 KB Output is correct
56 Correct 8 ms 1772 KB Output is correct
57 Correct 55 ms 9292 KB Output is correct
58 Correct 28 ms 4328 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 0 ms 332 KB Output is correct
61 Correct 607 ms 47184 KB Output is correct
62 Correct 672 ms 47188 KB Output is correct
63 Correct 667 ms 45372 KB Output is correct
64 Correct 402 ms 37864 KB Output is correct
65 Correct 430 ms 39700 KB Output is correct
66 Correct 408 ms 37828 KB Output is correct
67 Correct 389 ms 37956 KB Output is correct
68 Correct 423 ms 37132 KB Output is correct
69 Correct 423 ms 37424 KB Output is correct
70 Correct 376 ms 35460 KB Output is correct
71 Correct 359 ms 34204 KB Output is correct
72 Correct 354 ms 35048 KB Output is correct
73 Correct 364 ms 34792 KB Output is correct
74 Correct 373 ms 35872 KB Output is correct
75 Correct 366 ms 35864 KB Output is correct
76 Correct 16 ms 3028 KB Output is correct
77 Correct 517 ms 42120 KB Output is correct
78 Correct 261 ms 32188 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct