답안 #403928

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
403928 2021-05-13T15:15:35 Z silverfish Index (COCI21_index) C++17
110 / 110
2238 ms 133532 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> pii;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pb push_back
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define TC int __TC__; cin >> __TC__; while(__TC__--)
#define ar array

const int INF = 1e9 + 7, N = 2e5;

int n, q, p[N];

struct node{
	node* left = nullptr,* right = nullptr;
	int v = 0;
};

node* root[N+1];

void build(node *rt, int lx, int rx){
	if(lx == rx) return;
	int m = lx+(rx-lx) / 2;

	rt->left = new node;
	rt->right = new node;
	build(rt->left, lx, m);
	build(rt->right, m+1, rx);
	return;
}


void add(int pos, node* lr, node* nr, int lx, int rx){
	if(lx == rx){
		++nr->v;
		return;
	}

	int m = lx + (rx - lx) / 2;

	if(pos <= m){ // go left
		nr->right = lr->right;
		nr->left = new node;
		nr->left->v = lr->left->v;
		add(pos, lr->left, nr->left, lx, m);
	}else{ // go right
		nr->left = lr->left;
		nr->right = new node;
		nr->right->v = lr->right->v;
		add(pos, lr->right, nr->right, m+1, rx);
	}
	nr->v = nr->left->v + nr->right->v;
}

int get(int l, int r, node* rt, int lx, int rx){
	if(r < lx || l > rx) return 0;
	if(lx >= l && rx <= r) return rt->v;
	int m = lx + (rx - lx) / 2;

	return get(l, r, rt->left, lx, m) + get(l, r, rt->right, m+1, rx);
}

int main(void)
{
	FAST;
	cin >> n >> q;

	for(int i = 0; i < n; ++i){
		cin >> p[i];
	}

	root[0] = new node;

	build(root[0], 1, N);

	for(int i = 1; i <= n; ++i){
		root[i] = new node;	
		add(p[i-1], root[i-1], root[i], 1, N); 
	}
	
	while(q--){
		int l, r; cin >> l >> r;
		int ans = 0;
		for(int j = 18; j >= 0; --j){
			int nans = ans + (1<<j);
			if(nans > N) continue;
			if(get(nans, N, root[r], 1, N) - get(nans, N, root[l-1], 1, N) >= nans) ans += (1<<j);
		}
		cout << ans << '\n';
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 13388 KB Output is correct
2 Correct 23 ms 13388 KB Output is correct
3 Correct 26 ms 13452 KB Output is correct
4 Correct 23 ms 13384 KB Output is correct
5 Correct 23 ms 13412 KB Output is correct
6 Correct 23 ms 13384 KB Output is correct
7 Correct 23 ms 13356 KB Output is correct
8 Correct 23 ms 13344 KB Output is correct
9 Correct 24 ms 13456 KB Output is correct
10 Correct 24 ms 13400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 13388 KB Output is correct
2 Correct 23 ms 13388 KB Output is correct
3 Correct 26 ms 13452 KB Output is correct
4 Correct 23 ms 13384 KB Output is correct
5 Correct 23 ms 13412 KB Output is correct
6 Correct 23 ms 13384 KB Output is correct
7 Correct 23 ms 13356 KB Output is correct
8 Correct 23 ms 13344 KB Output is correct
9 Correct 24 ms 13456 KB Output is correct
10 Correct 24 ms 13400 KB Output is correct
11 Correct 422 ms 42948 KB Output is correct
12 Correct 435 ms 42956 KB Output is correct
13 Correct 398 ms 43088 KB Output is correct
14 Correct 398 ms 42928 KB Output is correct
15 Correct 420 ms 43012 KB Output is correct
16 Correct 440 ms 42880 KB Output is correct
17 Correct 474 ms 42884 KB Output is correct
18 Correct 452 ms 42984 KB Output is correct
19 Correct 468 ms 42944 KB Output is correct
20 Correct 443 ms 42972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 13388 KB Output is correct
2 Correct 23 ms 13388 KB Output is correct
3 Correct 26 ms 13452 KB Output is correct
4 Correct 23 ms 13384 KB Output is correct
5 Correct 23 ms 13412 KB Output is correct
6 Correct 23 ms 13384 KB Output is correct
7 Correct 23 ms 13356 KB Output is correct
8 Correct 23 ms 13344 KB Output is correct
9 Correct 24 ms 13456 KB Output is correct
10 Correct 24 ms 13400 KB Output is correct
11 Correct 422 ms 42948 KB Output is correct
12 Correct 435 ms 42956 KB Output is correct
13 Correct 398 ms 43088 KB Output is correct
14 Correct 398 ms 42928 KB Output is correct
15 Correct 420 ms 43012 KB Output is correct
16 Correct 440 ms 42880 KB Output is correct
17 Correct 474 ms 42884 KB Output is correct
18 Correct 452 ms 42984 KB Output is correct
19 Correct 468 ms 42944 KB Output is correct
20 Correct 443 ms 42972 KB Output is correct
21 Correct 2238 ms 133332 KB Output is correct
22 Correct 2108 ms 133532 KB Output is correct
23 Correct 2081 ms 133448 KB Output is correct
24 Correct 2039 ms 133332 KB Output is correct
25 Correct 2221 ms 133344 KB Output is correct
26 Correct 2102 ms 133408 KB Output is correct
27 Correct 2172 ms 133312 KB Output is correct
28 Correct 2114 ms 133392 KB Output is correct
29 Correct 2072 ms 133372 KB Output is correct
30 Correct 2168 ms 133516 KB Output is correct