답안 #525248

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
525248 2022-02-11T07:39:10 Z Pety Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++14
64 / 100
3000 ms 168372 KB
#include <iostream>
#include<vector>
#include<algorithm>

 
 
using namespace std;
 
const int N = 1e6;
int n, m, w[N + 2], cnt, lg2[N + 2];
struct nodA {
  int inv, st, dr;
  bool sorted;
} aint[4 * N + 2];
vector<int>all;
int ans = 0, mx = 0;
 
 
void build (int nod, int st, int dr) {
  if (st == dr) {
    aint[nod].inv = 0;
    aint[nod].st = all.size();
    all.push_back(w[st]);
    aint[nod].dr = all.size();
    aint[nod].sorted = 1;
    return;
  }
  int mid = (st + dr) / 2;
  build(2 * nod, st, mid);
  build(2 * nod + 1, mid + 1, dr);
  aint[nod].inv = max(aint[2 * nod].inv, aint[2 * nod + 1].inv);
  auto it = lower_bound(all.begin() + aint[2 * nod + 1].st,  all.begin() + aint[2 * nod + 1].dr, all[aint[2 * nod].dr - 1])-all.begin();
  if (it != aint[2 * nod + 1].st) {
    it--;
    aint[nod].inv = max(aint[nod].inv, all[aint[2 * nod].dr - 1] + all[it]);
  }
  aint[nod].st = all.size();
  int i = aint[2* nod].st, j = aint[2 * nod + 1].st;
  while (i < aint[2 * nod].dr&& j < aint[2 * nod + 1].dr) {
    if (all[i] == all[j]) {
      int x = all[i];
      all.push_back(all[i]);
      all.push_back(all[i]);
      i++;
      j++;
    }
    else if (all[i]< all[j]) {
      all.push_back(all[i]);
      i++;
    }
    else if (all[i] > all[j]) {
      all.push_back(all[j]);
      j++;
    }
  }
  while (i < aint[2 * nod].dr)
    all.push_back(all[i++]);
  while (j < aint[2 * nod + 1].dr)
    all.push_back(all[j++]);
  aint[nod].dr = all.size();
  aint[nod].sorted = (aint[2 * nod].sorted & aint[2 * nod + 1].sorted & w[mid] <= w[mid + 1]);
}
 
int lastdr;
bool ok;
 
void query (int nod, int st, int dr, int a, int b) {
  if (a <= st && dr <= b) {
    if (mx == -1) {
      ans = aint[nod].inv;
      mx = max(mx, all[aint[nod].dr - 1]);
      ok &= aint[nod].sorted;
      lastdr = dr;
    }
    else {
      ans = max(ans, aint[nod].inv);
      int poz = aint[nod].st - 1;
      int l = lg2[dr - st + 1];
      for (int pas = (1 << (l + 1)); pas; (pas >>= 1))
        if (poz + pas < aint[nod].dr && all[poz + pas] < mx)
          poz += pas;
      if (poz != aint[nod].st - 1) {
        ans = max(ans, mx + all[poz]);
      }
      ok &= aint[nod].sorted;
      ok &= (w[lastdr] <= w[lastdr + 1]);
      mx = max(mx, all[aint[nod].dr - 1]);
      lastdr = dr;
    }
    return;
  }
  int mid = (st + dr) / 2;
  if (a <= mid)
    query(2 * nod, st, mid, a, b);
  if (b > mid)
    query(2 * nod + 1, mid + 1, dr, a, b);
}
 
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n >> m;
  for (int i = 2; i <= n; i++)
    lg2[i] = lg2[i / 2] + 1;
  for (int i = 1; i <= n; i++)
    cin >> w[i];
  build(1, 1, n);
  for (int i = 1; i <= m; i++) {
    int l, r, k;
    cin >> l >> r >> k;
    ans = mx = lastdr = -1;
    ok = 1;
    query(1, 1, n, l, r);
    cout << (ans <= k) << "\n";
  }
  return 0;
}

Compilation message

sortbooks.cpp: In function 'void build(int, int, int)':
sortbooks.cpp:41:11: warning: unused variable 'x' [-Wunused-variable]
   41 |       int x = all[i];
      |           ^
sortbooks.cpp:61:80: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
   61 |   aint[nod].sorted = (aint[2 * nod].sorted & aint[2 * nod + 1].sorted & w[mid] <= w[mid + 1]);
      |                                                                         ~~~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 3 ms 588 KB Output is correct
12 Correct 4 ms 1040 KB Output is correct
13 Correct 4 ms 976 KB Output is correct
14 Correct 5 ms 1232 KB Output is correct
15 Correct 5 ms 1232 KB Output is correct
16 Correct 5 ms 1300 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 4 ms 1232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2950 ms 168364 KB Output is correct
2 Correct 2978 ms 168368 KB Output is correct
3 Correct 2999 ms 168372 KB Output is correct
4 Correct 2949 ms 168304 KB Output is correct
5 Execution timed out 3102 ms 168360 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 12404 KB Output is correct
2 Correct 127 ms 12432 KB Output is correct
3 Correct 171 ms 12432 KB Output is correct
4 Correct 153 ms 12472 KB Output is correct
5 Correct 140 ms 12428 KB Output is correct
6 Correct 134 ms 12412 KB Output is correct
7 Correct 148 ms 12452 KB Output is correct
8 Correct 105 ms 12448 KB Output is correct
9 Correct 35 ms 572 KB Output is correct
10 Correct 106 ms 12480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 3 ms 588 KB Output is correct
12 Correct 4 ms 1040 KB Output is correct
13 Correct 4 ms 976 KB Output is correct
14 Correct 5 ms 1232 KB Output is correct
15 Correct 5 ms 1232 KB Output is correct
16 Correct 5 ms 1300 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 4 ms 1232 KB Output is correct
19 Correct 361 ms 25228 KB Output is correct
20 Correct 354 ms 25200 KB Output is correct
21 Correct 285 ms 25200 KB Output is correct
22 Correct 292 ms 25228 KB Output is correct
23 Correct 286 ms 25372 KB Output is correct
24 Correct 329 ms 25228 KB Output is correct
25 Correct 317 ms 25232 KB Output is correct
26 Correct 367 ms 25232 KB Output is correct
27 Correct 404 ms 25268 KB Output is correct
28 Correct 388 ms 25160 KB Output is correct
29 Correct 392 ms 25560 KB Output is correct
30 Correct 411 ms 25232 KB Output is correct
31 Correct 398 ms 25276 KB Output is correct
32 Correct 422 ms 25136 KB Output is correct
33 Correct 406 ms 25184 KB Output is correct
34 Correct 319 ms 25232 KB Output is correct
35 Correct 324 ms 25260 KB Output is correct
36 Correct 314 ms 25236 KB Output is correct
37 Correct 326 ms 25260 KB Output is correct
38 Correct 322 ms 25148 KB Output is correct
39 Correct 318 ms 25260 KB Output is correct
40 Correct 208 ms 21948 KB Output is correct
41 Correct 270 ms 25140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 3 ms 588 KB Output is correct
12 Correct 4 ms 1040 KB Output is correct
13 Correct 4 ms 976 KB Output is correct
14 Correct 5 ms 1232 KB Output is correct
15 Correct 5 ms 1232 KB Output is correct
16 Correct 5 ms 1300 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 4 ms 1232 KB Output is correct
19 Correct 2950 ms 168364 KB Output is correct
20 Correct 2978 ms 168368 KB Output is correct
21 Correct 2999 ms 168372 KB Output is correct
22 Correct 2949 ms 168304 KB Output is correct
23 Execution timed out 3102 ms 168360 KB Time limit exceeded
24 Halted 0 ms 0 KB -