Submission #480876

# Submission time Handle Problem Language Result Execution time Memory
480876 2021-10-18T14:28:05 Z MilosMilutinovic Index (COCI21_index) C++14
110 / 110
1910 ms 59556 KB
/**
 *    author: m371
 *    created: 18.10.2021 15:55:58
**/
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;
const int M = 20 * N;  

int st[M], ls[M], rs[M], tsz, root[N];

void modify(int prv, int& node, int l, int r, int x, int y) {
  node = ++tsz; ls[node] = ls[prv]; rs[node] = rs[prv]; st[node] = st[prv] + y;
  if (l == r) {
    return;
  }
  int mid = l + r >> 1;
  if (x <= mid) {
    modify(ls[prv], ls[node], l, mid, x, y);
  } else {
    modify(rs[prv], rs[node], mid + 1, r, x, y);
  }
}

int get(int node, int l, int r, int ll, int rr) {
  if (l > r || l > rr || r < ll) {
    return 0;
  }
  if (ll <= l && r <= rr) {
    return st[node];
  }              
  int mid = l + r >> 1;
  return get(ls[node], l, mid, ll, rr) + get(rs[node], mid + 1, r, ll, rr); 
} 

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, qq;
  cin >> n >> qq;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  vector<vector<int>> at(N);
  for (int i = 0; i < n; i++) {
    at[p[i]].push_back(i);
  }
  for (int i = N - 1; i >= 1; i--) {
    root[i] = root[i + 1];
    for (int j : at[i]) {
      modify(root[i], root[i], 0, n - 1, j, 1);
    }
  }
  while (qq--) {
    int l, r;
    cin >> l >> r;
    --l; --r;
    int low = 1, high = N - 1, ans = 0;
    while (low <= high) {
      int mid = low + high >> 1;
      if (get(root[mid], 0, n - 1, l, r) >= mid) {
        ans = mid;
        low = mid + 1;  
      } else {
        high = mid - 1;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}

Compilation message

index.cpp: In function 'void modify(int, int&, int, int, int, int)':
index.cpp:19:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   19 |   int mid = l + r >> 1;
      |             ~~^~~
index.cpp: In function 'int get(int, int, int, int, int)':
index.cpp:34:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   34 |   int mid = l + r >> 1;
      |             ~~^~~
index.cpp: In function 'int main()':
index.cpp:63:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   63 |       int mid = low + high >> 1;
      |                 ~~~~^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5964 KB Output is correct
2 Correct 7 ms 5964 KB Output is correct
3 Correct 7 ms 5964 KB Output is correct
4 Correct 6 ms 5964 KB Output is correct
5 Correct 6 ms 5964 KB Output is correct
6 Correct 6 ms 5964 KB Output is correct
7 Correct 9 ms 6000 KB Output is correct
8 Correct 7 ms 5964 KB Output is correct
9 Correct 9 ms 5964 KB Output is correct
10 Correct 7 ms 5936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5964 KB Output is correct
2 Correct 7 ms 5964 KB Output is correct
3 Correct 7 ms 5964 KB Output is correct
4 Correct 6 ms 5964 KB Output is correct
5 Correct 6 ms 5964 KB Output is correct
6 Correct 6 ms 5964 KB Output is correct
7 Correct 9 ms 6000 KB Output is correct
8 Correct 7 ms 5964 KB Output is correct
9 Correct 9 ms 5964 KB Output is correct
10 Correct 7 ms 5936 KB Output is correct
11 Correct 295 ms 17848 KB Output is correct
12 Correct 340 ms 17900 KB Output is correct
13 Correct 337 ms 17864 KB Output is correct
14 Correct 280 ms 17792 KB Output is correct
15 Correct 326 ms 17904 KB Output is correct
16 Correct 260 ms 17868 KB Output is correct
17 Correct 288 ms 17820 KB Output is correct
18 Correct 303 ms 17832 KB Output is correct
19 Correct 278 ms 17964 KB Output is correct
20 Correct 285 ms 17860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5964 KB Output is correct
2 Correct 7 ms 5964 KB Output is correct
3 Correct 7 ms 5964 KB Output is correct
4 Correct 6 ms 5964 KB Output is correct
5 Correct 6 ms 5964 KB Output is correct
6 Correct 6 ms 5964 KB Output is correct
7 Correct 9 ms 6000 KB Output is correct
8 Correct 7 ms 5964 KB Output is correct
9 Correct 9 ms 5964 KB Output is correct
10 Correct 7 ms 5936 KB Output is correct
11 Correct 295 ms 17848 KB Output is correct
12 Correct 340 ms 17900 KB Output is correct
13 Correct 337 ms 17864 KB Output is correct
14 Correct 280 ms 17792 KB Output is correct
15 Correct 326 ms 17904 KB Output is correct
16 Correct 260 ms 17868 KB Output is correct
17 Correct 288 ms 17820 KB Output is correct
18 Correct 303 ms 17832 KB Output is correct
19 Correct 278 ms 17964 KB Output is correct
20 Correct 285 ms 17860 KB Output is correct
21 Correct 1756 ms 59556 KB Output is correct
22 Correct 1599 ms 59300 KB Output is correct
23 Correct 1570 ms 59232 KB Output is correct
24 Correct 1631 ms 59504 KB Output is correct
25 Correct 1701 ms 59368 KB Output is correct
26 Correct 1637 ms 59288 KB Output is correct
27 Correct 1655 ms 59308 KB Output is correct
28 Correct 1635 ms 59360 KB Output is correct
29 Correct 1910 ms 59352 KB Output is correct
30 Correct 1800 ms 59420 KB Output is correct