Submission #1288189

#TimeUsernameProblemLanguageResultExecution timeMemory
1288189hainam2k9Abracadabra (CEOI22_abracadabra)C++20
100 / 100
549 ms38256 KiB
#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 2e5+5;
const string NAME = "";
int n,q,a[MAXN],b[MAXN],nxt[MAXN],rs[MAXN*5],pos,sz;
int bit[MAXN];
pair<int,int> seg[MAXN];
vector<pair<int,int>> query[MAXN];
struct cmp{
    bool operator()(const pair<int,int>& p1, const pair<int,int>& p2) const{
        return a[p1.fi]<a[p2.fi];
    }
};
set<pair<int,int>, cmp> s;
stack<int> st;
void update(int pos, int val){
    while(pos<=n) bit[pos]+=val, pos+=pos&-pos;
}
int getsum(int pos){
    int rs=0;
    while(pos>0) rs+=bit[pos], pos-=pos&-pos;
    return rs;
}
int bs(int x){
    int rs=0,sum=0;
    for(int i = __lg(n); i>=0; --i)
        if(rs+(1<<i)<=n&&sum+bit[rs+(1<<i)]<x) rs+=1<<i, sum+=bit[rs];
    return rs+1;
}
void upd(){
    while(!s.empty()){
        int l=s.rbegin()->fi, r=s.rbegin()->se;
        if(sz-r+l-1<n/2) break;
        s.erase(prev(s.end())), sz-=r-l+1;
    }
    if(sz<=n/2) return;
    int l=s.rbegin()->fi, r=s.rbegin()->se, d=n/2-sz+r-l+1;
    update(a[l],-r+l-1);
    s.erase(prev(s.end()));
    s.ins(l,min(l+d-1,r)), seg[a[l]]={l,min(l+d-1,r)};
    update(a[l],min(d,r-l+1));
    l+=d;
    while(l<=r){
        s.ins(l,min(nxt[l],r)), seg[a[l]]={l,min(nxt[l],r)};
        update(a[l],min(nxt[l],r)-l+1);
        l=nxt[l]+1;
    }
}
int main()
{
    tt;
    if(fopen((NAME + ".INP").c_str(), "r")) fo;
    cin >> n >> q;
    for(int i = 1; i<=n; ++i)
        cin >> a[i];
    for(int i = 1; i<=q; ++i){
        int t,x;
        cin >> t >> x;
        query[min(t,n)].pb(x,i);
    }
    for(int i = n; i>0; --i){
        while(!st.empty()&&a[i]>a[st.top()]) st.pop();
        nxt[i]=(st.empty() ? n : st.top()-1), st.ins(i);
    }
    for(int i = 1; i<=n; i=nxt[i]+1)
        s.ins(i,nxt[i]), seg[a[i]]={i,nxt[i]}, update(a[i],nxt[i]-i+1);
    sz=n;
    for(int i = 0; i<=n; ++i){
        for(pair<int,int>& p : query[i]){
            int x=bs(p.fi);
            p.fi-=getsum(x-1);
            rs[p.se]=a[seg[x].fi+p.fi-1];
        }
        upd();
    }
    for(int i = 1; i<=q; ++i)
        cout << rs[i] << "\n";
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:69:45: note: in expansion of macro 'fo'
   69 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
Main.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |                                                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:69:45: note: in expansion of macro 'fo'
   69 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...