#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, 4 * n, 0, vec2[p].gap + n, vec2[p].X);
else
segt2.update(1, 0, 4 * n, vec2[p].gap, 4 * n, vec2[p].X);
p++;
}
ans[Q[i].num] = segt1.query(1, 0, 4 * n, Q[i].T - Q[i].R + n, Q[i].T - Q[i].L + n);
ans[Q[i].num] += segt2.query(1, 0, 4 * 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 |
388 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 |
596 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 |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
696 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 |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 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 |
1 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 |
720 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 |
388 KB |
Output is correct |
2 |
Correct |
884 ms |
145424 KB |
Output is correct |
3 |
Correct |
862 ms |
151040 KB |
Output is correct |
4 |
Correct |
885 ms |
151088 KB |
Output is correct |
5 |
Correct |
886 ms |
151472 KB |
Output is correct |
6 |
Correct |
928 ms |
150896 KB |
Output is correct |
7 |
Correct |
863 ms |
151368 KB |
Output is correct |
8 |
Correct |
937 ms |
151252 KB |
Output is correct |
9 |
Correct |
919 ms |
151272 KB |
Output is correct |
10 |
Correct |
868 ms |
151088 KB |
Output is correct |
11 |
Correct |
897 ms |
151568 KB |
Output is correct |
12 |
Correct |
861 ms |
150960 KB |
Output is correct |
13 |
Correct |
887 ms |
151028 KB |
Output is correct |
14 |
Correct |
897 ms |
151012 KB |
Output is correct |
15 |
Correct |
870 ms |
151088 KB |
Output is correct |
16 |
Correct |
932 ms |
150972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
388 KB |
Output is correct |
2 |
Correct |
837 ms |
145744 KB |
Output is correct |
3 |
Correct |
821 ms |
145456 KB |
Output is correct |
4 |
Correct |
852 ms |
145840 KB |
Output is correct |
5 |
Correct |
826 ms |
150828 KB |
Output is correct |
6 |
Correct |
855 ms |
151224 KB |
Output is correct |
7 |
Correct |
858 ms |
151084 KB |
Output is correct |
8 |
Correct |
897 ms |
151180 KB |
Output is correct |
9 |
Correct |
874 ms |
150932 KB |
Output is correct |
10 |
Correct |
829 ms |
150644 KB |
Output is correct |
11 |
Correct |
877 ms |
151380 KB |
Output is correct |
12 |
Correct |
867 ms |
151008 KB |
Output is correct |
13 |
Correct |
848 ms |
151356 KB |
Output is correct |
14 |
Correct |
855 ms |
150904 KB |
Output is correct |
15 |
Correct |
878 ms |
151264 KB |
Output is correct |
16 |
Correct |
873 ms |
150968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
821 ms |
145644 KB |
Output is correct |
2 |
Correct |
865 ms |
145560 KB |
Output is correct |
3 |
Correct |
897 ms |
145884 KB |
Output is correct |
4 |
Correct |
819 ms |
145796 KB |
Output is correct |
5 |
Correct |
858 ms |
145700 KB |
Output is correct |
6 |
Correct |
900 ms |
145628 KB |
Output is correct |
7 |
Correct |
862 ms |
145772 KB |
Output is correct |
8 |
Correct |
869 ms |
145788 KB |
Output is correct |
9 |
Correct |
846 ms |
145716 KB |
Output is correct |
10 |
Correct |
868 ms |
145680 KB |
Output is correct |
11 |
Correct |
875 ms |
145848 KB |
Output is correct |
12 |
Correct |
864 ms |
145816 KB |
Output is correct |
13 |
Correct |
876 ms |
145688 KB |
Output is correct |
14 |
Correct |
895 ms |
149736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
388 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 |
596 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 |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
696 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 |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 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 |
1 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 |
720 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 |
884 ms |
145424 KB |
Output is correct |
34 |
Correct |
862 ms |
151040 KB |
Output is correct |
35 |
Correct |
885 ms |
151088 KB |
Output is correct |
36 |
Correct |
886 ms |
151472 KB |
Output is correct |
37 |
Correct |
928 ms |
150896 KB |
Output is correct |
38 |
Correct |
863 ms |
151368 KB |
Output is correct |
39 |
Correct |
937 ms |
151252 KB |
Output is correct |
40 |
Correct |
919 ms |
151272 KB |
Output is correct |
41 |
Correct |
868 ms |
151088 KB |
Output is correct |
42 |
Correct |
897 ms |
151568 KB |
Output is correct |
43 |
Correct |
861 ms |
150960 KB |
Output is correct |
44 |
Correct |
887 ms |
151028 KB |
Output is correct |
45 |
Correct |
897 ms |
151012 KB |
Output is correct |
46 |
Correct |
870 ms |
151088 KB |
Output is correct |
47 |
Correct |
932 ms |
150972 KB |
Output is correct |
48 |
Correct |
837 ms |
145744 KB |
Output is correct |
49 |
Correct |
821 ms |
145456 KB |
Output is correct |
50 |
Correct |
852 ms |
145840 KB |
Output is correct |
51 |
Correct |
826 ms |
150828 KB |
Output is correct |
52 |
Correct |
855 ms |
151224 KB |
Output is correct |
53 |
Correct |
858 ms |
151084 KB |
Output is correct |
54 |
Correct |
897 ms |
151180 KB |
Output is correct |
55 |
Correct |
874 ms |
150932 KB |
Output is correct |
56 |
Correct |
829 ms |
150644 KB |
Output is correct |
57 |
Correct |
877 ms |
151380 KB |
Output is correct |
58 |
Correct |
867 ms |
151008 KB |
Output is correct |
59 |
Correct |
848 ms |
151356 KB |
Output is correct |
60 |
Correct |
855 ms |
150904 KB |
Output is correct |
61 |
Correct |
878 ms |
151264 KB |
Output is correct |
62 |
Correct |
873 ms |
150968 KB |
Output is correct |
63 |
Correct |
821 ms |
145644 KB |
Output is correct |
64 |
Correct |
865 ms |
145560 KB |
Output is correct |
65 |
Correct |
897 ms |
145884 KB |
Output is correct |
66 |
Correct |
819 ms |
145796 KB |
Output is correct |
67 |
Correct |
858 ms |
145700 KB |
Output is correct |
68 |
Correct |
900 ms |
145628 KB |
Output is correct |
69 |
Correct |
862 ms |
145772 KB |
Output is correct |
70 |
Correct |
869 ms |
145788 KB |
Output is correct |
71 |
Correct |
846 ms |
145716 KB |
Output is correct |
72 |
Correct |
868 ms |
145680 KB |
Output is correct |
73 |
Correct |
875 ms |
145848 KB |
Output is correct |
74 |
Correct |
864 ms |
145816 KB |
Output is correct |
75 |
Correct |
876 ms |
145688 KB |
Output is correct |
76 |
Correct |
895 ms |
149736 KB |
Output is correct |
77 |
Correct |
924 ms |
151216 KB |
Output is correct |
78 |
Correct |
999 ms |
151396 KB |
Output is correct |
79 |
Correct |
963 ms |
151216 KB |
Output is correct |
80 |
Correct |
894 ms |
151084 KB |
Output is correct |
81 |
Correct |
942 ms |
150984 KB |
Output is correct |
82 |
Correct |
924 ms |
151148 KB |
Output is correct |
83 |
Correct |
898 ms |
151024 KB |
Output is correct |
84 |
Execution timed out |
1010 ms |
151072 KB |
Time limit exceeded |
85 |
Halted |
0 ms |
0 KB |
- |