# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
203379 | Bruteforceman | Fire (JOI20_ho_t5) | C++17 | 1099 ms | 46724 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define prev previous
const int maxn = 200005;
using pii = pair <int, int>;
int n;
int s[maxn];
struct query {
int l, r, t;
} Q[maxn];
vector <int> v[maxn];
int prev[maxn];
int l[maxn], r[maxn];
vector <int> g[maxn];
int sub[maxn];
long long pred[maxn];
vector <pii> h[maxn];
void eulerOrder(int x) {
static int current = 0;
l[x] = ++current;
for(int i : g[x]) {
eulerOrder(i);
pred[i] = sub[x];
sub[x] += sub[i];
}
sub[x] += 1;
r[x] = current;
}
struct BinaryIndexedTree {
long long t[maxn];
int size;
void update(int x, long long val) {
x += 1;
for(int i = x; i <= size + 1; i += i & (-i)) {
t[i] += val;
}
}
long long sum(int x) {
x = min(x, size);
x += 1;
long long ans = 0;
for(int i = x; i > 0; i -= i & (-i)) {
ans += t[i];
}
return ans;
}
long long sum(int l, int r) {
if(l > r) return 0;
return sum(r) - sum(l - 1);
}
};
int main() {
int q;
scanf("%d %d", &n, &q);
for_each(s + 1, s + n + 1, [] (int &i) { scanf("%d", &i); });
for_each(Q + 1, Q + q + 1, [&] (query &i) {
scanf("%d %d %d", &i.t, &i.l, &i.r);
v[i.r].emplace_back(i.t);
v[i.l - 1].emplace_back(i.t); });
stack <int> st;
for(int i = 1; i <= n; i++) {
while(!st.empty() && s[st.top()] < s[i]) {
st.pop();
}
prev[i] = st.empty() ? 0 : st.top();
st.push(i);
}
for(int i = 1; i <= n; i++) {
g[prev[i]].push_back(i);
}
eulerOrder(0);
map <pii, long long> res;
auto cost = [&] (int x) {
if(prev[x] == 0) return 0;
else return s[prev[x]] - s[x];
};
BinaryIndexedTree A, B, C, D;
A.size = B.size = C.size = D.size = n;
vector <int> rmq;;
for(int i = 1; i <= n; i++) {
while(!rmq.empty() && s[rmq.back()] < s[i]) {
rmq.pop_back();
}
rmq.push_back(i);
for(int t : v[i]) {
if(res.find(pii(i, t)) != res.end()) continue;
int last = *lower_bound(rmq.begin(), rmq.end(), i - t);
long long ans = 1LL * s[last] * (i - last);
for(int k = 1; k <= last; k++) {
ans += s[k];
}
h[last].emplace_back(i, t);
res[pii(i, t)] += ans;
}
}
for(int i = 1; i <= n; i++) {
int k = i;
A.update(pred[k], 1LL * cost(k) * pred[k]);
B.update(pred[k], 1LL * cost(k));
C.update(pred[k] + sub[k], 1LL * cost(k) * (sub[k] + pred[k]));
D.update(pred[k] + sub[k], 1LL * cost(k));
for(auto j : h[i]) {
int t = j.second;
res[j] += B.sum(t) * t - A.sum(t);
res[j] += C.sum(t) - D.sum(t) * t;
}
}
for_each(Q + 1, Q + q + 1, [&] (query i) {
printf("%lld\n", res[pii(i.r, i.t)] - res[pii(i.l - 1, i.t)]); });
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |