답안 #965422

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
965422 2024-04-18T13:48:48 Z efedmrlr Abracadabra (CEOI22_abracadabra) C++17
100 / 100
647 ms 57028 KB
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>

using namespace std;


#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()


void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}


const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 3e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n,m,q;

struct SegT {
    vector<int> data;
    int sz;
    void reset(int s) {
        sz = s;
        data.assign(4*(sz + 3), 0);
    }
    void update(int tl, int tr, int v, int ind, int val) {
        if(tl == tr) {
            data[v] += val;
            return;
        }
        int tm = (tl + tr) >> 1;
        if(ind <= tm) {
            update(tl, tm, v << 1, ind, val);
        }
        else {
            update(tm + 1, tr, v << 1 ^ 1, ind, val);
        }
        data[v] = data[v << 1] + data[v << 1 ^ 1];
    }
    void update(int ind, int val) {
        update(0, sz, 1, ind, val);
    }
    int query(int tl, int tr, int v, int val) {
        if(tl == tr) {
            return tl;
        }   
        int tm = (tl + tr) >> 1;
        if(val <= data[v << 1]) {
            return query(tl, tm, v << 1, val);
        } 
        else {
            return query(tm + 1, tr, v << 1 ^ 1, val - data[v << 1]);
        }
    }
    int query(int val) {
        return query(0, sz, 1, val);
    }
    int query2(int tl, int tr, int v, int l, int r) {
        if(tl >= l && tr <= r) {
            return data[v];
        }
        if(tl > r || tr < l) {
            return 0;
        }
        int tm = (tl + tr) >> 1;
        return query2(tl, tm, v << 1, l, r) + query2(tm + 1, tr, v << 1 ^ 1, l, r);
    }
    int query2(int l, int r) {
        return query2(0, sz, 1, l, r);
    }
};  


vector<int> a, nxt;
vector<array<int,3> > comp;
set<array<int,3> > st;
vector<int> fin;
vector<array<int,3> > qu;
vector<int> res;
int tot = 0;
void calcn() {
    a[n + 1] = INF;
    vector<array<int,2> > tmp;
    for(int i = 1; i<=n + 1; i++) {
        while(tmp.size() && tmp.back()[0] < a[i]) {
            nxt[tmp.back()[1]] = i;
            tmp.pop_back();
        }
        tmp.pb({a[i], i});
    }

}   
int itr;
int t = 0;
SegT dt;
void get_fin(array<int,3> bl) {
    // cout << bl[0] << " " << bl[1] << " " << bl[2] << endl;
    for(int i = bl[2]; i >= bl[1]; i--) {
        fin[itr--] = a[i]; 
    }
}
void prt() {
    for(auto c : st) {
        cout << c[0] << " " << c[1] << " " << c[2] << "\n";
    }
}
void get_comp() {
    for(int l = 1; l <= n / 2;) {
        int r = min(n / 2, nxt[l] - 1);
        st.insert({a[l], l, r});
        comp.pb({a[l], l, r});
        tot += r - l + 1;
        l = nxt[l];
    }
    for(int l = n / 2 + 1; l <= n;) {
        int r = nxt[l] - 1;
        st.insert({a[l], l, r});
        comp.pb({a[l], l, r});
        tot += r - l + 1;
        l = nxt[l];
    }
    t++;
    while(1) {
        // prt();
        // cout << "round" << t << endl;
        // cout << "tot:" << tot << "\n";

        if(st.empty()) break;
        if(t == qu[q - 1][0]) break;
        auto last = st.end(); last--;
        while(tot - ((*last)[2] - (*last)[1] + 1) >= n / 2) {
            tot -= ((*last)[2] - (*last)[1] + 1);
            st.erase(last);            
            last = st.end(); last--;
        }
        auto el = *last;
        if(tot == n / 2) break;
        int len = el[2] - el[1] + 1;
        tot -= ((*last)[2] - (*last)[1] + 1);
        st.erase(last); 
        int ext = tot + len - n / 2;

        st.insert({el[0], el[1], el[2] - ext});
        comp.pb({el[0], el[1], el[2] - ext});
        tot += el[2] - ext - el[1] + 1;
        for(int l = el[2] - ext + 1; l <= el[2];) {
            int r = min(el[2], nxt[l] - 1);
            st.insert({a[l], l, r});
            tot += r - l + 1;
            comp.pb({a[l], l, r});
            l = nxt[l];
        } 
        t++;
    }
    // while(st.size()) { // TODO
    //     auto x = prev(st.end());
    //     get_fin(*x);
    //     st.erase(x);
    // }
    sort(all(comp));
    dt.reset((int)comp.size() + 5);
}
int ind = 0;
void go() {
    st.clear();
    tot = 0;
    for(; ind < q && qu[ind][0] == 0; ind++) {
        res[qu[ind][2]] = a[qu[ind][1]];
    }
    for(int l = 1; l <= n / 2;) {
        int r = min(n / 2, nxt[l] - 1);
        st.insert({a[l], l, r});
        tot += r - l + 1;
        dt.update((int)(lower_bound(all(comp), array<int,3>({a[l], l, r})) - comp.begin()), r - l + 1);
        l = nxt[l];
    }
    for(int l = n / 2 + 1; l <= n;) {
        int r = nxt[l] - 1;
        st.insert({a[l], l, r});
        tot += r - l + 1;
        dt.update((int)(lower_bound(all(comp), array<int,3>({a[l], l, r})) - comp.begin()), r - l + 1);
        l = nxt[l];
    }
    t++;
    while(1) {
        for(; ind < q && qu[ind][0] == t; ind++) {
            if(qu[ind][1] > itr) {
                res[qu[ind][2]] = fin[qu[ind][1]];
            }
            else {
                // cout << "que:" << qu[ind][0] << " " << qu[ind][1] << " " << qu[ind][2] << "\n";
                int cur = dt.query(qu[ind][1]);
                // cout << comp[cur][0] << " " << comp[cur][1] << " " << comp[cur][2] << "\n";
                int len = dt.query2(0, cur);
                // cout << "len:" << len << "\n";
                int fir = qu[ind][1] - len + comp[cur][2];
                // cout << "ind:" << qu[ind][1] - fir << "\n";

                res[qu[ind][2]] = a[fir]; 
            }
        }
        // prt();
        // cout << "round" << t << endl;
        // cout << "tot:" << tot << "\n";

        if(st.empty()) break;
        auto last = st.end(); last--;
        while(tot - ((*last)[2] - (*last)[1] + 1) >= n / 2) {
            tot -= ((*last)[2] - (*last)[1] + 1);
            dt.update(lower_bound(all(comp), *last) - comp.begin(), -((*last)[2] - (*last)[1] + 1));
            get_fin(*last);  // TODO
            
            st.erase(last);            
            assert(st.size() != 0);
            last = st.end(); last--;
        }
        auto el = *last;
        if(tot == n / 2) break;
        int len = el[2] - el[1] + 1;
        tot -= ((*last)[2] - (*last)[1] + 1);
        dt.update(lower_bound(all(comp), *last) - comp.begin(), -((*last)[2] - (*last)[1] + 1));
        st.erase(last); 
        int ext = tot + len - n / 2;

        st.insert({el[0], el[1], el[2] - ext});
        tot += el[2] - ext - el[1] + 1;
        dt.update(lower_bound(all(comp), array<int,3>({el[0], el[1], el[2] - ext})) - comp.begin(), el[2] - ext - el[1] + 1);
        for(int l = el[2] - ext + 1; l <= el[2];) {
            int r = min(el[2], nxt[l] - 1);
            st.insert({a[l], l, r});
            tot += r - l + 1;
            dt.update(lower_bound(all(comp), array<int,3>({a[l], l, r})) - comp.begin(), r - l + 1);
            l = nxt[l];
        } 
        t++;
    }
    while(st.size()) { // TODO
        auto x = prev(st.end());
        get_fin(*x);
        st.erase(x);
    }
    for(; ind < q; ind++) {
        res[qu[ind][2]] = fin[qu[ind][1]];
    }
}
inline void solve() {
    cin>>n>>q;
    a.resize(n + 2);
    nxt.resize(n + 1);
    fin.assign(n + 1, 0);
    qu.resize(q);
    itr = n;
    for(int i = 1; i<=n; i++) {
        cin >> a[i];
    }
    REP(i, q) {
        cin >> qu[i][0] >> qu[i][1];
        qu[i][2] = i;
    }
    sort(all(qu));
    calcn();
    get_comp();
    itr = n;
    res.assign(q, -1);
    t = 0;
    go();
    REP(i, q) cout << res[i] << "\n";
}
 
signed main() {

    fastio();
    int test = 1;
    //cin>>test;
    while(test--) {
        solve();
    }
    
}
# 결과 실행 시간 메모리 Grader output
1 Correct 314 ms 27540 KB Output is correct
2 Correct 317 ms 27736 KB Output is correct
3 Correct 316 ms 26452 KB Output is correct
4 Correct 294 ms 25084 KB Output is correct
5 Correct 301 ms 26860 KB Output is correct
6 Correct 285 ms 25364 KB Output is correct
7 Correct 316 ms 27032 KB Output is correct
8 Correct 293 ms 25612 KB Output is correct
9 Correct 305 ms 25324 KB Output is correct
10 Correct 292 ms 25648 KB Output is correct
11 Correct 306 ms 25636 KB Output is correct
12 Correct 305 ms 24536 KB Output is correct
13 Correct 302 ms 25480 KB Output is correct
14 Correct 327 ms 26192 KB Output is correct
15 Correct 312 ms 26192 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 289 ms 24936 KB Output is correct
18 Correct 282 ms 24660 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 495 ms 55220 KB Output is correct
2 Correct 466 ms 53648 KB Output is correct
3 Correct 455 ms 42012 KB Output is correct
4 Correct 344 ms 35768 KB Output is correct
5 Correct 365 ms 37640 KB Output is correct
6 Correct 324 ms 34748 KB Output is correct
7 Correct 346 ms 40748 KB Output is correct
8 Correct 337 ms 39948 KB Output is correct
9 Correct 342 ms 35836 KB Output is correct
10 Correct 304 ms 36080 KB Output is correct
11 Correct 272 ms 29780 KB Output is correct
12 Correct 295 ms 30400 KB Output is correct
13 Correct 278 ms 35060 KB Output is correct
14 Correct 292 ms 31256 KB Output is correct
15 Correct 302 ms 36360 KB Output is correct
16 Correct 22 ms 4180 KB Output is correct
17 Correct 355 ms 50436 KB Output is correct
18 Correct 253 ms 27652 KB Output is correct
19 Correct 58 ms 10064 KB Output is correct
20 Correct 68 ms 11600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 141 ms 14656 KB Output is correct
2 Correct 123 ms 13492 KB Output is correct
3 Correct 140 ms 12324 KB Output is correct
4 Correct 56 ms 6540 KB Output is correct
5 Correct 125 ms 10580 KB Output is correct
6 Correct 56 ms 6800 KB Output is correct
7 Correct 86 ms 8968 KB Output is correct
8 Correct 70 ms 7944 KB Output is correct
9 Correct 90 ms 9220 KB Output is correct
10 Correct 44 ms 5196 KB Output is correct
11 Correct 43 ms 5460 KB Output is correct
12 Correct 38 ms 5212 KB Output is correct
13 Correct 43 ms 5612 KB Output is correct
14 Correct 42 ms 5460 KB Output is correct
15 Correct 38 ms 5208 KB Output is correct
16 Correct 10 ms 2392 KB Output is correct
17 Correct 88 ms 13576 KB Output is correct
18 Correct 34 ms 4940 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 314 ms 27540 KB Output is correct
2 Correct 317 ms 27736 KB Output is correct
3 Correct 316 ms 26452 KB Output is correct
4 Correct 294 ms 25084 KB Output is correct
5 Correct 301 ms 26860 KB Output is correct
6 Correct 285 ms 25364 KB Output is correct
7 Correct 316 ms 27032 KB Output is correct
8 Correct 293 ms 25612 KB Output is correct
9 Correct 305 ms 25324 KB Output is correct
10 Correct 292 ms 25648 KB Output is correct
11 Correct 306 ms 25636 KB Output is correct
12 Correct 305 ms 24536 KB Output is correct
13 Correct 302 ms 25480 KB Output is correct
14 Correct 327 ms 26192 KB Output is correct
15 Correct 312 ms 26192 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 289 ms 24936 KB Output is correct
18 Correct 282 ms 24660 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 495 ms 55220 KB Output is correct
22 Correct 466 ms 53648 KB Output is correct
23 Correct 455 ms 42012 KB Output is correct
24 Correct 344 ms 35768 KB Output is correct
25 Correct 365 ms 37640 KB Output is correct
26 Correct 324 ms 34748 KB Output is correct
27 Correct 346 ms 40748 KB Output is correct
28 Correct 337 ms 39948 KB Output is correct
29 Correct 342 ms 35836 KB Output is correct
30 Correct 304 ms 36080 KB Output is correct
31 Correct 272 ms 29780 KB Output is correct
32 Correct 295 ms 30400 KB Output is correct
33 Correct 278 ms 35060 KB Output is correct
34 Correct 292 ms 31256 KB Output is correct
35 Correct 302 ms 36360 KB Output is correct
36 Correct 22 ms 4180 KB Output is correct
37 Correct 355 ms 50436 KB Output is correct
38 Correct 253 ms 27652 KB Output is correct
39 Correct 58 ms 10064 KB Output is correct
40 Correct 68 ms 11600 KB Output is correct
41 Correct 141 ms 14656 KB Output is correct
42 Correct 123 ms 13492 KB Output is correct
43 Correct 140 ms 12324 KB Output is correct
44 Correct 56 ms 6540 KB Output is correct
45 Correct 125 ms 10580 KB Output is correct
46 Correct 56 ms 6800 KB Output is correct
47 Correct 86 ms 8968 KB Output is correct
48 Correct 70 ms 7944 KB Output is correct
49 Correct 90 ms 9220 KB Output is correct
50 Correct 44 ms 5196 KB Output is correct
51 Correct 43 ms 5460 KB Output is correct
52 Correct 38 ms 5212 KB Output is correct
53 Correct 43 ms 5612 KB Output is correct
54 Correct 42 ms 5460 KB Output is correct
55 Correct 38 ms 5208 KB Output is correct
56 Correct 10 ms 2392 KB Output is correct
57 Correct 88 ms 13576 KB Output is correct
58 Correct 34 ms 4940 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 622 ms 57028 KB Output is correct
62 Correct 598 ms 54896 KB Output is correct
63 Correct 647 ms 50932 KB Output is correct
64 Correct 453 ms 42244 KB Output is correct
65 Correct 478 ms 44508 KB Output is correct
66 Correct 444 ms 42248 KB Output is correct
67 Correct 413 ms 40320 KB Output is correct
68 Correct 430 ms 40204 KB Output is correct
69 Correct 447 ms 40980 KB Output is correct
70 Correct 383 ms 36688 KB Output is correct
71 Correct 339 ms 35152 KB Output is correct
72 Correct 367 ms 36632 KB Output is correct
73 Correct 350 ms 35672 KB Output is correct
74 Correct 373 ms 37208 KB Output is correct
75 Correct 365 ms 37168 KB Output is correct
76 Correct 18 ms 3932 KB Output is correct
77 Correct 441 ms 50956 KB Output is correct
78 Correct 314 ms 33248 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 0 ms 348 KB Output is correct