#include <bits/stdc++.h>
using namespace std;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n'
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;
#define int long long
const int MAXN = 1e5+5;
int n,q;
int a[MAXN];
int calc(int l, int r) {
stack<pair<int,PII>> st;
vector<PII> v;
FOR(i,l,r+1) {
int cur = a[i], cursta = i;
while (!st.empty() && cur >= a[st.top().S.S]) {
int val = st.top().F, sta = st.top().S.F, en = st.top().S.S;
// cout << i << ": " << val << " " << sta << " " << en << ln;
st.pop();
cur += val;
cursta = sta;
if ((sta == l || a[sta-1] > cur-a[i]) && a[i] > cur-a[i]) v.PB({sta,i-1});
}
st.push({cur,{cursta,i}});
}
int cur = LLINF;
while (!st.empty() && cur >= a[st.top().S.S]) {
int val = st.top().F, sta = st.top().S.F, en = st.top().S.S;
// cout << val << " " << sta << " " << en << ln;
st.pop();
cur += val;
if ((sta == l || a[sta-1] > cur-LLINF)) v.PB({sta,r});
}
// FORX(u,v) cout << u.F << " " << u.S << ln;
// return 0;
stack<PII> st1;
int j = v.size()-1;
int cnt = 0;
FORR(i,n-1,-1) {
while (!st1.empty() && st1.top().F == i) st1.pop();
while (j >= 0 && v[j].S == i) st1.push({v[j].F-1,j}), j--;
if (!st1.empty() && v[st1.top().S] == make_pair(l,r)) cnt++;
}
return cnt;
}
signed main() {
OPTM;
cin >> n;
FOR(i,0,n) cin >> a[i];
a[n] = LLINF;
cin >> q;
FOR(_,0,q) {
int t; cin >> t;
if (t == 1) {
int x,y; cin >> x >> y;
a[x-1] = y;
}
else {
int l,r; cin >> l >> r;
l--; r--;
cout << calc(l,r) << ln;
}
}
}
Compilation message
fish2.cpp: In function 'long long int calc(long long int, long long int)':
fish2.cpp:35:55: warning: unused variable 'en' [-Wunused-variable]
35 | int val = st.top().F, sta = st.top().S.F, en = st.top().S.S;
| ^~
fish2.cpp:46:51: warning: unused variable 'en' [-Wunused-variable]
46 | int val = st.top().F, sta = st.top().S.F, en = st.top().S.S;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
11 ms |
3780 KB |
Output is correct |
3 |
Correct |
8 ms |
3536 KB |
Output is correct |
4 |
Correct |
12 ms |
3792 KB |
Output is correct |
5 |
Correct |
8 ms |
3608 KB |
Output is correct |
6 |
Correct |
11 ms |
3224 KB |
Output is correct |
7 |
Correct |
7 ms |
1960 KB |
Output is correct |
8 |
Correct |
10 ms |
3172 KB |
Output is correct |
9 |
Correct |
6 ms |
1880 KB |
Output is correct |
10 |
Correct |
9 ms |
2772 KB |
Output is correct |
11 |
Correct |
7 ms |
2512 KB |
Output is correct |
12 |
Correct |
7 ms |
2516 KB |
Output is correct |
13 |
Correct |
7 ms |
2528 KB |
Output is correct |
14 |
Correct |
9 ms |
3904 KB |
Output is correct |
15 |
Correct |
10 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
11 ms |
3780 KB |
Output is correct |
23 |
Correct |
8 ms |
3536 KB |
Output is correct |
24 |
Correct |
12 ms |
3792 KB |
Output is correct |
25 |
Correct |
8 ms |
3608 KB |
Output is correct |
26 |
Correct |
11 ms |
3224 KB |
Output is correct |
27 |
Correct |
7 ms |
1960 KB |
Output is correct |
28 |
Correct |
10 ms |
3172 KB |
Output is correct |
29 |
Correct |
6 ms |
1880 KB |
Output is correct |
30 |
Correct |
9 ms |
2772 KB |
Output is correct |
31 |
Correct |
7 ms |
2512 KB |
Output is correct |
32 |
Correct |
7 ms |
2516 KB |
Output is correct |
33 |
Correct |
7 ms |
2528 KB |
Output is correct |
34 |
Correct |
9 ms |
3904 KB |
Output is correct |
35 |
Correct |
10 ms |
3796 KB |
Output is correct |
36 |
Correct |
377 ms |
5328 KB |
Output is correct |
37 |
Correct |
460 ms |
4620 KB |
Output is correct |
38 |
Correct |
617 ms |
3512 KB |
Output is correct |
39 |
Correct |
137 ms |
4960 KB |
Output is correct |
40 |
Correct |
630 ms |
3588 KB |
Output is correct |
41 |
Correct |
526 ms |
3696 KB |
Output is correct |
42 |
Correct |
651 ms |
3792 KB |
Output is correct |
43 |
Correct |
538 ms |
2260 KB |
Output is correct |
44 |
Correct |
686 ms |
2224 KB |
Output is correct |
45 |
Correct |
256 ms |
3376 KB |
Output is correct |
46 |
Correct |
366 ms |
3312 KB |
Output is correct |
47 |
Correct |
436 ms |
2188 KB |
Output is correct |
48 |
Correct |
198 ms |
2912 KB |
Output is correct |
49 |
Correct |
627 ms |
3104 KB |
Output is correct |
50 |
Correct |
370 ms |
4720 KB |
Output is correct |
51 |
Correct |
1607 ms |
4076 KB |
Output is correct |
52 |
Correct |
1368 ms |
4876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
11 ms |
3780 KB |
Output is correct |
3 |
Correct |
8 ms |
3536 KB |
Output is correct |
4 |
Correct |
12 ms |
3792 KB |
Output is correct |
5 |
Correct |
8 ms |
3608 KB |
Output is correct |
6 |
Correct |
11 ms |
3224 KB |
Output is correct |
7 |
Correct |
7 ms |
1960 KB |
Output is correct |
8 |
Correct |
10 ms |
3172 KB |
Output is correct |
9 |
Correct |
6 ms |
1880 KB |
Output is correct |
10 |
Correct |
9 ms |
2772 KB |
Output is correct |
11 |
Correct |
7 ms |
2512 KB |
Output is correct |
12 |
Correct |
7 ms |
2516 KB |
Output is correct |
13 |
Correct |
7 ms |
2528 KB |
Output is correct |
14 |
Correct |
9 ms |
3904 KB |
Output is correct |
15 |
Correct |
10 ms |
3796 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Execution timed out |
4043 ms |
4988 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
11 ms |
3780 KB |
Output is correct |
3 |
Correct |
8 ms |
3536 KB |
Output is correct |
4 |
Correct |
12 ms |
3792 KB |
Output is correct |
5 |
Correct |
8 ms |
3608 KB |
Output is correct |
6 |
Correct |
11 ms |
3224 KB |
Output is correct |
7 |
Correct |
7 ms |
1960 KB |
Output is correct |
8 |
Correct |
10 ms |
3172 KB |
Output is correct |
9 |
Correct |
6 ms |
1880 KB |
Output is correct |
10 |
Correct |
9 ms |
2772 KB |
Output is correct |
11 |
Correct |
7 ms |
2512 KB |
Output is correct |
12 |
Correct |
7 ms |
2516 KB |
Output is correct |
13 |
Correct |
7 ms |
2528 KB |
Output is correct |
14 |
Correct |
9 ms |
3904 KB |
Output is correct |
15 |
Correct |
10 ms |
3796 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Execution timed out |
4058 ms |
5404 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
11 ms |
3780 KB |
Output is correct |
23 |
Correct |
8 ms |
3536 KB |
Output is correct |
24 |
Correct |
12 ms |
3792 KB |
Output is correct |
25 |
Correct |
8 ms |
3608 KB |
Output is correct |
26 |
Correct |
11 ms |
3224 KB |
Output is correct |
27 |
Correct |
7 ms |
1960 KB |
Output is correct |
28 |
Correct |
10 ms |
3172 KB |
Output is correct |
29 |
Correct |
6 ms |
1880 KB |
Output is correct |
30 |
Correct |
9 ms |
2772 KB |
Output is correct |
31 |
Correct |
7 ms |
2512 KB |
Output is correct |
32 |
Correct |
7 ms |
2516 KB |
Output is correct |
33 |
Correct |
7 ms |
2528 KB |
Output is correct |
34 |
Correct |
9 ms |
3904 KB |
Output is correct |
35 |
Correct |
10 ms |
3796 KB |
Output is correct |
36 |
Correct |
377 ms |
5328 KB |
Output is correct |
37 |
Correct |
460 ms |
4620 KB |
Output is correct |
38 |
Correct |
617 ms |
3512 KB |
Output is correct |
39 |
Correct |
137 ms |
4960 KB |
Output is correct |
40 |
Correct |
630 ms |
3588 KB |
Output is correct |
41 |
Correct |
526 ms |
3696 KB |
Output is correct |
42 |
Correct |
651 ms |
3792 KB |
Output is correct |
43 |
Correct |
538 ms |
2260 KB |
Output is correct |
44 |
Correct |
686 ms |
2224 KB |
Output is correct |
45 |
Correct |
256 ms |
3376 KB |
Output is correct |
46 |
Correct |
366 ms |
3312 KB |
Output is correct |
47 |
Correct |
436 ms |
2188 KB |
Output is correct |
48 |
Correct |
198 ms |
2912 KB |
Output is correct |
49 |
Correct |
627 ms |
3104 KB |
Output is correct |
50 |
Correct |
370 ms |
4720 KB |
Output is correct |
51 |
Correct |
1607 ms |
4076 KB |
Output is correct |
52 |
Correct |
1368 ms |
4876 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Execution timed out |
4043 ms |
4988 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |