답안 #935455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
935455 2024-02-29T06:29:52 Z RadicaI Growing Trees (BOI11_grow) C++17
100 / 100
495 ms 5948 KB
#include <bits/stdc++.h>
#define int long long
#define pi pair<int,int>
#define pb push_back
#define V vector
#define vi V<int>
#define mi map<int,int>
#define MOD 1000000007
#define MOD2 998244353
#define mp make_pair
#define ins insert
#define qi queue<int>
#define pqi priority_queue<int>
#define si set<int>
#define v2i V<vi>
#define v3i V<v2i>
#define v4i V<v3i>
#define v5i V<v4i>
#define INF 1e18
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define endl "\n"
#define lb lower_bound
#define ub upper_bound
#define print(x) cout << x<<" ";
#define vpi V<pi>
#define Y cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define msi multiset<int>
#define F first
#define S second
#define ld long double
#define RE return;
#define IMP cout<<-1<<endl;return;
using namespace std;

template <typename T>
std::istream& operator>>(std::istream& in, std::vector<T>& vec) {
    for (T& x : vec) {
        in >> x;
    }
    return in;
}

vi arr; vi heights;

void build(int node, int l, int r){
	if(l==r){
		arr[node] = (l==0 ? heights[l] : heights[l]-heights[l-1]);
		return;
	}
	int mid = (l+r)/2;
	build(2*node,l,mid); build(2*node+1, mid+1, r);
	arr[node] = arr[2*node]+arr[2*node+1];
}

void update(int node, int l, int r, int i, int val){
	if(i == sz(heights)) return;
	if(l==r){
		arr[node] += val;
		return;
	}
	int mid = (l+r)/2;
	if(mid<i) update(2*node+1, mid+1, r, i, val);
	else update(2*node, l, mid, i, val);
	arr[node] = arr[2*node]+arr[2*node+1];
}

int query(int node, int l, int r, int tl, int tr){
	if(tl>tr) return 0;
	if(l==tl && r==tr) return arr[node];
	int mid  = (l+r)/2;
	return query(2*node, l, mid, tl, min(mid, tr)) + query(2*node+1, mid+1, r, max(tl, mid+1), tr);
}

int findl(int val){
	int l=0; int r = sz(heights)-1; int best = -1; int rand=0;
	while(l<=r && rand<=2){
		int mid = (l+r+1)/2;
		if(query(1, 0, sz(heights)-1, 0, mid)<=val){
			best = mid;
			l = mid;
		}else r = mid-1;
		if(l==r) rand++;
	}
	return best;
}

signed main(){
	int n,q; cin >> n>>q; heights.resize(n); arr.resize(4*n); cin >> heights; sort(all(heights));
	build(1, 0, n-1);
	while(q--){
		char c; cin >> c;
		if(c=='C'){
			int l,r; cin >>l>>r;
			cout<<findl(r) - findl(l-1)<<endl;
		}else{
			int cnt,h; cin>>cnt>>h;
			int fst = findl(h-1)+1;
			int lst = fst + cnt-1;
			if(fst==n) continue;
			if(fst + cnt>=n){
				update(1, 0, n-1, fst, 1);
				continue;
			}
			int jhxt = query(1, 0, n-1, 0, lst);
			int aft = findl(jhxt); int bef = findl(jhxt-1);
			if(bef>=fst){
				update(1, 0, n-1, fst, 1);
				update(1, 0, n-1, bef+1, -1);
				update(1, 0, n-1, aft+1, -1);
				update(1, 0, n-1, aft - (lst - bef)+1, 1);
			}else{
				update(1, 0, n-1, aft+1,-1);
				update(1, 0, n-1, aft-cnt+1, 1);
			}
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 270 ms 4004 KB Output is correct
2 Correct 426 ms 5536 KB Output is correct
3 Correct 151 ms 5160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 348 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 10 ms 344 KB Output is correct
4 Correct 6 ms 504 KB Output is correct
5 Correct 242 ms 656 KB Output is correct
6 Correct 324 ms 1124 KB Output is correct
7 Correct 14 ms 604 KB Output is correct
8 Correct 173 ms 1308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 267 ms 1040 KB Output is correct
2 Correct 283 ms 2068 KB Output is correct
3 Correct 4 ms 600 KB Output is correct
4 Correct 225 ms 1608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 245 ms 1104 KB Output is correct
2 Correct 324 ms 1912 KB Output is correct
3 Correct 25 ms 860 KB Output is correct
4 Correct 296 ms 2080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 296 ms 3152 KB Output is correct
2 Correct 408 ms 4612 KB Output is correct
3 Correct 82 ms 1528 KB Output is correct
4 Correct 99 ms 4616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 359 ms 3356 KB Output is correct
2 Correct 400 ms 4868 KB Output is correct
3 Correct 132 ms 4816 KB Output is correct
4 Correct 77 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 323 ms 3728 KB Output is correct
2 Correct 262 ms 5136 KB Output is correct
3 Correct 128 ms 5072 KB Output is correct
4 Correct 74 ms 1508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 431 ms 3924 KB Output is correct
2 Correct 395 ms 4944 KB Output is correct
3 Correct 73 ms 4956 KB Output is correct
4 Correct 298 ms 5136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 391 ms 4124 KB Output is correct
2 Correct 429 ms 5456 KB Output is correct
3 Correct 495 ms 5948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 479 ms 4784 KB Output is correct