# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
572065 |
2022-06-03T13:15:16 Z |
gs14004 |
Fish 2 (JOI22_fish2) |
C++17 |
|
4000 ms |
30368 KB |
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef long long lint;
typedef pair<lint, lint> pi;
const int MAXN = 100005;
const int MAXT = 270000;
lint a[MAXN];
struct node{
int s, e, cost;
bool operator<(const node &nd)const{
return pi(s, -e) < pi(nd.s, -nd.e);
}
bool operator==(const node &nd)const{
return pi(s, -e) == pi(nd.s, -nd.e);
}
};
struct bit{
lint tree[MAXN];
void add(int x, lint v){
for(int i = x; i < MAXN; i += i & -i) tree[i] += v;
}
lint query(int x){
lint ret = 0;
for(int i = x; i; i -= i & -i) ret += tree[i];
return ret;
}
lint query(int s, int e){
if(s > e) return 0;
return query(e) - query(s - 1);
}
}bit, subs;
struct seg{
lint tree[MAXT];
void init(int s, int e, int p, lint *a){
if(s == e){
tree[p] = a[s];
return;
}
int m = (s+e)/2;
init(s, m, 2*p, a);
init(m+1, e, 2*p+1, a);
tree[p] = max(tree[2*p], tree[2*p+1]);
}
void upd(int pos, int s, int e, int p, lint v){
if(s == e){
tree[p] = v;
return;
}
int m = (s + e) / 2;
if(pos <= m) upd(pos, s, m, 2*p, v);
else upd(pos, m+1, e, 2*p+1, v);
tree[p] = max(tree[2*p], tree[2*p+1]);
}
int queryLeft(int s, int e, int ps, int pe, int p, lint v){
if(e < ps || pe < s) return 1e9;
if(tree[p] <= v) return 1e9;
if(ps == pe) return ps;
int pm = (ps + pe) / 2;
if(s <= ps && pe <= e){
if(tree[2*p] > v) return queryLeft(s, e, ps, pm, 2*p, v);
return queryLeft(s, e, pm+1, pe, 2*p+1, v);
}
int r = queryLeft(s, e, ps, pm, 2*p, v);
if(r < 1e7) return r;
return queryLeft(s, e, pm+1, pe, 2*p+1, v);
}
int queryRight(int s, int e, int ps, int pe, int p, lint v){
if(e < ps || pe < s) return -1e9;
if(tree[p] <= v) return -1e9;
if(ps == pe) return ps;
int pm = (ps + pe) / 2;
if(s <= ps && pe <= e){
if(tree[2*p+1] > v) return queryRight(s, e, pm+1, pe, 2*p+1, v);
return queryRight(s, e, ps, pm, 2*p, v);
}
int r = queryRight(s, e, pm+1, pe, 2*p+1, v);
if(r > 0) return r;
return queryRight(s, e, ps, pm, 2*p, v);
}
}seg;
int n;
set<node> st;
void getNodeSet(int x, vector<node> &v){
if(x < 2 || x >= n) return;
int l = x, r = x;
while(true){
lint curSum = bit.query(l, r);
if(curSum < min(a[l-1], a[r+1])){
v.push_back({l, r, -1});
if(l == 2 && r == n - 1) break;
if(a[l-1] < a[r+1]) l--;
else r++;
}
curSum = bit.query(l, r);
int leftClosest = seg.queryRight(1, l - 1, 1, n, 1, curSum);
int rightClosest = seg.queryLeft(r + 1, n, 1, n, 1, curSum);
l = leftClosest + 1;
r = rightClosest - 1;
}
}
void add(int x, int v){
vector<node> l;
getNodeSet(x-1, l);
getNodeSet(x, l);
getNodeSet(x+1, l);
sort(all(l));
reverse(all(l));
l.resize(unique(all(l)) - l.begin());
for(auto &x : l){
auto it = st.lower_bound({x.s, x.e, -1});
if(it != st.end() && it->s == x.s && it->e == x.e){
// cout << "erase interval " << x.s << " " << x.e << " " << it->cost << endl;
subs.add(it->s - 1, -it->cost);
st.erase(it);
}
else assert(0);
}
l.clear();
bit.add(x, v);
a[x] += v;
seg.upd(x, 1, n, 1, a[x]);
getNodeSet(x-1, l);
getNodeSet(x, l);
getNodeSet(x+1, l);
sort(all(l));
reverse(all(l));
l.resize(unique(all(l)) - l.begin());
for(auto &x : l){
auto it = st.lower_bound({x.s, x.e, -1});
if(it != st.end() && it->s == x.s && it->e == x.e){
assert(0);
}
int sum = subs.query(x.s - 1, x.e);
subs.add(x.s - 1, x.e - x.s + 1 - sum);
st.insert({x.s, x.e, x.e - x.s + 1 - sum});
// cout << "Add interval " << x.s << " " << x.e << " " << x.e - x.s + 1 - sum << endl;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
a[1] = a[n + 2] = 1e15;
for(int i = 2; i <= n + 1; i++){
cin >> a[i];
}
n += 2;
seg.init(1, n, 1, a);
for(int i = 1; i <= n; i++){
bit.add(i, a[i]);
}
vector<node> l;
for(int i = 2; i <= n - 1; i++){
getNodeSet(i, l);
}
sort(all(l));
reverse(all(l));
l.resize(unique(all(l)) - l.begin());
for(auto &x : l){
auto it = st.lower_bound({x.s, x.e, -1});
if(it != st.end() && it->s == x.s && it->e == x.e){
assert(0);
}
int sum = subs.query(x.s - 1, x.e);
subs.add(x.s - 1, x.e - x.s + 1 - sum);
st.insert({x.s, x.e, x.e - x.s + 1 - sum});
// cout << "Add interval " << x.s << " " << x.e << " " << x.e - x.s + 1 - sum << endl;
}
int q; cin >> q;
while(q--){
int t, x, y; cin >> t >> x >> y;
if(t == 1){
x++;
add(x, y - a[x]);
}
else{
x++; y++;
int sx = x, sy = y;
for(int i = x; i <= y; i++){
if(bit.query(x, i - 1) < a[i]) sx = i;
}
for(int i = y; i >= x; i--){
if(bit.query(i + 1, y) < a[i]) sy = i;
}
cout << sy - sx + 1 - subs.query(sx, sy - 1) << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
11 ms |
596 KB |
Output is correct |
6 |
Correct |
5 ms |
596 KB |
Output is correct |
7 |
Correct |
10 ms |
596 KB |
Output is correct |
8 |
Correct |
6 ms |
596 KB |
Output is correct |
9 |
Correct |
5 ms |
660 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
5 ms |
468 KB |
Output is correct |
16 |
Correct |
6 ms |
468 KB |
Output is correct |
17 |
Correct |
4 ms |
468 KB |
Output is correct |
18 |
Correct |
4 ms |
468 KB |
Output is correct |
19 |
Correct |
3 ms |
468 KB |
Output is correct |
20 |
Correct |
6 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
494 ms |
28616 KB |
Output is correct |
3 |
Correct |
620 ms |
29460 KB |
Output is correct |
4 |
Correct |
526 ms |
29360 KB |
Output is correct |
5 |
Correct |
620 ms |
29548 KB |
Output is correct |
6 |
Correct |
99 ms |
10168 KB |
Output is correct |
7 |
Correct |
276 ms |
16508 KB |
Output is correct |
8 |
Correct |
95 ms |
10296 KB |
Output is correct |
9 |
Correct |
305 ms |
16648 KB |
Output is correct |
10 |
Correct |
400 ms |
17712 KB |
Output is correct |
11 |
Correct |
410 ms |
29040 KB |
Output is correct |
12 |
Correct |
142 ms |
10592 KB |
Output is correct |
13 |
Correct |
124 ms |
10928 KB |
Output is correct |
14 |
Correct |
124 ms |
12048 KB |
Output is correct |
15 |
Correct |
137 ms |
12088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
11 ms |
596 KB |
Output is correct |
6 |
Correct |
5 ms |
596 KB |
Output is correct |
7 |
Correct |
10 ms |
596 KB |
Output is correct |
8 |
Correct |
6 ms |
596 KB |
Output is correct |
9 |
Correct |
5 ms |
660 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
5 ms |
468 KB |
Output is correct |
16 |
Correct |
6 ms |
468 KB |
Output is correct |
17 |
Correct |
4 ms |
468 KB |
Output is correct |
18 |
Correct |
4 ms |
468 KB |
Output is correct |
19 |
Correct |
3 ms |
468 KB |
Output is correct |
20 |
Correct |
6 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
494 ms |
28616 KB |
Output is correct |
23 |
Correct |
620 ms |
29460 KB |
Output is correct |
24 |
Correct |
526 ms |
29360 KB |
Output is correct |
25 |
Correct |
620 ms |
29548 KB |
Output is correct |
26 |
Correct |
99 ms |
10168 KB |
Output is correct |
27 |
Correct |
276 ms |
16508 KB |
Output is correct |
28 |
Correct |
95 ms |
10296 KB |
Output is correct |
29 |
Correct |
305 ms |
16648 KB |
Output is correct |
30 |
Correct |
400 ms |
17712 KB |
Output is correct |
31 |
Correct |
410 ms |
29040 KB |
Output is correct |
32 |
Correct |
142 ms |
10592 KB |
Output is correct |
33 |
Correct |
124 ms |
10928 KB |
Output is correct |
34 |
Correct |
124 ms |
12048 KB |
Output is correct |
35 |
Correct |
137 ms |
12088 KB |
Output is correct |
36 |
Correct |
1133 ms |
29704 KB |
Output is correct |
37 |
Correct |
1456 ms |
30368 KB |
Output is correct |
38 |
Correct |
1567 ms |
28952 KB |
Output is correct |
39 |
Correct |
800 ms |
29564 KB |
Output is correct |
40 |
Correct |
1645 ms |
28964 KB |
Output is correct |
41 |
Correct |
877 ms |
10292 KB |
Output is correct |
42 |
Correct |
1106 ms |
10248 KB |
Output is correct |
43 |
Correct |
1121 ms |
16568 KB |
Output is correct |
44 |
Correct |
1277 ms |
16476 KB |
Output is correct |
45 |
Correct |
992 ms |
20676 KB |
Output is correct |
46 |
Correct |
1180 ms |
18588 KB |
Output is correct |
47 |
Correct |
1703 ms |
16616 KB |
Output is correct |
48 |
Correct |
454 ms |
11656 KB |
Output is correct |
49 |
Correct |
1123 ms |
11128 KB |
Output is correct |
50 |
Correct |
713 ms |
12176 KB |
Output is correct |
51 |
Correct |
2217 ms |
12172 KB |
Output is correct |
52 |
Correct |
2919 ms |
12120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
494 ms |
28616 KB |
Output is correct |
3 |
Correct |
620 ms |
29460 KB |
Output is correct |
4 |
Correct |
526 ms |
29360 KB |
Output is correct |
5 |
Correct |
620 ms |
29548 KB |
Output is correct |
6 |
Correct |
99 ms |
10168 KB |
Output is correct |
7 |
Correct |
276 ms |
16508 KB |
Output is correct |
8 |
Correct |
95 ms |
10296 KB |
Output is correct |
9 |
Correct |
305 ms |
16648 KB |
Output is correct |
10 |
Correct |
400 ms |
17712 KB |
Output is correct |
11 |
Correct |
410 ms |
29040 KB |
Output is correct |
12 |
Correct |
142 ms |
10592 KB |
Output is correct |
13 |
Correct |
124 ms |
10928 KB |
Output is correct |
14 |
Correct |
124 ms |
12048 KB |
Output is correct |
15 |
Correct |
137 ms |
12088 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Execution timed out |
4054 ms |
29676 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
494 ms |
28616 KB |
Output is correct |
3 |
Correct |
620 ms |
29460 KB |
Output is correct |
4 |
Correct |
526 ms |
29360 KB |
Output is correct |
5 |
Correct |
620 ms |
29548 KB |
Output is correct |
6 |
Correct |
99 ms |
10168 KB |
Output is correct |
7 |
Correct |
276 ms |
16508 KB |
Output is correct |
8 |
Correct |
95 ms |
10296 KB |
Output is correct |
9 |
Correct |
305 ms |
16648 KB |
Output is correct |
10 |
Correct |
400 ms |
17712 KB |
Output is correct |
11 |
Correct |
410 ms |
29040 KB |
Output is correct |
12 |
Correct |
142 ms |
10592 KB |
Output is correct |
13 |
Correct |
124 ms |
10928 KB |
Output is correct |
14 |
Correct |
124 ms |
12048 KB |
Output is correct |
15 |
Correct |
137 ms |
12088 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Execution timed out |
4065 ms |
29424 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
11 ms |
596 KB |
Output is correct |
6 |
Correct |
5 ms |
596 KB |
Output is correct |
7 |
Correct |
10 ms |
596 KB |
Output is correct |
8 |
Correct |
6 ms |
596 KB |
Output is correct |
9 |
Correct |
5 ms |
660 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
5 ms |
468 KB |
Output is correct |
16 |
Correct |
6 ms |
468 KB |
Output is correct |
17 |
Correct |
4 ms |
468 KB |
Output is correct |
18 |
Correct |
4 ms |
468 KB |
Output is correct |
19 |
Correct |
3 ms |
468 KB |
Output is correct |
20 |
Correct |
6 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
494 ms |
28616 KB |
Output is correct |
23 |
Correct |
620 ms |
29460 KB |
Output is correct |
24 |
Correct |
526 ms |
29360 KB |
Output is correct |
25 |
Correct |
620 ms |
29548 KB |
Output is correct |
26 |
Correct |
99 ms |
10168 KB |
Output is correct |
27 |
Correct |
276 ms |
16508 KB |
Output is correct |
28 |
Correct |
95 ms |
10296 KB |
Output is correct |
29 |
Correct |
305 ms |
16648 KB |
Output is correct |
30 |
Correct |
400 ms |
17712 KB |
Output is correct |
31 |
Correct |
410 ms |
29040 KB |
Output is correct |
32 |
Correct |
142 ms |
10592 KB |
Output is correct |
33 |
Correct |
124 ms |
10928 KB |
Output is correct |
34 |
Correct |
124 ms |
12048 KB |
Output is correct |
35 |
Correct |
137 ms |
12088 KB |
Output is correct |
36 |
Correct |
1133 ms |
29704 KB |
Output is correct |
37 |
Correct |
1456 ms |
30368 KB |
Output is correct |
38 |
Correct |
1567 ms |
28952 KB |
Output is correct |
39 |
Correct |
800 ms |
29564 KB |
Output is correct |
40 |
Correct |
1645 ms |
28964 KB |
Output is correct |
41 |
Correct |
877 ms |
10292 KB |
Output is correct |
42 |
Correct |
1106 ms |
10248 KB |
Output is correct |
43 |
Correct |
1121 ms |
16568 KB |
Output is correct |
44 |
Correct |
1277 ms |
16476 KB |
Output is correct |
45 |
Correct |
992 ms |
20676 KB |
Output is correct |
46 |
Correct |
1180 ms |
18588 KB |
Output is correct |
47 |
Correct |
1703 ms |
16616 KB |
Output is correct |
48 |
Correct |
454 ms |
11656 KB |
Output is correct |
49 |
Correct |
1123 ms |
11128 KB |
Output is correct |
50 |
Correct |
713 ms |
12176 KB |
Output is correct |
51 |
Correct |
2217 ms |
12172 KB |
Output is correct |
52 |
Correct |
2919 ms |
12120 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Execution timed out |
4054 ms |
29676 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |