Submission #1042475

# Submission time Handle Problem Language Result Execution time Memory
1042475 2024-08-03T05:13:33 Z juicy Fire (JOI20_ho_t5) C++17
100 / 100
201 ms 55288 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 2e5 + 5;

int n, q;
int a[N], lt[N];
long long res[N];
vector<array<int, 3>> events[N], Queries[N];

struct Fenwick {
  long long s[2 * N];

  void upd(int i, long long x) {
    for (; i <= 2 * n; i += i & -i) {
      s[i] += x;
    }
  }

  void upd(int l, int r, long long x) {
    upd(l, x);
    upd(r + 1, -x);
  }

  long long qry(int i) {
    long long res = 0;
    for (; i; i -= i & -i) {
      res += s[i];
    }
    return res;
  }

  long long qry(int l, int r) {
    return qry(r) - qry(l - 1);
  }
} ft[5];

void upd(int l, int r, int x) {
  int len = r - l + 1;
  ft[0].upd(r + 1, (long long) x * len);
  ft[0].upd(l, r, (long long) -l * x);
  ft[1].upd(r + 1, -x);
  ft[2].upd(l, r, x);
  ft[3].upd(l, -x);
  ft[4].upd(l, (long long) x * l);
}

long long qry(int i, int t) {
  i += n;
  long long x = ft[2].qry(i) + ft[3].qry(i - t + 1, i);
  return ft[0].qry(i) + ft[1].qry(i) * t + x * (i + 1 - t) + ft[4].qry(i - t + 1, i);
}

void add(int l, int r, int h, int x) {
  l += n, r += n;
  events[0].push_back({l, r, x});
  if (h <= n) {
    events[h].push_back({l, r, -x});
  }
}

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

  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= q; ++i) {
    int l, r, t; cin >> t >> l >> r;
    Queries[t].push_back({l, r, i});
  }
  vector<int> st = {-n};
  for (int i = 1; i <= n; ++i) {
    while (st.size() > 1 && a[st.back()] <= a[i]) {
      st.pop_back();
    }
    lt[i] = st.back();
    st.push_back(i);
  }
  st.clear();
  st.push_back(n + 1);
  for (int i = n; i > 0; --i) {
    while (st.size() > 1 && a[st.back()] < a[i]) {
      st.pop_back();
    }
    int r = st.back();
    if (lt[i] + 1 < i) {
      add(lt[i] + 1, i - 1, i - lt[i] - 1, -a[i]);
    }
    if (i < r - 1) {
      add(i + 1, r - 1, r - i - 1, -a[i]);
    }
    add(lt[i] + 1, r - 1, r - lt[i] - 1, a[i]);
    st.push_back(i);
  }
  for (int t = 0; t <= n; ++t) {
    for (auto [l, r, x] : events[t]) {
      upd(l, r, x);
    }
    for (auto [l, r, id] : Queries[t]) {
      res[id] = qry(r, t) - qry(l - 1, t);
    }
  }
  for (int i = 1; i <= q; ++i) {
    cout << res[i] << "\n";
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 20824 KB Output is correct
2 Correct 2 ms 20828 KB Output is correct
3 Correct 2 ms 20828 KB Output is correct
4 Correct 2 ms 20828 KB Output is correct
5 Correct 2 ms 20824 KB Output is correct
6 Correct 3 ms 20828 KB Output is correct
7 Correct 2 ms 20828 KB Output is correct
8 Correct 2 ms 20828 KB Output is correct
9 Correct 3 ms 20824 KB Output is correct
10 Correct 2 ms 20828 KB Output is correct
11 Correct 2 ms 20828 KB Output is correct
12 Correct 2 ms 20828 KB Output is correct
13 Correct 3 ms 20824 KB Output is correct
14 Correct 3 ms 20828 KB Output is correct
15 Correct 3 ms 20828 KB Output is correct
16 Correct 2 ms 20828 KB Output is correct
17 Correct 3 ms 20828 KB Output is correct
18 Correct 3 ms 20828 KB Output is correct
19 Correct 3 ms 20828 KB Output is correct
20 Correct 2 ms 20828 KB Output is correct
21 Correct 2 ms 20828 KB Output is correct
22 Correct 2 ms 20828 KB Output is correct
23 Correct 3 ms 20932 KB Output is correct
24 Correct 2 ms 20920 KB Output is correct
25 Correct 2 ms 20828 KB Output is correct
26 Correct 2 ms 20828 KB Output is correct
27 Correct 2 ms 20828 KB Output is correct
28 Correct 2 ms 20828 KB Output is correct
29 Correct 2 ms 20828 KB Output is correct
30 Correct 2 ms 20932 KB Output is correct
31 Correct 2 ms 20828 KB Output is correct
32 Correct 3 ms 20828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 20824 KB Output is correct
2 Correct 144 ms 43968 KB Output is correct
3 Correct 140 ms 50872 KB Output is correct
4 Correct 152 ms 49856 KB Output is correct
5 Correct 150 ms 51416 KB Output is correct
6 Correct 158 ms 49092 KB Output is correct
7 Correct 142 ms 50100 KB Output is correct
8 Correct 158 ms 52300 KB Output is correct
9 Correct 155 ms 51324 KB Output is correct
10 Correct 156 ms 50096 KB Output is correct
11 Correct 162 ms 51644 KB Output is correct
12 Correct 133 ms 49592 KB Output is correct
13 Correct 145 ms 51736 KB Output is correct
14 Correct 148 ms 52148 KB Output is correct
15 Correct 169 ms 52388 KB Output is correct
16 Correct 139 ms 51388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 20824 KB Output is correct
2 Correct 159 ms 45780 KB Output is correct
3 Correct 152 ms 51188 KB Output is correct
4 Correct 162 ms 54648 KB Output is correct
5 Correct 160 ms 50420 KB Output is correct
6 Correct 159 ms 50424 KB Output is correct
7 Correct 189 ms 53444 KB Output is correct
8 Correct 160 ms 50676 KB Output is correct
9 Correct 152 ms 50928 KB Output is correct
10 Correct 152 ms 49908 KB Output is correct
11 Correct 170 ms 52984 KB Output is correct
12 Correct 165 ms 54000 KB Output is correct
13 Correct 176 ms 53240 KB Output is correct
14 Correct 154 ms 51028 KB Output is correct
15 Correct 172 ms 53240 KB Output is correct
16 Correct 165 ms 55288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 43252 KB Output is correct
2 Correct 170 ms 47344 KB Output is correct
3 Correct 188 ms 48212 KB Output is correct
4 Correct 172 ms 47244 KB Output is correct
5 Correct 178 ms 48368 KB Output is correct
6 Correct 180 ms 47704 KB Output is correct
7 Correct 197 ms 47344 KB Output is correct
8 Correct 180 ms 47856 KB Output is correct
9 Correct 176 ms 46924 KB Output is correct
10 Correct 180 ms 47604 KB Output is correct
11 Correct 181 ms 47124 KB Output is correct
12 Correct 177 ms 47856 KB Output is correct
13 Correct 201 ms 46836 KB Output is correct
14 Correct 179 ms 48372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 20824 KB Output is correct
2 Correct 2 ms 20828 KB Output is correct
3 Correct 2 ms 20828 KB Output is correct
4 Correct 2 ms 20828 KB Output is correct
5 Correct 2 ms 20824 KB Output is correct
6 Correct 3 ms 20828 KB Output is correct
7 Correct 2 ms 20828 KB Output is correct
8 Correct 2 ms 20828 KB Output is correct
9 Correct 3 ms 20824 KB Output is correct
10 Correct 2 ms 20828 KB Output is correct
11 Correct 2 ms 20828 KB Output is correct
12 Correct 2 ms 20828 KB Output is correct
13 Correct 3 ms 20824 KB Output is correct
14 Correct 3 ms 20828 KB Output is correct
15 Correct 3 ms 20828 KB Output is correct
16 Correct 2 ms 20828 KB Output is correct
17 Correct 3 ms 20828 KB Output is correct
18 Correct 3 ms 20828 KB Output is correct
19 Correct 3 ms 20828 KB Output is correct
20 Correct 2 ms 20828 KB Output is correct
21 Correct 2 ms 20828 KB Output is correct
22 Correct 2 ms 20828 KB Output is correct
23 Correct 3 ms 20932 KB Output is correct
24 Correct 2 ms 20920 KB Output is correct
25 Correct 2 ms 20828 KB Output is correct
26 Correct 2 ms 20828 KB Output is correct
27 Correct 2 ms 20828 KB Output is correct
28 Correct 2 ms 20828 KB Output is correct
29 Correct 2 ms 20828 KB Output is correct
30 Correct 2 ms 20932 KB Output is correct
31 Correct 2 ms 20828 KB Output is correct
32 Correct 3 ms 20828 KB Output is correct
33 Correct 144 ms 43968 KB Output is correct
34 Correct 140 ms 50872 KB Output is correct
35 Correct 152 ms 49856 KB Output is correct
36 Correct 150 ms 51416 KB Output is correct
37 Correct 158 ms 49092 KB Output is correct
38 Correct 142 ms 50100 KB Output is correct
39 Correct 158 ms 52300 KB Output is correct
40 Correct 155 ms 51324 KB Output is correct
41 Correct 156 ms 50096 KB Output is correct
42 Correct 162 ms 51644 KB Output is correct
43 Correct 133 ms 49592 KB Output is correct
44 Correct 145 ms 51736 KB Output is correct
45 Correct 148 ms 52148 KB Output is correct
46 Correct 169 ms 52388 KB Output is correct
47 Correct 139 ms 51388 KB Output is correct
48 Correct 159 ms 45780 KB Output is correct
49 Correct 152 ms 51188 KB Output is correct
50 Correct 162 ms 54648 KB Output is correct
51 Correct 160 ms 50420 KB Output is correct
52 Correct 159 ms 50424 KB Output is correct
53 Correct 189 ms 53444 KB Output is correct
54 Correct 160 ms 50676 KB Output is correct
55 Correct 152 ms 50928 KB Output is correct
56 Correct 152 ms 49908 KB Output is correct
57 Correct 170 ms 52984 KB Output is correct
58 Correct 165 ms 54000 KB Output is correct
59 Correct 176 ms 53240 KB Output is correct
60 Correct 154 ms 51028 KB Output is correct
61 Correct 172 ms 53240 KB Output is correct
62 Correct 165 ms 55288 KB Output is correct
63 Correct 173 ms 43252 KB Output is correct
64 Correct 170 ms 47344 KB Output is correct
65 Correct 188 ms 48212 KB Output is correct
66 Correct 172 ms 47244 KB Output is correct
67 Correct 178 ms 48368 KB Output is correct
68 Correct 180 ms 47704 KB Output is correct
69 Correct 197 ms 47344 KB Output is correct
70 Correct 180 ms 47856 KB Output is correct
71 Correct 176 ms 46924 KB Output is correct
72 Correct 180 ms 47604 KB Output is correct
73 Correct 181 ms 47124 KB Output is correct
74 Correct 177 ms 47856 KB Output is correct
75 Correct 201 ms 46836 KB Output is correct
76 Correct 179 ms 48372 KB Output is correct
77 Correct 163 ms 51704 KB Output is correct
78 Correct 179 ms 52336 KB Output is correct
79 Correct 172 ms 55040 KB Output is correct
80 Correct 183 ms 53064 KB Output is correct
81 Correct 159 ms 52720 KB Output is correct
82 Correct 181 ms 53620 KB Output is correct
83 Correct 166 ms 52984 KB Output is correct
84 Correct 162 ms 51444 KB Output is correct
85 Correct 199 ms 53704 KB Output is correct
86 Correct 167 ms 52724 KB Output is correct
87 Correct 169 ms 54220 KB Output is correct
88 Correct 168 ms 54008 KB Output is correct
89 Correct 151 ms 51336 KB Output is correct
90 Correct 170 ms 53196 KB Output is correct
91 Correct 150 ms 52140 KB Output is correct
92 Correct 152 ms 52476 KB Output is correct
93 Correct 153 ms 50676 KB Output is correct
94 Correct 166 ms 54332 KB Output is correct
95 Correct 159 ms 54004 KB Output is correct
96 Correct 164 ms 51956 KB Output is correct
97 Correct 170 ms 51088 KB Output is correct
98 Correct 157 ms 50948 KB Output is correct
99 Correct 144 ms 52228 KB Output is correct
100 Correct 149 ms 52980 KB Output is correct
101 Correct 152 ms 50500 KB Output is correct
102 Correct 157 ms 54512 KB Output is correct