Submission #669856

# Submission time Handle Problem Language Result Execution time Memory
669856 2022-12-07T13:57:41 Z ktkerem Abracadabra (CEOI22_abracadabra) C++17
100 / 100
774 ms 80336 KB
/*#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
#include<bits/stdc++.h>
/**/
//typedef int ll;
typedef long long ll;
typedef unsigned long long ull;
typedef std::string str;
//typedef vector<std::vector<ll>> vv; 
/*typedef __int128 vll;
typedef unsigned __int128 uvll;*/
#define llll std::pair<ll , ll>
#define pb push_back
#define pf push_front
#define halo cout << "hello\n"
#define fi first
#define sec second
#define vv(x) std::vector<std::vector<x>>
#define all(a) a.begin() , a.end()
const ll limit = 1e15+7; 
const ll ous = 2e5 + 7;
const ll dx[4] = {-1 , 0 , 1 , 0} , dy[4] = {0,1,0,-1};
ll n , m;std::vector<ll> ar , nex , ants , valt;std::vector<std::pair<ll , llll>> que , v;
std::set<std::pair<ll , llll>> st; 
ll siz = 0 , kd = 0;
ll fnd(ll f , ll s , ll t){
    ll ret = std::lower_bound(all(v) , std::make_pair(f , std::make_pair(s , t))) - v.begin();
    return ret;
}
void upd(ll l , ll r , ll a , ll nt , ll oz){
    if(l == r){
        if(oz){
            valt[a] = v[l].sec.sec - v[l].sec.fi + 1; 
        }
        else{
            valt[a] = 0;
        }
        return;
    }
    ll md = (l + r)/2;
    if(nt > md){
        upd(md+1 , r , a*2+1 , nt , oz);
    }
    else{
        upd(l , md , a*2 , nt , oz);
    }
    valt[a] = valt[a*2] + valt[a*2+1]; 
}
ll binseg(ll l , ll r , ll a ,ll cnt){
    if(l == r){
        return ar[v[l].sec.fi + cnt - 1];
    }
    ll md = (l + r) / 2;
    if(valt[a*2] >= cnt){
        return binseg(l , md , a*2 ,cnt);
    }
    else{
        cnt -= valt[a*2];
        return binseg(md+1 , r , a*2+1 , cnt);
    }
}
void addset(ll l , ll r){
    siz += r - l + 1;
    st.insert({ar[l] , {l , r}});
    if(kd == 0){
        v.pb({ar[l] , {l , r}});
    }
    else{
        upd(0 , v.size()-1 , 1 , fnd(ar[l] , l , r) , 1);
    }
}
void addi(ll l , ll r){
    while(r >= l){
        ll nx = nex[l] - 1;;
        nx = std::min(nx , r);
        addset(l , nx);
        //std::cout << l << " " << nx << "\n";
        l = nex[l];
    }
}
void rm(){
    while(1){
        std::pair<ll , llll> a = *st.rbegin();
        ll p = a.sec.sec - a.sec.fi + 1;
        if(siz - p < n/2){
            return; 
        }
        siz-=p;
        //std::cout << a.fi << " " << a.sec.fi << " " << a.sec.sec << " " << siz << "\n";
        auto it = st.end();it--;
        st.erase(it);
    }
}
void midup(){
    std::pair<ll , llll> a = *st.rbegin();
    ll p = a.sec.sec - a.sec.fi + 1;
    auto it = st.end();it--;st.erase(it);
    siz -= p;
    if(kd){
        upd(0 , v.size()-1 , 1 , fnd(a.fi , a.sec.fi , a.sec.sec) , 0);
    }
    //std::cout << a.fi << " " << a.sec.fi << " " << a.sec.sec << " " << siz << "\n";
    ll of = n/2 -siz;
    addset(a.sec.fi , a.sec.fi + of - 1);
    addi(a.sec.fi+of , a.sec.sec);
}
void solve(){
    std::cin >> n >> m;
    ar.resize(n);que.resize(m);nex.resize(n);ants.resize(m);
    for(ll i = 0;n>i;i++){
        std::cin >> ar[i];
    }
    for(ll i = 0;m>i;i++){
        std::cin >> que[i].fi >> que[i].sec.fi;
        que[i].fi = std::min(que[i].fi , n);
        que[i].sec.sec = i;
    }
    std::sort(all(que));
    std::vector<llll> sta;
    sta.pb({limit , n});
    for(ll i = n-1;0<=i;i--){
        while(sta.back().fi < ar[i]){
            sta.pop_back();
        }
        nex[i] = sta.back().sec;
        sta.pb({ar[i] , i});
    }
    addi(0 , n-1);
    while(1){
        rm();
        if(siz == n/2){
            break;
        }
        midup();  
    }
    std::sort(all(v));
    valt.resize(4 * v.size());
    kd = 1;
    st.clear();
    ll tt = 0;
    siz = 0;
    addi(0 , n-1);
    for(ll i = 0;m>i;i++){
        for(;que[i].fi > tt;tt++){
            rm();
            midup();
        }
        ants[que[i].sec.sec] = binseg(0 , v.size() - 1 , 1 , que[i].sec.fi);
    }
    for(ll i = 0;m>i;i++){
        std::cout << ants[i] << "\n";
    }
    return;/**/
}
signed main(){
    std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
    ll t=1;
    //std::cin >> t;
    ll o = 1;
    while(t--){ 
        solve();        
    }
    return 0;
}/**/

Compilation message

Main.cpp:5:78: warning: "/*" within comment [-Wcomment]
    5 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
      |                                                                               
Main.cpp: In function 'int main()':
Main.cpp:162:8: warning: unused variable 'o' [-Wunused-variable]
  162 |     ll o = 1;
      |        ^
# Verdict Execution time Memory Grader output
1 Correct 328 ms 40552 KB Output is correct
2 Correct 331 ms 41036 KB Output is correct
3 Correct 324 ms 39400 KB Output is correct
4 Correct 300 ms 40012 KB Output is correct
5 Correct 333 ms 41300 KB Output is correct
6 Correct 329 ms 39608 KB Output is correct
7 Correct 332 ms 40780 KB Output is correct
8 Correct 330 ms 39300 KB Output is correct
9 Correct 319 ms 39188 KB Output is correct
10 Correct 316 ms 39888 KB Output is correct
11 Correct 318 ms 39324 KB Output is correct
12 Correct 298 ms 38572 KB Output is correct
13 Correct 309 ms 39228 KB Output is correct
14 Correct 317 ms 41284 KB Output is correct
15 Correct 315 ms 41628 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 291 ms 39768 KB Output is correct
18 Correct 296 ms 39424 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 526 ms 77712 KB Output is correct
2 Correct 486 ms 79444 KB Output is correct
3 Correct 463 ms 66752 KB Output is correct
4 Correct 365 ms 50172 KB Output is correct
5 Correct 392 ms 51828 KB Output is correct
6 Correct 345 ms 51192 KB Output is correct
7 Correct 371 ms 54328 KB Output is correct
8 Correct 374 ms 55712 KB Output is correct
9 Correct 393 ms 48476 KB Output is correct
10 Correct 345 ms 50096 KB Output is correct
11 Correct 324 ms 40524 KB Output is correct
12 Correct 322 ms 40944 KB Output is correct
13 Correct 341 ms 49240 KB Output is correct
14 Correct 340 ms 44184 KB Output is correct
15 Correct 382 ms 52460 KB Output is correct
16 Correct 22 ms 4680 KB Output is correct
17 Correct 485 ms 72324 KB Output is correct
18 Correct 248 ms 37964 KB Output is correct
19 Correct 70 ms 13228 KB Output is correct
20 Correct 126 ms 14412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 191 ms 21364 KB Output is correct
2 Correct 164 ms 19652 KB Output is correct
3 Correct 173 ms 18652 KB Output is correct
4 Correct 79 ms 8580 KB Output is correct
5 Correct 161 ms 14344 KB Output is correct
6 Correct 84 ms 9196 KB Output is correct
7 Correct 125 ms 11920 KB Output is correct
8 Correct 101 ms 10332 KB Output is correct
9 Correct 128 ms 12668 KB Output is correct
10 Correct 61 ms 7132 KB Output is correct
11 Correct 66 ms 7004 KB Output is correct
12 Correct 58 ms 7064 KB Output is correct
13 Correct 66 ms 7448 KB Output is correct
14 Correct 64 ms 7500 KB Output is correct
15 Correct 56 ms 6580 KB Output is correct
16 Correct 12 ms 2500 KB Output is correct
17 Correct 124 ms 19180 KB Output is correct
18 Correct 46 ms 6428 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 328 ms 40552 KB Output is correct
2 Correct 331 ms 41036 KB Output is correct
3 Correct 324 ms 39400 KB Output is correct
4 Correct 300 ms 40012 KB Output is correct
5 Correct 333 ms 41300 KB Output is correct
6 Correct 329 ms 39608 KB Output is correct
7 Correct 332 ms 40780 KB Output is correct
8 Correct 330 ms 39300 KB Output is correct
9 Correct 319 ms 39188 KB Output is correct
10 Correct 316 ms 39888 KB Output is correct
11 Correct 318 ms 39324 KB Output is correct
12 Correct 298 ms 38572 KB Output is correct
13 Correct 309 ms 39228 KB Output is correct
14 Correct 317 ms 41284 KB Output is correct
15 Correct 315 ms 41628 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 291 ms 39768 KB Output is correct
18 Correct 296 ms 39424 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 526 ms 77712 KB Output is correct
22 Correct 486 ms 79444 KB Output is correct
23 Correct 463 ms 66752 KB Output is correct
24 Correct 365 ms 50172 KB Output is correct
25 Correct 392 ms 51828 KB Output is correct
26 Correct 345 ms 51192 KB Output is correct
27 Correct 371 ms 54328 KB Output is correct
28 Correct 374 ms 55712 KB Output is correct
29 Correct 393 ms 48476 KB Output is correct
30 Correct 345 ms 50096 KB Output is correct
31 Correct 324 ms 40524 KB Output is correct
32 Correct 322 ms 40944 KB Output is correct
33 Correct 341 ms 49240 KB Output is correct
34 Correct 340 ms 44184 KB Output is correct
35 Correct 382 ms 52460 KB Output is correct
36 Correct 22 ms 4680 KB Output is correct
37 Correct 485 ms 72324 KB Output is correct
38 Correct 248 ms 37964 KB Output is correct
39 Correct 70 ms 13228 KB Output is correct
40 Correct 126 ms 14412 KB Output is correct
41 Correct 191 ms 21364 KB Output is correct
42 Correct 164 ms 19652 KB Output is correct
43 Correct 173 ms 18652 KB Output is correct
44 Correct 79 ms 8580 KB Output is correct
45 Correct 161 ms 14344 KB Output is correct
46 Correct 84 ms 9196 KB Output is correct
47 Correct 125 ms 11920 KB Output is correct
48 Correct 101 ms 10332 KB Output is correct
49 Correct 128 ms 12668 KB Output is correct
50 Correct 61 ms 7132 KB Output is correct
51 Correct 66 ms 7004 KB Output is correct
52 Correct 58 ms 7064 KB Output is correct
53 Correct 66 ms 7448 KB Output is correct
54 Correct 64 ms 7500 KB Output is correct
55 Correct 56 ms 6580 KB Output is correct
56 Correct 12 ms 2500 KB Output is correct
57 Correct 124 ms 19180 KB Output is correct
58 Correct 46 ms 6428 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 736 ms 80336 KB Output is correct
62 Correct 769 ms 77972 KB Output is correct
63 Correct 774 ms 72724 KB Output is correct
64 Correct 556 ms 57924 KB Output is correct
65 Correct 584 ms 61692 KB Output is correct
66 Correct 551 ms 57996 KB Output is correct
67 Correct 506 ms 55132 KB Output is correct
68 Correct 499 ms 55424 KB Output is correct
69 Correct 544 ms 55420 KB Output is correct
70 Correct 453 ms 51012 KB Output is correct
71 Correct 402 ms 49300 KB Output is correct
72 Correct 440 ms 49228 KB Output is correct
73 Correct 420 ms 48856 KB Output is correct
74 Correct 450 ms 51392 KB Output is correct
75 Correct 453 ms 50024 KB Output is correct
76 Correct 23 ms 4180 KB Output is correct
77 Correct 542 ms 71672 KB Output is correct
78 Correct 344 ms 46888 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct