#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 |
5112 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 |
7 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 |
7 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 |
5112 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 |
5112 KB |
Output is correct |
27 |
Correct |
7 ms |
5112 KB |
Output is correct |
28 |
Correct |
8 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 |
8 ms |
5112 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 |
817 ms |
68056 KB |
Output is correct |
3 |
Correct |
853 ms |
69364 KB |
Output is correct |
4 |
Correct |
780 ms |
68208 KB |
Output is correct |
5 |
Correct |
777 ms |
68976 KB |
Output is correct |
6 |
Correct |
739 ms |
69232 KB |
Output is correct |
7 |
Correct |
798 ms |
70512 KB |
Output is correct |
8 |
Correct |
765 ms |
73072 KB |
Output is correct |
9 |
Correct |
846 ms |
74096 KB |
Output is correct |
10 |
Correct |
807 ms |
72304 KB |
Output is correct |
11 |
Correct |
825 ms |
71028 KB |
Output is correct |
12 |
Correct |
805 ms |
69232 KB |
Output is correct |
13 |
Correct |
753 ms |
68592 KB |
Output is correct |
14 |
Correct |
795 ms |
68464 KB |
Output is correct |
15 |
Correct |
770 ms |
68464 KB |
Output is correct |
16 |
Correct |
747 ms |
68208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
838 ms |
67904 KB |
Output is correct |
3 |
Correct |
797 ms |
67572 KB |
Output is correct |
4 |
Correct |
827 ms |
68344 KB |
Output is correct |
5 |
Correct |
796 ms |
67572 KB |
Output is correct |
6 |
Correct |
814 ms |
67700 KB |
Output is correct |
7 |
Correct |
802 ms |
67676 KB |
Output is correct |
8 |
Correct |
814 ms |
67828 KB |
Output is correct |
9 |
Correct |
805 ms |
67700 KB |
Output is correct |
10 |
Correct |
773 ms |
67572 KB |
Output is correct |
11 |
Correct |
821 ms |
68212 KB |
Output is correct |
12 |
Correct |
802 ms |
67956 KB |
Output is correct |
13 |
Correct |
841 ms |
67956 KB |
Output is correct |
14 |
Correct |
818 ms |
67808 KB |
Output is correct |
15 |
Correct |
817 ms |
67828 KB |
Output is correct |
16 |
Correct |
820 ms |
67572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
711 ms |
65216 KB |
Output is correct |
2 |
Correct |
688 ms |
65100 KB |
Output is correct |
3 |
Correct |
710 ms |
65476 KB |
Output is correct |
4 |
Correct |
686 ms |
65496 KB |
Output is correct |
5 |
Correct |
706 ms |
65364 KB |
Output is correct |
6 |
Correct |
669 ms |
65100 KB |
Output is correct |
7 |
Correct |
689 ms |
65220 KB |
Output is correct |
8 |
Correct |
690 ms |
65352 KB |
Output is correct |
9 |
Correct |
695 ms |
65364 KB |
Output is correct |
10 |
Correct |
706 ms |
65224 KB |
Output is correct |
11 |
Correct |
729 ms |
65456 KB |
Output is correct |
12 |
Correct |
735 ms |
65484 KB |
Output is correct |
13 |
Correct |
700 ms |
65356 KB |
Output is correct |
14 |
Correct |
701 ms |
65396 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 |
5112 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 |
7 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 |
7 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 |
5112 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 |
5112 KB |
Output is correct |
27 |
Correct |
7 ms |
5112 KB |
Output is correct |
28 |
Correct |
8 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 |
8 ms |
5112 KB |
Output is correct |
32 |
Correct |
8 ms |
5112 KB |
Output is correct |
33 |
Correct |
899 ms |
68664 KB |
Output is correct |
34 |
Correct |
894 ms |
68980 KB |
Output is correct |
35 |
Correct |
880 ms |
68716 KB |
Output is correct |
36 |
Correct |
881 ms |
68692 KB |
Output is correct |
37 |
Correct |
872 ms |
68596 KB |
Output is correct |
38 |
Correct |
869 ms |
68724 KB |
Output is correct |
39 |
Correct |
865 ms |
68468 KB |
Output is correct |
40 |
Correct |
889 ms |
68724 KB |
Output is correct |
41 |
Correct |
856 ms |
68852 KB |
Output is correct |
42 |
Correct |
860 ms |
68852 KB |
Output is correct |
43 |
Correct |
906 ms |
68468 KB |
Output is correct |
44 |
Correct |
919 ms |
68596 KB |
Output is correct |
45 |
Correct |
886 ms |
68152 KB |
Output is correct |
46 |
Correct |
883 ms |
68468 KB |
Output is correct |
47 |
Correct |
877 ms |
67968 KB |
Output is correct |
48 |
Correct |
880 ms |
67956 KB |
Output is correct |
49 |
Correct |
888 ms |
68084 KB |
Output is correct |
50 |
Correct |
917 ms |
68724 KB |
Output is correct |
51 |
Correct |
903 ms |
68852 KB |
Output is correct |
52 |
Correct |
891 ms |
68340 KB |
Output is correct |
53 |
Correct |
896 ms |
68340 KB |
Output is correct |
54 |
Correct |
779 ms |
68040 KB |
Output is correct |
55 |
Correct |
773 ms |
67960 KB |
Output is correct |
56 |
Correct |
787 ms |
68212 KB |
Output is correct |
57 |
Correct |
797 ms |
68084 KB |
Output is correct |
58 |
Correct |
787 ms |
68724 KB |
Output is correct |
59 |
Correct |
817 ms |
68056 KB |
Output is correct |
60 |
Correct |
853 ms |
69364 KB |
Output is correct |
61 |
Correct |
780 ms |
68208 KB |
Output is correct |
62 |
Correct |
777 ms |
68976 KB |
Output is correct |
63 |
Correct |
739 ms |
69232 KB |
Output is correct |
64 |
Correct |
798 ms |
70512 KB |
Output is correct |
65 |
Correct |
765 ms |
73072 KB |
Output is correct |
66 |
Correct |
846 ms |
74096 KB |
Output is correct |
67 |
Correct |
807 ms |
72304 KB |
Output is correct |
68 |
Correct |
825 ms |
71028 KB |
Output is correct |
69 |
Correct |
805 ms |
69232 KB |
Output is correct |
70 |
Correct |
753 ms |
68592 KB |
Output is correct |
71 |
Correct |
795 ms |
68464 KB |
Output is correct |
72 |
Correct |
770 ms |
68464 KB |
Output is correct |
73 |
Correct |
747 ms |
68208 KB |
Output is correct |
74 |
Correct |
838 ms |
67904 KB |
Output is correct |
75 |
Correct |
797 ms |
67572 KB |
Output is correct |
76 |
Correct |
827 ms |
68344 KB |
Output is correct |
77 |
Correct |
796 ms |
67572 KB |
Output is correct |
78 |
Correct |
814 ms |
67700 KB |
Output is correct |
79 |
Correct |
802 ms |
67676 KB |
Output is correct |
80 |
Correct |
814 ms |
67828 KB |
Output is correct |
81 |
Correct |
805 ms |
67700 KB |
Output is correct |
82 |
Correct |
773 ms |
67572 KB |
Output is correct |
83 |
Correct |
821 ms |
68212 KB |
Output is correct |
84 |
Correct |
802 ms |
67956 KB |
Output is correct |
85 |
Correct |
841 ms |
67956 KB |
Output is correct |
86 |
Correct |
818 ms |
67808 KB |
Output is correct |
87 |
Correct |
817 ms |
67828 KB |
Output is correct |
88 |
Correct |
820 ms |
67572 KB |
Output is correct |
89 |
Correct |
711 ms |
65216 KB |
Output is correct |
90 |
Correct |
688 ms |
65100 KB |
Output is correct |
91 |
Correct |
710 ms |
65476 KB |
Output is correct |
92 |
Correct |
686 ms |
65496 KB |
Output is correct |
93 |
Correct |
706 ms |
65364 KB |
Output is correct |
94 |
Correct |
669 ms |
65100 KB |
Output is correct |
95 |
Correct |
689 ms |
65220 KB |
Output is correct |
96 |
Correct |
690 ms |
65352 KB |
Output is correct |
97 |
Correct |
695 ms |
65364 KB |
Output is correct |
98 |
Correct |
706 ms |
65224 KB |
Output is correct |
99 |
Correct |
729 ms |
65456 KB |
Output is correct |
100 |
Correct |
735 ms |
65484 KB |
Output is correct |
101 |
Correct |
700 ms |
65356 KB |
Output is correct |
102 |
Correct |
701 ms |
65396 KB |
Output is correct |