#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())
struct gujo
{
ll T, L, R, num;
bool operator < (const gujo &xx) const
{
return T < xx.T;
}
};
struct gujo2
{
ll T, gap, typ, X;
bool operator < (const gujo2 &xx) const
{
return T < xx.T;
}
};
ll n, q;
ll a[200010];
gujo Q[200010];
ll Lidx[200010], Ridx[200010];
ll h[200010], w[200010];
vector<gujo2> vec2;
ll ans[200010];
struct lazysegtree
{
ll seg[4000010];
ll lazy[4000010];
void prop(ll no, ll s, ll e)
{
if(lazy[no])
{
seg[no] += lazy[no] * (e - s + 1);
if(s != e)
{
lazy[no << 1] += lazy[no];
lazy[no << 1 | 1] += lazy[no];
}
lazy[no] = 0;
}
}
void update(ll no, ll s, ll e, ll l, ll r, ll v)
{
prop(no, s, e);
if(e < l || r < s)
return;
if(l <= s && e <= r)
{
seg[no] += v * (e - s + 1);
if(s != e)
{
lazy[no << 1] += v;
lazy[no << 1 | 1] += v;
}
return;
}
update(no << 1, s, (s + e) >> 1, l, r, v);
update(no << 1 | 1, ((s + e) >> 1) + 1, e, l, r, v);
seg[no] = seg[no << 1] + seg[no << 1 | 1];
}
ll query(ll no, ll s, ll e, ll l, ll r)
{
prop(no, s, e);
if(e < l || r < s)
return 0;
if(l <= s && e <= r)
return seg[no];
return query(no << 1, s, (s + e) >> 1, l, r) + query(no << 1 | 1, ((s + e) >> 1) + 1, e, l, r);
}
}segt1, segt2;
void LR(void)
{
stack<ll> stk;
for(ll i = 1 ; i <= n ; i++)
{
while(!stk.empty() && a[stk.top()] <= a[i])
stk.pop();
if(!stk.empty())
Lidx[i] = stk.top();
stk.push(i);
}
while(!stk.empty())
stk.pop();
for(ll i = n ; i >= 1 ; i--)
{
while(!stk.empty() && a[stk.top()] < a[i])
stk.pop();
if(!stk.empty())
Ridx[i] = stk.top();
stk.push(i);
}
}
void diag(ll X, ll S, ll E, ll gp)
{
vec2.push_back({S, X, 1, gp});
vec2.push_back({E + 1, X, -1, -gp});
}
void rect(ll X, ll S, ll E, ll gp)
{
vec2.push_back({S, X, 2, gp});
vec2.push_back({E + 1, X, -2, -gp});
}
void tri(ll idx, ll len, ll S, ll E, ll gp)
{
diag(S - idx, S, E, gp);
rect(idx + len, S, E, -gp);
}
int main(void)
{
fastio
cin >> n >> q;
for(ll i = 1 ; i <= n ; i++)
cin >> a[i];
for(ll i = 1 ; i <= q ; i++)
{
cin >> Q[i].T >> Q[i].L >> Q[i].R;
Q[i].num = i;
}
sort(Q + 1, Q + 1 + q);
LR();
for(ll i = 1 ; i <= n ; i++)
{
if(!Lidx[i])
h[i] = n + 1;
else
h[i] = i - Lidx[i];
if(!Ridx[i])
w[i] = n - i + 1;
else
w[i] = Ridx[i] - i;
}
for(ll i = 1 ; i <= n ; i++)
{
tri(i - h[i] + 1, h[i] + w[i] - 1, 0, h[i] + w[i] - 2, a[i]);
tri(i - h[i] + 1, h[i] - 1, 0, h[i] - 2, -a[i]);
tri(i + 1, w[i] - 1, 0, w[i] - 2, -a[i]);
}
sort(vec2.begin(), vec2.end());
ll p = 0;
for(ll i = 1 ; i <= q ; i++)
{
while(p < (ll)vec2.size() && vec2[p].T <= Q[i].T)
{
if(abs(vec2[p].typ) == 1)
segt1.update(1, 0, 3 * n, 0, vec2[p].gap + n, vec2[p].X);
else
segt2.update(1, 0, n, vec2[p].gap, n, vec2[p].X);
p++;
}
ans[Q[i].num] = segt1.query(1, 0, 3 * n, Q[i].T - Q[i].R + n, Q[i].T - Q[i].L + n);
ans[Q[i].num] += segt2.query(1, 0, n, Q[i].L, Q[i].R);
}
for(ll i = 1 ; i <= q ; i++)
cout << ans[i] en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
720 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
720 KB |
Output is correct |
16 |
Correct |
2 ms |
720 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
842 ms |
145664 KB |
Output is correct |
3 |
Correct |
861 ms |
145756 KB |
Output is correct |
4 |
Correct |
917 ms |
145928 KB |
Output is correct |
5 |
Correct |
863 ms |
145968 KB |
Output is correct |
6 |
Correct |
872 ms |
145776 KB |
Output is correct |
7 |
Correct |
904 ms |
146040 KB |
Output is correct |
8 |
Correct |
916 ms |
146032 KB |
Output is correct |
9 |
Correct |
865 ms |
146280 KB |
Output is correct |
10 |
Correct |
843 ms |
145672 KB |
Output is correct |
11 |
Correct |
862 ms |
146100 KB |
Output is correct |
12 |
Correct |
862 ms |
145704 KB |
Output is correct |
13 |
Correct |
853 ms |
145908 KB |
Output is correct |
14 |
Correct |
904 ms |
145800 KB |
Output is correct |
15 |
Correct |
870 ms |
145864 KB |
Output is correct |
16 |
Correct |
866 ms |
145680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
880 ms |
146024 KB |
Output is correct |
3 |
Correct |
814 ms |
145564 KB |
Output is correct |
4 |
Correct |
896 ms |
146048 KB |
Output is correct |
5 |
Correct |
904 ms |
145552 KB |
Output is correct |
6 |
Correct |
810 ms |
145708 KB |
Output is correct |
7 |
Correct |
828 ms |
145808 KB |
Output is correct |
8 |
Correct |
814 ms |
145628 KB |
Output is correct |
9 |
Correct |
803 ms |
145584 KB |
Output is correct |
10 |
Correct |
824 ms |
145356 KB |
Output is correct |
11 |
Correct |
839 ms |
145864 KB |
Output is correct |
12 |
Correct |
820 ms |
145584 KB |
Output is correct |
13 |
Correct |
871 ms |
145800 KB |
Output is correct |
14 |
Correct |
817 ms |
145516 KB |
Output is correct |
15 |
Correct |
845 ms |
145720 KB |
Output is correct |
16 |
Correct |
842 ms |
145564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
779 ms |
145304 KB |
Output is correct |
2 |
Correct |
880 ms |
145324 KB |
Output is correct |
3 |
Correct |
877 ms |
145672 KB |
Output is correct |
4 |
Correct |
833 ms |
145460 KB |
Output is correct |
5 |
Correct |
888 ms |
145476 KB |
Output is correct |
6 |
Correct |
901 ms |
145300 KB |
Output is correct |
7 |
Correct |
952 ms |
145564 KB |
Output is correct |
8 |
Correct |
860 ms |
145528 KB |
Output is correct |
9 |
Correct |
823 ms |
145440 KB |
Output is correct |
10 |
Correct |
837 ms |
145420 KB |
Output is correct |
11 |
Correct |
846 ms |
145564 KB |
Output is correct |
12 |
Correct |
864 ms |
145488 KB |
Output is correct |
13 |
Correct |
861 ms |
145440 KB |
Output is correct |
14 |
Correct |
875 ms |
145612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
720 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
720 KB |
Output is correct |
16 |
Correct |
2 ms |
720 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
842 ms |
145664 KB |
Output is correct |
34 |
Correct |
861 ms |
145756 KB |
Output is correct |
35 |
Correct |
917 ms |
145928 KB |
Output is correct |
36 |
Correct |
863 ms |
145968 KB |
Output is correct |
37 |
Correct |
872 ms |
145776 KB |
Output is correct |
38 |
Correct |
904 ms |
146040 KB |
Output is correct |
39 |
Correct |
916 ms |
146032 KB |
Output is correct |
40 |
Correct |
865 ms |
146280 KB |
Output is correct |
41 |
Correct |
843 ms |
145672 KB |
Output is correct |
42 |
Correct |
862 ms |
146100 KB |
Output is correct |
43 |
Correct |
862 ms |
145704 KB |
Output is correct |
44 |
Correct |
853 ms |
145908 KB |
Output is correct |
45 |
Correct |
904 ms |
145800 KB |
Output is correct |
46 |
Correct |
870 ms |
145864 KB |
Output is correct |
47 |
Correct |
866 ms |
145680 KB |
Output is correct |
48 |
Correct |
880 ms |
146024 KB |
Output is correct |
49 |
Correct |
814 ms |
145564 KB |
Output is correct |
50 |
Correct |
896 ms |
146048 KB |
Output is correct |
51 |
Correct |
904 ms |
145552 KB |
Output is correct |
52 |
Correct |
810 ms |
145708 KB |
Output is correct |
53 |
Correct |
828 ms |
145808 KB |
Output is correct |
54 |
Correct |
814 ms |
145628 KB |
Output is correct |
55 |
Correct |
803 ms |
145584 KB |
Output is correct |
56 |
Correct |
824 ms |
145356 KB |
Output is correct |
57 |
Correct |
839 ms |
145864 KB |
Output is correct |
58 |
Correct |
820 ms |
145584 KB |
Output is correct |
59 |
Correct |
871 ms |
145800 KB |
Output is correct |
60 |
Correct |
817 ms |
145516 KB |
Output is correct |
61 |
Correct |
845 ms |
145720 KB |
Output is correct |
62 |
Correct |
842 ms |
145564 KB |
Output is correct |
63 |
Correct |
779 ms |
145304 KB |
Output is correct |
64 |
Correct |
880 ms |
145324 KB |
Output is correct |
65 |
Correct |
877 ms |
145672 KB |
Output is correct |
66 |
Correct |
833 ms |
145460 KB |
Output is correct |
67 |
Correct |
888 ms |
145476 KB |
Output is correct |
68 |
Correct |
901 ms |
145300 KB |
Output is correct |
69 |
Correct |
952 ms |
145564 KB |
Output is correct |
70 |
Correct |
860 ms |
145528 KB |
Output is correct |
71 |
Correct |
823 ms |
145440 KB |
Output is correct |
72 |
Correct |
837 ms |
145420 KB |
Output is correct |
73 |
Correct |
846 ms |
145564 KB |
Output is correct |
74 |
Correct |
864 ms |
145488 KB |
Output is correct |
75 |
Correct |
861 ms |
145440 KB |
Output is correct |
76 |
Correct |
875 ms |
145612 KB |
Output is correct |
77 |
Correct |
907 ms |
145556 KB |
Output is correct |
78 |
Correct |
916 ms |
145856 KB |
Output is correct |
79 |
Correct |
937 ms |
145632 KB |
Output is correct |
80 |
Correct |
976 ms |
145604 KB |
Output is correct |
81 |
Correct |
906 ms |
145460 KB |
Output is correct |
82 |
Correct |
892 ms |
145584 KB |
Output is correct |
83 |
Correct |
873 ms |
145596 KB |
Output is correct |
84 |
Correct |
904 ms |
145456 KB |
Output is correct |
85 |
Correct |
940 ms |
151136 KB |
Output is correct |
86 |
Correct |
878 ms |
151156 KB |
Output is correct |
87 |
Correct |
871 ms |
151472 KB |
Output is correct |
88 |
Correct |
889 ms |
151344 KB |
Output is correct |
89 |
Correct |
870 ms |
151132 KB |
Output is correct |
90 |
Correct |
865 ms |
151320 KB |
Output is correct |
91 |
Correct |
861 ms |
150980 KB |
Output is correct |
92 |
Correct |
852 ms |
150884 KB |
Output is correct |
93 |
Correct |
844 ms |
151088 KB |
Output is correct |
94 |
Correct |
906 ms |
151552 KB |
Output is correct |
95 |
Correct |
877 ms |
151496 KB |
Output is correct |
96 |
Correct |
948 ms |
151272 KB |
Output is correct |
97 |
Correct |
882 ms |
151212 KB |
Output is correct |
98 |
Correct |
889 ms |
150656 KB |
Output is correct |
99 |
Correct |
916 ms |
150828 KB |
Output is correct |
100 |
Correct |
908 ms |
151092 KB |
Output is correct |
101 |
Correct |
902 ms |
150768 KB |
Output is correct |
102 |
Correct |
995 ms |
151216 KB |
Output is correct |