Submission #519330

# Submission time Handle Problem Language Result Execution time Memory
519330 2022-01-26T08:03:18 Z Be_dos Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
1785 ms 139336 KB
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <unordered_set>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <random>
#include <cfloat>
#include <chrono>
#include <bitset>
#include <complex>
#include <immintrin.h>
#include <cassert>

struct Query {
  int32_t left, x;
  int32_t ind;

  Query(int32_t left_, int32_t x_, int32_t ind_) {
    left = left_;
    x = x_;
    ind = ind_;
  }
};

#define M_INF (-1'000'000'005)

struct Node {
  int32_t max_added = M_INF;
  int32_t max1 = M_INF;
  int32_t ans = M_INF;
};

struct Segtree {
  int32_t* arr;
  int32_t size_;
  Node* segtree;

  void build(int32_t node, int32_t left, int32_t right) {
    if(right - left == 1) {
      segtree[node].max1 = arr[left];
      return;
    }
    int32_t m = (left + right) / 2;
    build(node * 2 + 1, left, m);
    build(node * 2 + 2, m, right);
    segtree[node].max1 = std::max(segtree[node * 2 + 1].max1, segtree[node *  2 + 2].max1);
    segtree[node].ans = std::max(segtree[node].max_added + segtree[node].max1, std::max(segtree[node * 2 + 1].ans, segtree[node * 2 + 2].ans));
  }

  Segtree(int32_t n, int32_t* arr_) {
    arr = arr_;
    segtree = new Node[4 * n];
    build(0, 0, n);
    size_ = n;
  }

  void propagate(int32_t node, int32_t left, int32_t m, int32_t right) {
    segtree[node * 2 + 1].max_added = std::max(segtree[node * 2 + 1].max_added, segtree[node].max_added);
    segtree[node * 2 + 1].ans = std::max(segtree[node * 2 + 1].ans, segtree[node * 2 + 1].max_added + segtree[node * 2 + 1].max1);
    segtree[node * 2 + 2].max_added = std::max(segtree[node * 2 + 2].max_added, segtree[node].max_added);
    segtree[node * 2 + 2].ans = std::max(segtree[node * 2 + 2].ans, segtree[node * 2 + 2].max_added + segtree[node * 2 + 2].max1);
  }

  void max_eq(int32_t node, int32_t left, int32_t right, int32_t query_left, int32_t query_right, int32_t x) {
    if(left >= query_right || right <= query_left)
      return;
    if(left >= query_left && right <= query_right) {
      segtree[node].max_added = std::max(segtree[node].max_added, x);
      if(right - left == 1)
        segtree[node].ans = segtree[node].max_added + segtree[node].max1;
      else
        segtree[node].ans = std::max(segtree[node].max_added + segtree[node].max1,
                                     std::max(segtree[node * 2 + 1].ans, segtree[node * 2 + 2].ans));
      return;
    }
    int32_t m = (left + right) / 2;
    propagate(node, left, m, right);
    max_eq(node * 2 + 1, left, m, query_left, query_right, x);
    max_eq(node * 2 + 2, m, right, query_left, query_right, x);
    segtree[node].ans = std::max(segtree[node].max_added + segtree[node].max1,
                                 std::max(segtree[node * 2 + 1].ans, segtree[node * 2 + 2].ans));
  }

  void max_eq(int32_t left, int32_t right, int32_t x) {
    max_eq(0, 0, size_, left, right, x);
  }

  int32_t get(int32_t node, int32_t left, int32_t right, int32_t query_left, int32_t query_right) {
    if(left >= query_right || right <= query_left)
      return M_INF;
    if(left >= query_left && right <= query_right)
      return segtree[node].ans;
    int32_t m = (left + right) / 2;
    propagate(node, left, m, right);
    return std::max(get(node * 2 + 1, left, m, query_left, query_right),
                    get(node * 2 + 2, m, right, query_left, query_right));
  }

  int32_t get(int32_t left, int32_t right) {
    return get(0, 0, size_, left, right);
  }
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int32_t n, num_q;
  std::cin >> n >> num_q;

  int32_t* arr = new int32_t[n];
  for(int32_t i = 0; i < n; i++)
    std::cin >> arr[i];

  std::vector<Query>* queries = new std::vector<Query>[n];
  int32_t* bounds = new int32_t[num_q];
  for(int32_t q = 0; q < num_q; q++) {
    int32_t left, right, x;
    std::cin >> left >> right >> x;
    left--;
    right--;
    bounds[q] = x;

    queries[right].emplace_back(left, x, q);
  }

  Segtree tree(n, arr);
  std::stack<int32_t> maximums;
  int32_t* ans = new int32_t[num_q];
  for(int32_t i = 0; i < n; i++) {
    while(!maximums.empty() && arr[maximums.top()] <= arr[i])
      maximums.pop();
    if(!maximums.empty())
      tree.max_eq(0, maximums.top() + 1, arr[i]);
    maximums.push(i);

    for(int32_t q = 0; q < queries[i].size(); q++)
      ans[queries[i][q].ind] = tree.get(queries[i][q].left, i + 1);
  }

  for(int32_t q = 0; q < num_q; q++)
    std::cout << (ans[q] > bounds[q] ? 0 : 1) << "\n";
  return 0;
}

Compilation message

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:147:26: warning: comparison of integer expressions of different signedness: 'int32_t' {aka 'int'} and 'std::vector<Query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |     for(int32_t q = 0; q < queries[i].size(); q++)
      |                        ~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 320 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 320 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 824 KB Output is correct
13 Correct 4 ms 840 KB Output is correct
14 Correct 7 ms 932 KB Output is correct
15 Correct 7 ms 956 KB Output is correct
16 Correct 6 ms 844 KB Output is correct
17 Correct 5 ms 836 KB Output is correct
18 Correct 4 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1697 ms 106328 KB Output is correct
2 Correct 1606 ms 106668 KB Output is correct
3 Correct 1611 ms 106576 KB Output is correct
4 Correct 1658 ms 106588 KB Output is correct
5 Correct 1231 ms 106652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 12716 KB Output is correct
2 Correct 103 ms 12556 KB Output is correct
3 Correct 84 ms 12740 KB Output is correct
4 Correct 82 ms 12744 KB Output is correct
5 Correct 82 ms 12824 KB Output is correct
6 Correct 73 ms 12228 KB Output is correct
7 Correct 76 ms 12184 KB Output is correct
8 Correct 96 ms 12284 KB Output is correct
9 Correct 41 ms 4068 KB Output is correct
10 Correct 82 ms 12284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 320 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 824 KB Output is correct
13 Correct 4 ms 840 KB Output is correct
14 Correct 7 ms 932 KB Output is correct
15 Correct 7 ms 956 KB Output is correct
16 Correct 6 ms 844 KB Output is correct
17 Correct 5 ms 836 KB Output is correct
18 Correct 4 ms 844 KB Output is correct
19 Correct 254 ms 23392 KB Output is correct
20 Correct 284 ms 26048 KB Output is correct
21 Correct 246 ms 25636 KB Output is correct
22 Correct 290 ms 25600 KB Output is correct
23 Correct 274 ms 25632 KB Output is correct
24 Correct 186 ms 25708 KB Output is correct
25 Correct 189 ms 25732 KB Output is correct
26 Correct 244 ms 25820 KB Output is correct
27 Correct 233 ms 25764 KB Output is correct
28 Correct 235 ms 25684 KB Output is correct
29 Correct 256 ms 25928 KB Output is correct
30 Correct 221 ms 25924 KB Output is correct
31 Correct 219 ms 25976 KB Output is correct
32 Correct 208 ms 25988 KB Output is correct
33 Correct 251 ms 25976 KB Output is correct
34 Correct 172 ms 25260 KB Output is correct
35 Correct 175 ms 25240 KB Output is correct
36 Correct 161 ms 25224 KB Output is correct
37 Correct 164 ms 25156 KB Output is correct
38 Correct 173 ms 25160 KB Output is correct
39 Correct 203 ms 25916 KB Output is correct
40 Correct 131 ms 19984 KB Output is correct
41 Correct 196 ms 25760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 320 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 824 KB Output is correct
13 Correct 4 ms 840 KB Output is correct
14 Correct 7 ms 932 KB Output is correct
15 Correct 7 ms 956 KB Output is correct
16 Correct 6 ms 844 KB Output is correct
17 Correct 5 ms 836 KB Output is correct
18 Correct 4 ms 844 KB Output is correct
19 Correct 1697 ms 106328 KB Output is correct
20 Correct 1606 ms 106668 KB Output is correct
21 Correct 1611 ms 106576 KB Output is correct
22 Correct 1658 ms 106588 KB Output is correct
23 Correct 1231 ms 106652 KB Output is correct
24 Correct 111 ms 12716 KB Output is correct
25 Correct 103 ms 12556 KB Output is correct
26 Correct 84 ms 12740 KB Output is correct
27 Correct 82 ms 12744 KB Output is correct
28 Correct 82 ms 12824 KB Output is correct
29 Correct 73 ms 12228 KB Output is correct
30 Correct 76 ms 12184 KB Output is correct
31 Correct 96 ms 12284 KB Output is correct
32 Correct 41 ms 4068 KB Output is correct
33 Correct 82 ms 12284 KB Output is correct
34 Correct 254 ms 23392 KB Output is correct
35 Correct 284 ms 26048 KB Output is correct
36 Correct 246 ms 25636 KB Output is correct
37 Correct 290 ms 25600 KB Output is correct
38 Correct 274 ms 25632 KB Output is correct
39 Correct 186 ms 25708 KB Output is correct
40 Correct 189 ms 25732 KB Output is correct
41 Correct 244 ms 25820 KB Output is correct
42 Correct 233 ms 25764 KB Output is correct
43 Correct 235 ms 25684 KB Output is correct
44 Correct 256 ms 25928 KB Output is correct
45 Correct 221 ms 25924 KB Output is correct
46 Correct 219 ms 25976 KB Output is correct
47 Correct 208 ms 25988 KB Output is correct
48 Correct 251 ms 25976 KB Output is correct
49 Correct 172 ms 25260 KB Output is correct
50 Correct 175 ms 25240 KB Output is correct
51 Correct 161 ms 25224 KB Output is correct
52 Correct 164 ms 25156 KB Output is correct
53 Correct 173 ms 25160 KB Output is correct
54 Correct 203 ms 25916 KB Output is correct
55 Correct 131 ms 19984 KB Output is correct
56 Correct 196 ms 25760 KB Output is correct
57 Correct 1704 ms 139248 KB Output is correct
58 Correct 1667 ms 139276 KB Output is correct
59 Correct 1577 ms 137700 KB Output is correct
60 Correct 1778 ms 137684 KB Output is correct
61 Correct 1785 ms 137648 KB Output is correct
62 Correct 1606 ms 137668 KB Output is correct
63 Correct 965 ms 134496 KB Output is correct
64 Correct 936 ms 134524 KB Output is correct
65 Correct 1289 ms 137548 KB Output is correct
66 Correct 1359 ms 137624 KB Output is correct
67 Correct 1453 ms 137712 KB Output is correct
68 Correct 1306 ms 139336 KB Output is correct
69 Correct 1375 ms 139284 KB Output is correct
70 Correct 1278 ms 138940 KB Output is correct
71 Correct 1267 ms 138936 KB Output is correct
72 Correct 1351 ms 139056 KB Output is correct
73 Correct 923 ms 130340 KB Output is correct
74 Correct 961 ms 131288 KB Output is correct
75 Correct 867 ms 130340 KB Output is correct
76 Correct 950 ms 130616 KB Output is correct
77 Correct 909 ms 130240 KB Output is correct
78 Correct 1210 ms 131448 KB Output is correct
79 Correct 735 ms 88864 KB Output is correct
80 Correct 1208 ms 128508 KB Output is correct