#include <bits/stdc++.h>
#define pb push_back
#define sz(x) (int)(x).size()
#define trav(x, a) for (const auto& x: a)
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define st first
#define nd second
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
const int inf = 1e9 + 7;
int nxL[N], nxH[N];
array<int,3> arr[N];
vector<array<int,3>> upd[N];
ll ans[N];
struct segment_tree {
struct data {
ll s=0, l=1;
data() {}
data(ll s, ll l) : s(s), l(l) {}
};
typedef ll operation;
static data combine(data dl, data dr) {
return {dl.s + dr.s, dl.l + dr.l};
}
static data calculate(operation o, data d) {
return {o * d.l + d.s, d.l};
}
static operation merge(operation ot, operation ob) {
return ot + ob;
}
int n, h;
vector<data> t;
vector<operation> o;
segment_tree(int n = 0) : n(n), h(32 - __builtin_clz(n)), t(2 * n), o(n) {
for (int i = 0; i < n; i++)
t[i + n] = {0, 1};
for (int x = n - 1; x > 0; x--)
t[x] = combine(t[x << 1], t[x << 1 | 1]);
}
segment_tree(vector<data> & a) : segment_tree(a.size()) {
for (int i = 0; i < n; i++)
t[i + n] = a[i];
for (int x = n - 1; x > 0; x--)
t[x] = combine(t[x << 1], t[x << 1 | 1]);
}
void apply(int x, operation op) {
t[x] = calculate(op, t[x]);
if (x < n)
o[x] = merge(op, o[x]);
}
void push(int x) {
for (int s = h; s > 0; s--) {
int c = x >> s;
apply(c << 1, o[c]);
apply(c << 1 | 1, o[c]);
o[c] = operation();
}
}
void build(int x) {
while (x >>= 1)
t[x] = calculate(o[x], combine(t[x << 1], t[x << 1 | 1]));
}
// set the data at the position i
void setValue(int i, data d) {
i += n;
push(i);
t[i] = d;
build(i);
}
// query the data on the range [l, r[
ll query(int l, int r) {
l += n; r += n;
push(l); push(r - 1);
data dl, dr;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1)
dl = combine(dl, t[l++]);
if (r & 1)
dr = combine(t[--r], dr);
}
return combine(dl, dr).s;
}
// apply an operation on the range [l, r[
void apply(int l, int r, operation op) {
l += n; r += n;
push(l); push(r - 1);
int xl = l, xr = r;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1)
apply(l++, op);
if (r & 1)
apply(--r, op);
}
build(xl); build(xr - 1);
}
};
#define log2(x) (31 - __builtin_clz(x))
struct sparse_table {
int n;
vector<segment_tree::data> a;
vector<vector<int>> st;
int combine(int dl, int dr) {
return a[dl].s > a[dr].s ? dl : dr;
}
sparse_table() {}
sparse_table(vector<segment_tree::data> & a) : n(a.size()), a(a), st(log2(n) + 1, vector<int>(n)) {
for (int i = 0; i < n; i++)
st[0][i] = i;
for (int j = 1; 1 << j <= n; j++)
for (int i = 0; i + (1 << j) <= n; i++)
st[j][i] = combine(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}
// query the data on the range [l, r[
int query(int l, int r) {
int s = log2(r - l);
return combine(st[s][l], st[s][r - (1 << s)]);
}
};
int main() {
int n, q; scanf("%d %d", &n, &q);
int n2 = n+2;
while (n2 & (n2-1)) n2++;
vector<segment_tree::data> s(n2);
s[0] = s[n+1] = {inf, 1};
for (int i = 1; i <= n; i++) {
scanf("%lld", &s[i].s);
s[i].l = 1;
}
stack<int> st; st.push(n+1);
for (int i = n; i; i--) {
while (!st.empty() && s[st.top()].s < s[i].s)
st.pop();
nxH[i] = st.top();
st.push(i);
}
while (!st.empty()) st.pop();
st.push(0);
for (int i = 1; i <= n; i++) {
while (!st.empty() && s[st.top()].s < s[i].s)
st.pop();
nxL[i] = st.top();
st.push(i);
}
sparse_table sp(s);
segment_tree f(n2);
for (int i = 1; i <= n; i++) {
if (i+1 <= nxH[i]-1) {
int v = s[i].s - s[sp.query(i+1, nxH[i])].s;
f.apply(i, n+1, v);
upd[nxH[i]-i].pb({i+1, nxH[i]-1, v});
}
if (nxL[i]+1 <= i-1 && nxL[i] != 0 && s[nxL[i]].s != s[i].s) {
int v = s[i].s - s[sp.query(nxL[i]+1, i)].s;
f.apply(nxL[i], n+1, v);
upd[i-nxL[i]].pb({nxL[i]+1, i-1, v});
}
}
segment_tree seg(s);
for (int i = 0; i < q; i++) {
for (int j = 0; j < 3; j++)
scanf("%d", &arr[i][j]);
upd[arr[i][0]].pb({-i, arr[i][1], arr[i][2]});
}
for (int i = 1; i <= n; i++) {
trav(j, upd[i]) {
if (j[0] > 0) {
seg.apply(j[0], j[1]+1, j[2]);
f.apply(j[0]-1, n+1, -j[2]);
}
}
trav(j, upd[i]) {
if (j[0] <= 0) {
ans[-j[0]] = seg.query(j[1], j[2]+1);
ans[-j[0]] += f.query(max(0,j[2]-i), j[2]-1+1) - f.query(max(0,j[1]-i-1), max(0,j[1]-2)+1);
}
}
}
for (int i = 0; i < q; i++)
printf("%lld\n", ans[i]);
}
Compilation message
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:126:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n, q; scanf("%d %d", &n, &q);
~~~~~^~~~~~~~~~~~~~~~~
ho_t5.cpp:132:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &s[i].s);
~~~~~^~~~~~~~~~~~~~~~~
ho_t5.cpp:167:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[i][j]);
~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
8 ms |
5116 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
9 ms |
5112 KB |
Output is correct |
13 |
Correct |
8 ms |
5112 KB |
Output is correct |
14 |
Correct |
8 ms |
5112 KB |
Output is correct |
15 |
Correct |
8 ms |
5112 KB |
Output is correct |
16 |
Correct |
8 ms |
5112 KB |
Output is correct |
17 |
Correct |
8 ms |
5112 KB |
Output is correct |
18 |
Correct |
8 ms |
5112 KB |
Output is correct |
19 |
Correct |
8 ms |
5112 KB |
Output is correct |
20 |
Correct |
8 ms |
5112 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
8 ms |
5116 KB |
Output is correct |
23 |
Correct |
8 ms |
5116 KB |
Output is correct |
24 |
Correct |
8 ms |
5112 KB |
Output is correct |
25 |
Correct |
8 ms |
5112 KB |
Output is correct |
26 |
Correct |
8 ms |
5116 KB |
Output is correct |
27 |
Correct |
8 ms |
5112 KB |
Output is correct |
28 |
Correct |
7 ms |
5112 KB |
Output is correct |
29 |
Correct |
8 ms |
5112 KB |
Output is correct |
30 |
Correct |
8 ms |
5112 KB |
Output is correct |
31 |
Correct |
7 ms |
5116 KB |
Output is correct |
32 |
Correct |
8 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
783 ms |
68080 KB |
Output is correct |
3 |
Correct |
839 ms |
67952 KB |
Output is correct |
4 |
Correct |
754 ms |
68336 KB |
Output is correct |
5 |
Correct |
810 ms |
68464 KB |
Output is correct |
6 |
Correct |
751 ms |
67952 KB |
Output is correct |
7 |
Correct |
799 ms |
68336 KB |
Output is correct |
8 |
Correct |
786 ms |
68864 KB |
Output is correct |
9 |
Correct |
812 ms |
68592 KB |
Output is correct |
10 |
Correct |
788 ms |
68076 KB |
Output is correct |
11 |
Correct |
821 ms |
68592 KB |
Output is correct |
12 |
Correct |
802 ms |
68080 KB |
Output is correct |
13 |
Correct |
747 ms |
68464 KB |
Output is correct |
14 |
Correct |
775 ms |
68464 KB |
Output is correct |
15 |
Correct |
759 ms |
68464 KB |
Output is correct |
16 |
Correct |
758 ms |
68212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
830 ms |
67956 KB |
Output is correct |
3 |
Correct |
783 ms |
67660 KB |
Output is correct |
4 |
Correct |
817 ms |
68296 KB |
Output is correct |
5 |
Correct |
782 ms |
67572 KB |
Output is correct |
6 |
Correct |
794 ms |
67700 KB |
Output is correct |
7 |
Correct |
805 ms |
67572 KB |
Output is correct |
8 |
Correct |
809 ms |
67828 KB |
Output is correct |
9 |
Correct |
786 ms |
67700 KB |
Output is correct |
10 |
Correct |
782 ms |
67572 KB |
Output is correct |
11 |
Correct |
812 ms |
68340 KB |
Output is correct |
12 |
Correct |
804 ms |
67956 KB |
Output is correct |
13 |
Correct |
827 ms |
67956 KB |
Output is correct |
14 |
Correct |
837 ms |
67700 KB |
Output is correct |
15 |
Correct |
850 ms |
67828 KB |
Output is correct |
16 |
Correct |
801 ms |
67680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
677 ms |
65232 KB |
Output is correct |
2 |
Correct |
675 ms |
65228 KB |
Output is correct |
3 |
Correct |
691 ms |
65476 KB |
Output is correct |
4 |
Correct |
697 ms |
65496 KB |
Output is correct |
5 |
Correct |
696 ms |
65352 KB |
Output is correct |
6 |
Correct |
671 ms |
65104 KB |
Output is correct |
7 |
Correct |
686 ms |
65220 KB |
Output is correct |
8 |
Correct |
675 ms |
65352 KB |
Output is correct |
9 |
Correct |
693 ms |
65328 KB |
Output is correct |
10 |
Correct |
688 ms |
65224 KB |
Output is correct |
11 |
Correct |
700 ms |
65488 KB |
Output is correct |
12 |
Correct |
695 ms |
65484 KB |
Output is correct |
13 |
Correct |
700 ms |
65356 KB |
Output is correct |
14 |
Correct |
685 ms |
65488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
8 ms |
5116 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
9 ms |
5112 KB |
Output is correct |
13 |
Correct |
8 ms |
5112 KB |
Output is correct |
14 |
Correct |
8 ms |
5112 KB |
Output is correct |
15 |
Correct |
8 ms |
5112 KB |
Output is correct |
16 |
Correct |
8 ms |
5112 KB |
Output is correct |
17 |
Correct |
8 ms |
5112 KB |
Output is correct |
18 |
Correct |
8 ms |
5112 KB |
Output is correct |
19 |
Correct |
8 ms |
5112 KB |
Output is correct |
20 |
Correct |
8 ms |
5112 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
8 ms |
5116 KB |
Output is correct |
23 |
Correct |
8 ms |
5116 KB |
Output is correct |
24 |
Correct |
8 ms |
5112 KB |
Output is correct |
25 |
Correct |
8 ms |
5112 KB |
Output is correct |
26 |
Correct |
8 ms |
5116 KB |
Output is correct |
27 |
Correct |
8 ms |
5112 KB |
Output is correct |
28 |
Correct |
7 ms |
5112 KB |
Output is correct |
29 |
Correct |
8 ms |
5112 KB |
Output is correct |
30 |
Correct |
8 ms |
5112 KB |
Output is correct |
31 |
Correct |
7 ms |
5116 KB |
Output is correct |
32 |
Correct |
8 ms |
5112 KB |
Output is correct |
33 |
Correct |
867 ms |
68724 KB |
Output is correct |
34 |
Correct |
883 ms |
68980 KB |
Output is correct |
35 |
Correct |
861 ms |
68724 KB |
Output is correct |
36 |
Correct |
873 ms |
68916 KB |
Output is correct |
37 |
Correct |
855 ms |
68600 KB |
Output is correct |
38 |
Correct |
857 ms |
68724 KB |
Output is correct |
39 |
Correct |
872 ms |
68468 KB |
Output is correct |
40 |
Correct |
874 ms |
68724 KB |
Output is correct |
41 |
Correct |
875 ms |
68852 KB |
Output is correct |
42 |
Correct |
869 ms |
68852 KB |
Output is correct |
43 |
Correct |
894 ms |
68468 KB |
Output is correct |
44 |
Correct |
920 ms |
68596 KB |
Output is correct |
45 |
Correct |
887 ms |
68212 KB |
Output is correct |
46 |
Correct |
899 ms |
68468 KB |
Output is correct |
47 |
Correct |
885 ms |
67932 KB |
Output is correct |
48 |
Correct |
878 ms |
67956 KB |
Output is correct |
49 |
Correct |
880 ms |
68088 KB |
Output is correct |
50 |
Correct |
906 ms |
68724 KB |
Output is correct |
51 |
Correct |
906 ms |
68724 KB |
Output is correct |
52 |
Correct |
893 ms |
68204 KB |
Output is correct |
53 |
Correct |
893 ms |
68212 KB |
Output is correct |
54 |
Correct |
782 ms |
68084 KB |
Output is correct |
55 |
Correct |
782 ms |
68084 KB |
Output is correct |
56 |
Correct |
770 ms |
68212 KB |
Output is correct |
57 |
Correct |
761 ms |
68084 KB |
Output is correct |
58 |
Correct |
811 ms |
68724 KB |
Output is correct |
59 |
Correct |
783 ms |
68080 KB |
Output is correct |
60 |
Correct |
839 ms |
67952 KB |
Output is correct |
61 |
Correct |
754 ms |
68336 KB |
Output is correct |
62 |
Correct |
810 ms |
68464 KB |
Output is correct |
63 |
Correct |
751 ms |
67952 KB |
Output is correct |
64 |
Correct |
799 ms |
68336 KB |
Output is correct |
65 |
Correct |
786 ms |
68864 KB |
Output is correct |
66 |
Correct |
812 ms |
68592 KB |
Output is correct |
67 |
Correct |
788 ms |
68076 KB |
Output is correct |
68 |
Correct |
821 ms |
68592 KB |
Output is correct |
69 |
Correct |
802 ms |
68080 KB |
Output is correct |
70 |
Correct |
747 ms |
68464 KB |
Output is correct |
71 |
Correct |
775 ms |
68464 KB |
Output is correct |
72 |
Correct |
759 ms |
68464 KB |
Output is correct |
73 |
Correct |
758 ms |
68212 KB |
Output is correct |
74 |
Correct |
830 ms |
67956 KB |
Output is correct |
75 |
Correct |
783 ms |
67660 KB |
Output is correct |
76 |
Correct |
817 ms |
68296 KB |
Output is correct |
77 |
Correct |
782 ms |
67572 KB |
Output is correct |
78 |
Correct |
794 ms |
67700 KB |
Output is correct |
79 |
Correct |
805 ms |
67572 KB |
Output is correct |
80 |
Correct |
809 ms |
67828 KB |
Output is correct |
81 |
Correct |
786 ms |
67700 KB |
Output is correct |
82 |
Correct |
782 ms |
67572 KB |
Output is correct |
83 |
Correct |
812 ms |
68340 KB |
Output is correct |
84 |
Correct |
804 ms |
67956 KB |
Output is correct |
85 |
Correct |
827 ms |
67956 KB |
Output is correct |
86 |
Correct |
837 ms |
67700 KB |
Output is correct |
87 |
Correct |
850 ms |
67828 KB |
Output is correct |
88 |
Correct |
801 ms |
67680 KB |
Output is correct |
89 |
Correct |
677 ms |
65232 KB |
Output is correct |
90 |
Correct |
675 ms |
65228 KB |
Output is correct |
91 |
Correct |
691 ms |
65476 KB |
Output is correct |
92 |
Correct |
697 ms |
65496 KB |
Output is correct |
93 |
Correct |
696 ms |
65352 KB |
Output is correct |
94 |
Correct |
671 ms |
65104 KB |
Output is correct |
95 |
Correct |
686 ms |
65220 KB |
Output is correct |
96 |
Correct |
675 ms |
65352 KB |
Output is correct |
97 |
Correct |
693 ms |
65328 KB |
Output is correct |
98 |
Correct |
688 ms |
65224 KB |
Output is correct |
99 |
Correct |
700 ms |
65488 KB |
Output is correct |
100 |
Correct |
695 ms |
65484 KB |
Output is correct |
101 |
Correct |
700 ms |
65356 KB |
Output is correct |
102 |
Correct |
685 ms |
65488 KB |
Output is correct |