Submission #894514

# Submission time Handle Problem Language Result Execution time Memory
894514 2023-12-28T11:24:30 Z vjudge1 Index (COCI21_index) C++17
110 / 110
520 ms 8792 KB
#include <bits/stdc++.h>
using namespace std;
 
#define speed ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define rall(x) x.rbegin(), x.rend()
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define nl '\n'
//#define int long long
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
 
const int N = 2e5 + 5;
const int M = 1e3 + 5;
const int inf = 1e9 + 123;
const ll infl = 1e15 + 10;
const int mod = 1e9 + 7;
const int P = 31;
const int block = 300;
 
int n, q, MX, a[N], t[N], ans[N];
 
struct query{
    int l, r, id;
};
 
vector <query> qs;
 
int sum (int r) {
    int res = 0;
    for (; r > 0; r -= r & -r)
        res += t[r];
    return res;
}
 
int sum (int l, int r) {
    return sum(r) - sum(l-1);
}
 
void add (int k, int x) {
    for (; k <= n; k += k & -k)
        t[k] += x;
}

bool cmp(const query& a, const query& b) {
    if (a.l / block != b.l / block) 
        return a.l / block < b.l / block; 
    else
        return a.r < b.r;
}
 
void add(int x){
    add(x, 1);
}
 
void del(int x){
    add(x, -1);
}
 
void solve() {
    cin >> n >> q;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    for(int i = 1; i <= q; i++){
        int l, r;
        cin >> l >> r;
        qs.pb({l, r, i});
        MX = max(MX, r);
    }

    sort(all(qs), cmp);
 
    int l = 1, r = 0;
    for(auto Q : qs){
        while (r < Q.r)
            add(a[++r]);
        while (l < Q.l)
            del(a[l++]);
        while (l > Q.l)
            add(a[--l]);
        while(r > Q.r)
            del(a[r--]);
        int l = 1, r = n + 1, mid;
        while(r - l > 1){
            mid = (l + r) >> 1;
            if(sum(mid, MX) >= mid)
                l = mid; 
            else
                r = mid;
        }
        ans[Q.id] = l;
    }
    for(int i = 1; i <= q; i++)
        cout << ans[i] << nl;
}
 
signed main() { 
    speed;
    int T = 1;
    // cin >> T;
    while (T--)
        solve();
}
 
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2536 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2536 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 80 ms 3788 KB Output is correct
12 Correct 81 ms 3528 KB Output is correct
13 Correct 75 ms 3528 KB Output is correct
14 Correct 79 ms 3528 KB Output is correct
15 Correct 76 ms 3532 KB Output is correct
16 Correct 86 ms 3660 KB Output is correct
17 Correct 76 ms 3532 KB Output is correct
18 Correct 76 ms 3652 KB Output is correct
19 Correct 75 ms 3532 KB Output is correct
20 Correct 78 ms 3528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2536 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 80 ms 3788 KB Output is correct
12 Correct 81 ms 3528 KB Output is correct
13 Correct 75 ms 3528 KB Output is correct
14 Correct 79 ms 3528 KB Output is correct
15 Correct 76 ms 3532 KB Output is correct
16 Correct 86 ms 3660 KB Output is correct
17 Correct 76 ms 3532 KB Output is correct
18 Correct 76 ms 3652 KB Output is correct
19 Correct 75 ms 3532 KB Output is correct
20 Correct 78 ms 3528 KB Output is correct
21 Correct 505 ms 6280 KB Output is correct
22 Correct 491 ms 8792 KB Output is correct
23 Correct 507 ms 8636 KB Output is correct
24 Correct 511 ms 8772 KB Output is correct
25 Correct 485 ms 8600 KB Output is correct
26 Correct 519 ms 8608 KB Output is correct
27 Correct 486 ms 8608 KB Output is correct
28 Correct 520 ms 8596 KB Output is correct
29 Correct 519 ms 8600 KB Output is correct
30 Correct 499 ms 8736 KB Output is correct