Submission #1081586

# Submission time Handle Problem Language Result Execution time Memory
1081586 2024-08-30T07:47:55 Z asli_bg Index (COCI21_index) C++11
110 / 110
542 ms 143188 KB
#include<bits/stdc++.h>
using namespace std;

//#define int long long
 
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pb push_back
 
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
 
#define sp <<' '<<
#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)

#define cont(x) {for(auto el:x) cout<<el<<' ';cout<<endl;}
#define contp(x) {for(auto el:x) cout<<el.fi<<'-'<<el.se<<' ';cout<<endl;}
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
 
#define carp(x,y,mod) ((x%mod)*(y%mod))%mod
#define topla(x,y,mod) ((x%mod)+(y%mod))%mod
 
const int MAXN=2e5+5;
const int INF=1e9+1;
#define mid (l+r)/2
#define endl '\n'

int n,q;
vi a;

struct Dug{
    Dug *l;
    Dug *r;
    int sm;

    Dug(int val) : l(nullptr), r(nullptr), sm(val) {}
    Dug(Dug* l, Dug* r) : l(l), r(r), sm(0){
        if(l) sm+=l->sm;
        if(r) sm+=r->sm;
    }
    Dug(Dug* kopya) : l(kopya->l), r(kopya->r), sm(kopya->sm) {}
};

Dug *root[MAXN];

Dug *build(int l=1,int r=n){
    if(l==r) return new Dug(0);
    auto sol=build(l,mid);
    auto sag=build(mid+1,r);
    return new Dug(sol,sag);
}

Dug *update(int pos,int val,Dug* nd,int l=1,int r=n){
    if(l==r){
        return new Dug(val);
    }
    if(pos<=mid){
        auto sol=update(pos,val,nd->l,l,mid);
        return new Dug(sol,nd->r);
    }
    else{
        auto sag=update(pos,val,nd->r,mid+1,r);
        return new Dug(nd->l,sag);
    }
}

int query(int elde,Dug *bir,Dug *iki,int l=1,int r=n){
    if(l==r){
        return l;
    }
    int deg=(iki->r->sm)-(bir->r->sm);
    if(deg>=mid+1-elde) return query(elde,bir->r,iki->r,mid+1,r);
    else return query(elde+deg,bir->l,iki->l,l,mid);
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>q;
    a.resize(n+1);
    FORE(i,1,n+1) cin>>a[i];
    root[0]=build();

    map<int,int> cnt;

    FORE(i,1,n+1){
        cnt[a[i]]++;
        root[i]=update(a[i],cnt[a[i]],root[i-1]);
    }

    while(q--){
        int l,r;
        cin>>l>>r;
        cout<<query(0,root[l-1],root[r])<<endl;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 81 ms 32848 KB Output is correct
12 Correct 85 ms 32848 KB Output is correct
13 Correct 74 ms 32848 KB Output is correct
14 Correct 78 ms 32772 KB Output is correct
15 Correct 79 ms 32852 KB Output is correct
16 Correct 78 ms 32708 KB Output is correct
17 Correct 83 ms 32852 KB Output is correct
18 Correct 80 ms 32856 KB Output is correct
19 Correct 82 ms 32752 KB Output is correct
20 Correct 83 ms 32856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 860 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 81 ms 32848 KB Output is correct
12 Correct 85 ms 32848 KB Output is correct
13 Correct 74 ms 32848 KB Output is correct
14 Correct 78 ms 32772 KB Output is correct
15 Correct 79 ms 32852 KB Output is correct
16 Correct 78 ms 32708 KB Output is correct
17 Correct 83 ms 32852 KB Output is correct
18 Correct 80 ms 32856 KB Output is correct
19 Correct 82 ms 32752 KB Output is correct
20 Correct 83 ms 32856 KB Output is correct
21 Correct 470 ms 143184 KB Output is correct
22 Correct 514 ms 143016 KB Output is correct
23 Correct 492 ms 143160 KB Output is correct
24 Correct 522 ms 143104 KB Output is correct
25 Correct 475 ms 143004 KB Output is correct
26 Correct 542 ms 143160 KB Output is correct
27 Correct 474 ms 143184 KB Output is correct
28 Correct 521 ms 143188 KB Output is correct
29 Correct 487 ms 143188 KB Output is correct
30 Correct 471 ms 142996 KB Output is correct