Submission #805202

# Submission time Handle Problem Language Result Execution time Memory
805202 2023-08-03T13:58:13 Z vjudge1 Index (COCI21_index) C++17
110 / 110
1274 ms 56144 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn=2e5+5;
int n, q, cnt=1, mx=0;
int ver[maxn];
struct T
{
    int x, no;
} a[maxn];
struct Node
{
    int l, r, v;
    Node() {};
    Node(int x, int y, int z)
    {
        l=x; r=y; v=z;
    }
} d[22*maxn];

bool lf(const T& i, const T& j)
{
    return i.x>j.x;
}

void build(int i, int l, int r)
{
    if (l==r) return;
    d[i].l=++cnt; d[i].r=++cnt;
    int mid=(l+r)/2;
    build(d[i].l, l, mid);
    build(d[i].r, mid+1, r);
}

int update(int i, int l, int r, int x)
{
    int cur=++cnt;
    if (l==r)
    {
        d[cur]=Node(0,0,1);
        return cur;
    }
    d[cur]=d[i];
    int mid=(l+r)/2;
    if (x<=mid) d[cur].l=update(d[i].l, l, mid, x);
    else d[cur].r=update(d[i].r, mid+1, r, x);
    d[cur].v=d[d[cur].l].v+d[d[cur].r].v;
    return cur;
}

int get(int i, int l, int r, int x, int y)
{
    if (r<x || l>y) return 0;
    if (x<=l && r<=y) return d[i].v;
    int mid=(l+r)/2;
    return get(d[i].l, l, mid, x, y)+get(d[i].r, mid+1, r, x, y);
}

int ssearch(int x)
{
    int l=1, r=n;
    while (l<=r)
    {
        int mid=(l+r)/2;
        if (a[mid].x>=x) l=mid+1;
        else r=mid-1;
    }
    return r;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> q;
    for (int i=1; i<=n; ++i)
    {
        cin >> a[i].x;
        a[i].no=i;
        mx=max(a[i].x, mx);
    }
    sort(a+1, a+n+1, lf);
    build(1, 1, n);
    ver[0]=1;
    for (int i=1; i<=n; ++i)
        ver[i]=update(ver[i-1], 1, n, a[i].no);
    int x, y;
    while (q--)
    {
        cin >> x >> y;
        int l=1, r=mx;
        while (l<=r)
        {
            int mid=(l+r)/2;
            int s=ssearch(mid);
            if (get(ver[s], 1, n, x, y)>=mid)
                l=mid+1;
            else r=mid-1;
        }
        cout << r << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 2 ms 464 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 2 ms 464 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 203 ms 12948 KB Output is correct
12 Correct 210 ms 12960 KB Output is correct
13 Correct 197 ms 12948 KB Output is correct
14 Correct 200 ms 12940 KB Output is correct
15 Correct 207 ms 13116 KB Output is correct
16 Correct 205 ms 12988 KB Output is correct
17 Correct 199 ms 12992 KB Output is correct
18 Correct 210 ms 12976 KB Output is correct
19 Correct 199 ms 12932 KB Output is correct
20 Correct 198 ms 12996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 2 ms 464 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 203 ms 12948 KB Output is correct
12 Correct 210 ms 12960 KB Output is correct
13 Correct 197 ms 12948 KB Output is correct
14 Correct 200 ms 12940 KB Output is correct
15 Correct 207 ms 13116 KB Output is correct
16 Correct 205 ms 12988 KB Output is correct
17 Correct 199 ms 12992 KB Output is correct
18 Correct 210 ms 12976 KB Output is correct
19 Correct 199 ms 12932 KB Output is correct
20 Correct 198 ms 12996 KB Output is correct
21 Correct 1169 ms 56100 KB Output is correct
22 Correct 1274 ms 56040 KB Output is correct
23 Correct 1210 ms 56144 KB Output is correct
24 Correct 1182 ms 56124 KB Output is correct
25 Correct 1176 ms 56108 KB Output is correct
26 Correct 1175 ms 56128 KB Output is correct
27 Correct 1209 ms 56036 KB Output is correct
28 Correct 1240 ms 56100 KB Output is correct
29 Correct 1172 ms 56032 KB Output is correct
30 Correct 1191 ms 56136 KB Output is correct