답안 #923114

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
923114 2024-02-06T16:51:40 Z GrindMachine Abracadabra (CEOI22_abracadabra) C++17
100 / 100
694 ms 66332 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

read the edi a long time ago and remember some ideas from there

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct fenwick {
    int siz;
    vector<T> tree;

    fenwick() {

    }

    fenwick(int n) {
        siz = n;
        tree = vector<T>(n + 1);
    }

    int lsb(int x) {
        return x & -x;
    }

    void build(vector<T> &a, int n) {
        for (int i = 1; i <= n; ++i) {
            int par = i + lsb(i);
            tree[i] += a[i];

            if (par <= siz) {
                tree[par] += tree[i];
            }
        }
    }

    void pupd(int i, T v) {
        while (i <= siz) {
            tree[i] += v;
            i += lsb(i);
        }
    }

    T sum(int i) {
        T res = 0;

        while (i) {
            res += tree[i];
            i -= lsb(i);
        }

        return res;
    }

    T query(int l, int r) {
        if (l > r) return 0;
        T res = sum(r) - sum(l - 1);
        return res;
    }
};

void solve(int test_case)
{
    ll n,q; cin >> n >> q;
    vector<ll> a(n+5);
    rep1(i,n) cin >> a[i];

    vector<ll> rx(n+5,n);
    stack<ll> stk;
    rep1(i,n){
        while(!stk.empty() and a[i] > a[stk.top()]){
            rx[stk.top()] = i-1;
            stk.pop();
        }
        stk.push(i);
    }

    vector<ll> pos(n+5);
    rep1(i,n) pos[a[i]] = i;

    fenwick<ll> fenw(n+5);
    ll curr_mx = 0;

    rep1(i,n){
        if(a[i] > curr_mx){
            fenw.pupd(a[i],rx[i]-i+1);
            curr_mx = a[i];
        }
    }

    vector<pll> queries[n+5];
    rep1(id,q){
        ll t,i; cin >> t >> i;
        amin(t,n);
        queries[t].pb({i,id});
    }

    vector<ll> ans(q+5);

    rep(t,n+1){
        if(t){
            ll l = 1, r = n;
            ll mid_block = -1;

            while(l <= r){
                ll mid = (l+r) >> 1;
                if(fenw.query(1,mid) > n/2){
                    mid_block = mid;
                    r = mid-1;
                }
                else{
                    l = mid+1;
                }
            }

            assert(mid_block != -1);

            ll prev_sum = fenw.query(1,mid_block-1);
            ll ignore = n/2-prev_sum;

            if(ignore){
                ll siz = fenw.query(mid_block,mid_block);
                fenw.pupd(mid_block,-siz+ignore);

                ll ptr = pos[mid_block]+ignore;
                ll mxr = pos[mid_block]+siz-1;

                while(ptr <= mxr){
                    if(rx[ptr] <= mxr){
                        fenw.pupd(a[ptr],rx[ptr]-ptr+1);
                        ptr = rx[ptr]+1;
                    }
                    else{
                        fenw.pupd(a[ptr],mxr-ptr+1);
                        ptr = mxr+1;
                    }
                }
            }
        }

        for(auto [i,id] : queries[t]){
            ll l = 1, r = n;
            ll block = -1;

            while(l <= r){
                ll mid = (l+r) >> 1;
                if(fenw.query(1,mid) >= i){
                    block = mid;
                    r = mid-1;
                }
                else{
                    l = mid+1;
                }
            }

            ll prev_sum = fenw.query(1,block-1);
            ans[id] = a[pos[block]+i-prev_sum-1];
        }
    }

    rep1(i,q) cout << ans[i] << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 32100 KB Output is correct
2 Correct 259 ms 30160 KB Output is correct
3 Correct 236 ms 29392 KB Output is correct
4 Correct 205 ms 26952 KB Output is correct
5 Correct 217 ms 32852 KB Output is correct
6 Correct 197 ms 30688 KB Output is correct
7 Correct 223 ms 34900 KB Output is correct
8 Correct 204 ms 29284 KB Output is correct
9 Correct 205 ms 28544 KB Output is correct
10 Correct 206 ms 28280 KB Output is correct
11 Correct 203 ms 28356 KB Output is correct
12 Correct 198 ms 30352 KB Output is correct
13 Correct 215 ms 27384 KB Output is correct
14 Correct 213 ms 30224 KB Output is correct
15 Correct 222 ms 29080 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 233 ms 27052 KB Output is correct
18 Correct 189 ms 27464 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 368 ms 42572 KB Output is correct
2 Correct 363 ms 55260 KB Output is correct
3 Correct 302 ms 47800 KB Output is correct
4 Correct 321 ms 47024 KB Output is correct
5 Correct 317 ms 48052 KB Output is correct
6 Correct 317 ms 46000 KB Output is correct
7 Correct 340 ms 54444 KB Output is correct
8 Correct 349 ms 53276 KB Output is correct
9 Correct 295 ms 47616 KB Output is correct
10 Correct 337 ms 51120 KB Output is correct
11 Correct 267 ms 44464 KB Output is correct
12 Correct 291 ms 44604 KB Output is correct
13 Correct 339 ms 51812 KB Output is correct
14 Correct 279 ms 46072 KB Output is correct
15 Correct 354 ms 53548 KB Output is correct
16 Correct 50 ms 12480 KB Output is correct
17 Correct 339 ms 49276 KB Output is correct
18 Correct 263 ms 43848 KB Output is correct
19 Correct 113 ms 20160 KB Output is correct
20 Correct 115 ms 20932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 10324 KB Output is correct
2 Correct 68 ms 9732 KB Output is correct
3 Correct 74 ms 9812 KB Output is correct
4 Correct 63 ms 9552 KB Output is correct
5 Correct 66 ms 9824 KB Output is correct
6 Correct 57 ms 9300 KB Output is correct
7 Correct 61 ms 9812 KB Output is correct
8 Correct 67 ms 9456 KB Output is correct
9 Correct 57 ms 9552 KB Output is correct
10 Correct 52 ms 9296 KB Output is correct
11 Correct 49 ms 9808 KB Output is correct
12 Correct 53 ms 9564 KB Output is correct
13 Correct 54 ms 9044 KB Output is correct
14 Correct 51 ms 9556 KB Output is correct
15 Correct 54 ms 9300 KB Output is correct
16 Correct 19 ms 5720 KB Output is correct
17 Correct 62 ms 8516 KB Output is correct
18 Correct 47 ms 9416 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 32100 KB Output is correct
2 Correct 259 ms 30160 KB Output is correct
3 Correct 236 ms 29392 KB Output is correct
4 Correct 205 ms 26952 KB Output is correct
5 Correct 217 ms 32852 KB Output is correct
6 Correct 197 ms 30688 KB Output is correct
7 Correct 223 ms 34900 KB Output is correct
8 Correct 204 ms 29284 KB Output is correct
9 Correct 205 ms 28544 KB Output is correct
10 Correct 206 ms 28280 KB Output is correct
11 Correct 203 ms 28356 KB Output is correct
12 Correct 198 ms 30352 KB Output is correct
13 Correct 215 ms 27384 KB Output is correct
14 Correct 213 ms 30224 KB Output is correct
15 Correct 222 ms 29080 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 233 ms 27052 KB Output is correct
18 Correct 189 ms 27464 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 368 ms 42572 KB Output is correct
22 Correct 363 ms 55260 KB Output is correct
23 Correct 302 ms 47800 KB Output is correct
24 Correct 321 ms 47024 KB Output is correct
25 Correct 317 ms 48052 KB Output is correct
26 Correct 317 ms 46000 KB Output is correct
27 Correct 340 ms 54444 KB Output is correct
28 Correct 349 ms 53276 KB Output is correct
29 Correct 295 ms 47616 KB Output is correct
30 Correct 337 ms 51120 KB Output is correct
31 Correct 267 ms 44464 KB Output is correct
32 Correct 291 ms 44604 KB Output is correct
33 Correct 339 ms 51812 KB Output is correct
34 Correct 279 ms 46072 KB Output is correct
35 Correct 354 ms 53548 KB Output is correct
36 Correct 50 ms 12480 KB Output is correct
37 Correct 339 ms 49276 KB Output is correct
38 Correct 263 ms 43848 KB Output is correct
39 Correct 113 ms 20160 KB Output is correct
40 Correct 115 ms 20932 KB Output is correct
41 Correct 75 ms 10324 KB Output is correct
42 Correct 68 ms 9732 KB Output is correct
43 Correct 74 ms 9812 KB Output is correct
44 Correct 63 ms 9552 KB Output is correct
45 Correct 66 ms 9824 KB Output is correct
46 Correct 57 ms 9300 KB Output is correct
47 Correct 61 ms 9812 KB Output is correct
48 Correct 67 ms 9456 KB Output is correct
49 Correct 57 ms 9552 KB Output is correct
50 Correct 52 ms 9296 KB Output is correct
51 Correct 49 ms 9808 KB Output is correct
52 Correct 53 ms 9564 KB Output is correct
53 Correct 54 ms 9044 KB Output is correct
54 Correct 51 ms 9556 KB Output is correct
55 Correct 54 ms 9300 KB Output is correct
56 Correct 19 ms 5720 KB Output is correct
57 Correct 62 ms 8516 KB Output is correct
58 Correct 47 ms 9416 KB Output is correct
59 Correct 0 ms 344 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 694 ms 66332 KB Output is correct
62 Correct 646 ms 64764 KB Output is correct
63 Correct 656 ms 63184 KB Output is correct
64 Correct 529 ms 62432 KB Output is correct
65 Correct 512 ms 65232 KB Output is correct
66 Correct 533 ms 62488 KB Output is correct
67 Correct 418 ms 61900 KB Output is correct
68 Correct 457 ms 59240 KB Output is correct
69 Correct 465 ms 63312 KB Output is correct
70 Correct 443 ms 56720 KB Output is correct
71 Correct 358 ms 59852 KB Output is correct
72 Correct 419 ms 57680 KB Output is correct
73 Correct 398 ms 58764 KB Output is correct
74 Correct 394 ms 62244 KB Output is correct
75 Correct 436 ms 58732 KB Output is correct
76 Correct 43 ms 12372 KB Output is correct
77 Correct 544 ms 49360 KB Output is correct
78 Correct 344 ms 51052 KB Output is correct
79 Correct 0 ms 344 KB Output is correct
80 Correct 0 ms 348 KB Output is correct