답안 #880697

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
880697 2023-11-29T21:59:54 Z MilosMilutinovic Fire (JOI20_ho_t5) C++14
100 / 100
340 ms 64176 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  vector<int> t(q), l(q), r(q);
  for (int i = 0; i < q; i++) {
    cin >> t[i] >> l[i] >> r[i];
    --l[i]; --r[i];
    if (t[i] == n) {
      --t[i];
    }
  }
  vector<int> prv(n);
  vector<int> stk;
  for (int i = 0; i < n; i++) {
    while (!stk.empty() && s[i] >= s[stk.back()]) {
      stk.pop_back();
    }
    prv[i] = (stk.empty() ? -1 : stk.back());
    stk.push_back(i);
  }
  vector<vector<int>> ch(n);
  for (int i = 0; i < n; i++) {
    if (prv[i] != -1) {
      ch[prv[i]].push_back(i);
    }
  }
  vector<int> to(n);
  for (int i = n - 1; i >= 0; i--) {
    to[i] = i;
    for (int j : ch[i]) {
      to[i] = max(to[i], to[j]);
    }
  }
  vector<long long> res(q);
  {
    vector<long long> pref(n);
    for (int i = 0; i < n; i++) {
      if (i > 0) {
        pref[i] = pref[i - 1];
      }
      pref[i] += s[i];
    }
    for (int i = 0; i < q; i++) {
      res[i] = pref[r[i]] - (l[i] == 0 ? 0LL : pref[l[i] - 1]);
    }
  }
  vector<vector<array<int, 3>>> seg(n);
  for (int i = 0; i < n; i++) {
    for (int j : ch[i]) {
      seg[to[j]].push_back({s[i] - s[j], j - i, to[j] - i});
    }
  }
  vector<vector<vector<int>>> qs(n, vector<vector<int>>(2));
  for (int i = 0; i < q; i++) {
    if (l[i] > 0) {
      qs[l[i] - 1][0].push_back(i);
    }
    qs[r[i]][1].push_back(i);
  }
  vector<vector<long long>> fenw(4, vector<long long>(n));
  auto Modify = [&](int t, int x, long long v) {
    assert(0 <= x);
    while (x < n) {
      fenw[t][x] += v;
      x |= (x + 1);
    }
  };
  auto Query = [&](int t, int x) {
    assert(x < n);
    long long v = 0;
    while (x >= 0) {
      v += fenw[t][x];
      x = (x & (x + 1)) - 1;
    }
    return v;
  };
  auto QueryRange = [&](int t, int l, int r) {
    return Query(t, r) - Query(t, l - 1);
  };
  auto AddInc = [&](int L, int R, int v) {
    Modify(0, L, +v);
    Modify(1, L, (L - 1) * 1LL * v);
    Modify(0, R, -v);
    Modify(1, R, -R * 1LL * v);
  };
  auto Ins = [&](int i, int j) {
    AddInc(j - i, n - 1, s[i] - s[j]);
    Modify(2, i, s[i] - s[j]);
    Modify(3, i, i * 1LL * (s[i] - s[j]));
  }; 
  auto Rem = [&](int i, int j) {
    AddInc(j - i, n - 1, s[j] - s[i]);
    Modify(2, i, s[j] - s[i]);
    Modify(3, i, i * 1LL * (s[j] - s[i]));
  };
  auto Solve = [&](int i, int k) {
    long long ret = 0;
    ret += Query(0, k) * k;
    ret -= Query(1, k);
    int low = 0, high = (int) stk.size() - 1, pos = -1;
    while (low <= high) {
      int mid = low + high >> 1;
      if (i - stk[mid] < k) {
        pos = mid;
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    if (pos != -1) {
      ret -= (k - i) * QueryRange(2, stk[pos], i); 
      ret -= QueryRange(3, stk[pos], i);
    }
    return ret;
  };
  stk.clear();
  for (int i = 0; i < n; i++) {
    while (!stk.empty() && s[stk.back()] <= s[i]) {
      if (stk.size() > 1) {
        Rem(stk[stk.size() - 2], stk.back());
      }
      stk.pop_back();
    }
    if (!stk.empty()) {
      Ins(stk.back(), i);
    }
    stk.push_back(i);
    for (int j : qs[i][0]) {
      res[j] -= Solve(i, t[j]);
    }
    for (int j : qs[i][1]) {
      res[j] += Solve(i, t[j]);
    }
    for (auto& p : seg[i]) {
      AddInc(p[1], p[2], p[0]);
    }
  }
  for (int i = 0; i < q; i++) {
    cout << res[i] << '\n';
  }
  return 0;
}

Compilation message

ho_t5.cpp: In lambda function:
ho_t5.cpp:112:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  112 |       int mid = low + high >> 1;
      |                 ~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 452 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 452 KB Output is correct
19 Correct 1 ms 780 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 314 ms 62952 KB Output is correct
3 Correct 263 ms 62468 KB Output is correct
4 Correct 292 ms 62820 KB Output is correct
5 Correct 276 ms 63548 KB Output is correct
6 Correct 270 ms 62800 KB Output is correct
7 Correct 283 ms 63556 KB Output is correct
8 Correct 262 ms 63800 KB Output is correct
9 Correct 300 ms 63436 KB Output is correct
10 Correct 264 ms 62264 KB Output is correct
11 Correct 270 ms 63816 KB Output is correct
12 Correct 305 ms 62080 KB Output is correct
13 Correct 299 ms 63244 KB Output is correct
14 Correct 259 ms 63044 KB Output is correct
15 Correct 320 ms 63300 KB Output is correct
16 Correct 282 ms 62812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 277 ms 62564 KB Output is correct
3 Correct 264 ms 61628 KB Output is correct
4 Correct 283 ms 63756 KB Output is correct
5 Correct 267 ms 62048 KB Output is correct
6 Correct 270 ms 62644 KB Output is correct
7 Correct 322 ms 62904 KB Output is correct
8 Correct 267 ms 62568 KB Output is correct
9 Correct 265 ms 61800 KB Output is correct
10 Correct 263 ms 61312 KB Output is correct
11 Correct 284 ms 63800 KB Output is correct
12 Correct 289 ms 63044 KB Output is correct
13 Correct 320 ms 63456 KB Output is correct
14 Correct 267 ms 61924 KB Output is correct
15 Correct 276 ms 63588 KB Output is correct
16 Correct 267 ms 62968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 52140 KB Output is correct
2 Correct 228 ms 56680 KB Output is correct
3 Correct 250 ms 57896 KB Output is correct
4 Correct 250 ms 56188 KB Output is correct
5 Correct 236 ms 56540 KB Output is correct
6 Correct 241 ms 56656 KB Output is correct
7 Correct 226 ms 57664 KB Output is correct
8 Correct 243 ms 57164 KB Output is correct
9 Correct 231 ms 56428 KB Output is correct
10 Correct 228 ms 57112 KB Output is correct
11 Correct 235 ms 56940 KB Output is correct
12 Correct 241 ms 57044 KB Output is correct
13 Correct 253 ms 56920 KB Output is correct
14 Correct 249 ms 56828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 452 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 452 KB Output is correct
19 Correct 1 ms 780 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 314 ms 62952 KB Output is correct
34 Correct 263 ms 62468 KB Output is correct
35 Correct 292 ms 62820 KB Output is correct
36 Correct 276 ms 63548 KB Output is correct
37 Correct 270 ms 62800 KB Output is correct
38 Correct 283 ms 63556 KB Output is correct
39 Correct 262 ms 63800 KB Output is correct
40 Correct 300 ms 63436 KB Output is correct
41 Correct 264 ms 62264 KB Output is correct
42 Correct 270 ms 63816 KB Output is correct
43 Correct 305 ms 62080 KB Output is correct
44 Correct 299 ms 63244 KB Output is correct
45 Correct 259 ms 63044 KB Output is correct
46 Correct 320 ms 63300 KB Output is correct
47 Correct 282 ms 62812 KB Output is correct
48 Correct 277 ms 62564 KB Output is correct
49 Correct 264 ms 61628 KB Output is correct
50 Correct 283 ms 63756 KB Output is correct
51 Correct 267 ms 62048 KB Output is correct
52 Correct 270 ms 62644 KB Output is correct
53 Correct 322 ms 62904 KB Output is correct
54 Correct 267 ms 62568 KB Output is correct
55 Correct 265 ms 61800 KB Output is correct
56 Correct 263 ms 61312 KB Output is correct
57 Correct 284 ms 63800 KB Output is correct
58 Correct 289 ms 63044 KB Output is correct
59 Correct 320 ms 63456 KB Output is correct
60 Correct 267 ms 61924 KB Output is correct
61 Correct 276 ms 63588 KB Output is correct
62 Correct 267 ms 62968 KB Output is correct
63 Correct 236 ms 52140 KB Output is correct
64 Correct 228 ms 56680 KB Output is correct
65 Correct 250 ms 57896 KB Output is correct
66 Correct 250 ms 56188 KB Output is correct
67 Correct 236 ms 56540 KB Output is correct
68 Correct 241 ms 56656 KB Output is correct
69 Correct 226 ms 57664 KB Output is correct
70 Correct 243 ms 57164 KB Output is correct
71 Correct 231 ms 56428 KB Output is correct
72 Correct 228 ms 57112 KB Output is correct
73 Correct 235 ms 56940 KB Output is correct
74 Correct 241 ms 57044 KB Output is correct
75 Correct 253 ms 56920 KB Output is correct
76 Correct 249 ms 56828 KB Output is correct
77 Correct 319 ms 62636 KB Output is correct
78 Correct 319 ms 63472 KB Output is correct
79 Correct 275 ms 63808 KB Output is correct
80 Correct 290 ms 62516 KB Output is correct
81 Correct 283 ms 62532 KB Output is correct
82 Correct 317 ms 63152 KB Output is correct
83 Correct 280 ms 62740 KB Output is correct
84 Correct 277 ms 62076 KB Output is correct
85 Correct 314 ms 63556 KB Output is correct
86 Correct 292 ms 62544 KB Output is correct
87 Correct 306 ms 63872 KB Output is correct
88 Correct 272 ms 63924 KB Output is correct
89 Correct 295 ms 62048 KB Output is correct
90 Correct 306 ms 63536 KB Output is correct
91 Correct 287 ms 62516 KB Output is correct
92 Correct 289 ms 61744 KB Output is correct
93 Correct 270 ms 62804 KB Output is correct
94 Correct 287 ms 64176 KB Output is correct
95 Correct 310 ms 64128 KB Output is correct
96 Correct 270 ms 63016 KB Output is correct
97 Correct 340 ms 63296 KB Output is correct
98 Correct 267 ms 62004 KB Output is correct
99 Correct 301 ms 62764 KB Output is correct
100 Correct 271 ms 62932 KB Output is correct
101 Correct 301 ms 62260 KB Output is correct
102 Correct 292 ms 63560 KB Output is correct