Submission #944078

# Submission time Handle Problem Language Result Execution time Memory
944078 2024-03-12T08:09:48 Z 12345678 Index (COCI21_index) C++17
110 / 110
394 ms 137372 KB
#include <bits/stdc++.h>

using namespace std;

const int nx=2e5+5;

int n, q, p[nx], l, r;

struct persist
{
    struct node
    {
        int vl;
        node *l, *r;
        node(): vl(0), l(0), r(0){}
        node(int vl): vl(vl), l(0), r(0){}
    };
    typedef node* pnode;
    pnode rt[nx];
    void build(int l, int r, pnode &k)
    {
        k=new node(0);
        if (l==r) return;
        int md=(l+r)/2;
        build(l, md, k->l);
        build(md+1, r, k->r);
    }
    void update(int l, int r, pnode &k, pnode t, int idx)
    {
        k=new node(*t);
        if (l==r) return k->vl++, void();
        int md=(l+r)/2;
        if (idx<=md) update(l, md, k->l, t->l, idx);
        else update(md+1, r, k->r, t->r, idx);
        k->vl=k->l->vl+k->r->vl;
    }
    void show(int l, int r, pnode k)
    {
        cout<<l<<' '<<r<<' '<<k->vl<<'\n';
        if (l==r) return;
        int md=(l+r)/2;
        show(l, md, k->l);
        show(md+1, r ,k->r);
    }
    int query(int l, int r, pnode t, pnode k, int sm)
    {
        if (l==r) return l;
        //cout<<"query "<<l<<' '<<r<<' '<<sm<<' '<<(k->r->vl)-(t->r->vl)<<'\n';
        int md=(l+r)/2;
        if ((k->r->vl)-(t->r->vl)+sm>=md+1) return query(md+1, r, t->r, k->r, sm);
        else return query(l, md, t->l, k->l, (k->r->vl)-(t->r->vl)+sm);
    }
} s;

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>q;
    for (int i=1; i<=n; i++) cin>>p[i];
    s.build(1, n, s.rt[0]);
    for (int i=1; i<=n; i++) s.update(1, n, s.rt[i], s.rt[i-1], p[i]);
    //s.show(1, n, s.rt[n]);
    while (q--)
    {
        cin>>l>>r;
        cout<<s.query(1, n, s.rt[l-1], s.rt[r], 0)<<'\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2908 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 1 ms 2908 KB Output is correct
6 Correct 1 ms 2908 KB Output is correct
7 Correct 1 ms 2860 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 1 ms 2908 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2908 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 1 ms 2908 KB Output is correct
6 Correct 1 ms 2908 KB Output is correct
7 Correct 1 ms 2860 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 1 ms 2908 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
11 Correct 61 ms 32848 KB Output is correct
12 Correct 62 ms 33012 KB Output is correct
13 Correct 60 ms 32844 KB Output is correct
14 Correct 67 ms 32852 KB Output is correct
15 Correct 64 ms 33172 KB Output is correct
16 Correct 64 ms 32972 KB Output is correct
17 Correct 64 ms 33000 KB Output is correct
18 Correct 60 ms 32852 KB Output is correct
19 Correct 72 ms 32864 KB Output is correct
20 Correct 69 ms 32852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2908 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2908 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 1 ms 2908 KB Output is correct
6 Correct 1 ms 2908 KB Output is correct
7 Correct 1 ms 2860 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 1 ms 2908 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
11 Correct 61 ms 32848 KB Output is correct
12 Correct 62 ms 33012 KB Output is correct
13 Correct 60 ms 32844 KB Output is correct
14 Correct 67 ms 32852 KB Output is correct
15 Correct 64 ms 33172 KB Output is correct
16 Correct 64 ms 32972 KB Output is correct
17 Correct 64 ms 33000 KB Output is correct
18 Correct 60 ms 32852 KB Output is correct
19 Correct 72 ms 32864 KB Output is correct
20 Correct 69 ms 32852 KB Output is correct
21 Correct 352 ms 137040 KB Output is correct
22 Correct 394 ms 137220 KB Output is correct
23 Correct 348 ms 137196 KB Output is correct
24 Correct 360 ms 137004 KB Output is correct
25 Correct 351 ms 137164 KB Output is correct
26 Correct 359 ms 137088 KB Output is correct
27 Correct 355 ms 137344 KB Output is correct
28 Correct 358 ms 137080 KB Output is correct
29 Correct 355 ms 137372 KB Output is correct
30 Correct 367 ms 137232 KB Output is correct