답안 #572061

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
572061 2022-06-03T12:45:17 Z gs14004 Fish 2 (JOI22_fish2) C++17
5 / 100
4000 ms 14436 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;

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;

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 = 0, rightClosest = n + 1;
		for(int i = l - 1; i > 0; i--){
			if(a[i] > curSum){
				leftClosest = i;
				break;
			}
		}
		for(int i = r + 1; i <= n; i++){
			if(a[i] > curSum){
				rightClosest = i;
				break;
			}
		}
		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;
	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;
	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";
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 6 ms 572 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 5 ms 596 KB Output is correct
8 Correct 4 ms 584 KB Output is correct
9 Correct 4 ms 652 KB Output is correct
10 Correct 4 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 3 ms 460 KB Output is correct
14 Correct 3 ms 468 KB Output is correct
15 Correct 4 ms 468 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 4 ms 584 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 3 ms 340 KB Output is correct
20 Correct 4 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Execution timed out 4059 ms 14436 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 6 ms 572 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 5 ms 596 KB Output is correct
8 Correct 4 ms 584 KB Output is correct
9 Correct 4 ms 652 KB Output is correct
10 Correct 4 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 3 ms 460 KB Output is correct
14 Correct 3 ms 468 KB Output is correct
15 Correct 4 ms 468 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 4 ms 584 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 3 ms 340 KB Output is correct
20 Correct 4 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Execution timed out 4059 ms 14436 KB Time limit exceeded
23 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Execution timed out 4059 ms 14436 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Execution timed out 4059 ms 14436 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 6 ms 572 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 5 ms 596 KB Output is correct
8 Correct 4 ms 584 KB Output is correct
9 Correct 4 ms 652 KB Output is correct
10 Correct 4 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 3 ms 460 KB Output is correct
14 Correct 3 ms 468 KB Output is correct
15 Correct 4 ms 468 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 4 ms 584 KB Output is correct
18 Correct 3 ms 340 KB Output is correct
19 Correct 3 ms 340 KB Output is correct
20 Correct 4 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Execution timed out 4059 ms 14436 KB Time limit exceeded
23 Halted 0 ms 0 KB -