Submission #493203

# Submission time Handle Problem Language Result Execution time Memory
493203 2021-12-10T10:55:15 Z inksamurai Index (COCI21_index) C++17
110 / 110
1489 ms 145948 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _3cSpNGp ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;

struct node{
	int val=0;
	node *l,*r;
	node(int _val) : val(_val),l(nullptr),r(nullptr){}
	node(node *_l,node *_r) : val(0),l(_l),r(_r){
		if(l) val+=l->val;
		if(r) val+=r->val;
	}
};
 
node* roots[200006];
 
node* build(int l,int r){
	if(l==r-1)
		return new node(0);
	int m=(l+r)>>1;
	return new node(build(l,m),build(m,r));
}
 
node* update(node* v,int l,int r,int pos,int val){
	if(l==r-1){
		return new node(val);
	}
	int m=(l+r)>>1;
	if(pos<m)
		return new node(update(v->l,l,m,pos,val),v->r);
	return new node(v->l,update(v->r,m,r,pos,val));
}
 
int prod(node* v,int l,int r,int _l,int _r){
	if(l>=_r or r<=_l)
		// _e
		return 0;
	if(_l<=l and r<=_r)
		return v->val;
	int m=(l+r)>>1;
	return prod(v->l,l,m,_l,_r)+prod(v->r,m,r,_l,_r);
}

int get(node* v,int l,int r,int pos){
	// cout<<pos<<"\n";
	if(l==r-1) 
		return v->val;
	int m=(l+r)>>1;
	if(pos<m)
		return get(v->l,l,m,pos);
	return get(v->r,m,r,pos);
}
 

int main(){
_3cSpNGp;
	int n,q;
	cin>>n>>q;
	vi a(n);
	rep(i,n) cin>>a[i];
	rep(i,n) a[i]=min(a[i],n);
	vec(vi) rbts(n+1);
	rep(i,n){
		rbts[a[i]].pb(i);
	}
	roots[n+1]=build(0,n);
	drep(i,n+1){
		if(!i) break;
		roots[i]=roots[i+1];
		for(auto j : rbts[i]){
			roots[i]=update(roots[i],0,n,j,1);
		}
	}
	auto bs=[&](int l,int r)->int{
		int _l=1,_r=n,_c=0;
		while(_l<=_r){
			int _m=(_l+_r)>>1;
			if(prod(roots[_m],0,n,l,r)>=_m){
				_c=_m;
				_l=_m+1;
			}else{
				_r=_m-1;
			}
		}
		return _c;
	};
	rep(_,q){
		int l,r;
		cin>>l>>r;
		cout<<bs(l-1,r)<<"\n";
	}
//	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
3 Correct 2 ms 716 KB Output is correct
4 Correct 2 ms 716 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 2 ms 716 KB Output is correct
7 Correct 2 ms 716 KB Output is correct
8 Correct 2 ms 716 KB Output is correct
9 Correct 2 ms 716 KB Output is correct
10 Correct 2 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
3 Correct 2 ms 716 KB Output is correct
4 Correct 2 ms 716 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 2 ms 716 KB Output is correct
7 Correct 2 ms 716 KB Output is correct
8 Correct 2 ms 716 KB Output is correct
9 Correct 2 ms 716 KB Output is correct
10 Correct 2 ms 716 KB Output is correct
11 Correct 230 ms 32508 KB Output is correct
12 Correct 220 ms 32744 KB Output is correct
13 Correct 218 ms 32564 KB Output is correct
14 Correct 217 ms 32616 KB Output is correct
15 Correct 219 ms 32580 KB Output is correct
16 Correct 221 ms 32508 KB Output is correct
17 Correct 227 ms 32708 KB Output is correct
18 Correct 230 ms 32576 KB Output is correct
19 Correct 212 ms 32540 KB Output is correct
20 Correct 226 ms 32708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 716 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
3 Correct 2 ms 716 KB Output is correct
4 Correct 2 ms 716 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 2 ms 716 KB Output is correct
7 Correct 2 ms 716 KB Output is correct
8 Correct 2 ms 716 KB Output is correct
9 Correct 2 ms 716 KB Output is correct
10 Correct 2 ms 716 KB Output is correct
11 Correct 230 ms 32508 KB Output is correct
12 Correct 220 ms 32744 KB Output is correct
13 Correct 218 ms 32564 KB Output is correct
14 Correct 217 ms 32616 KB Output is correct
15 Correct 219 ms 32580 KB Output is correct
16 Correct 221 ms 32508 KB Output is correct
17 Correct 227 ms 32708 KB Output is correct
18 Correct 230 ms 32576 KB Output is correct
19 Correct 212 ms 32540 KB Output is correct
20 Correct 226 ms 32708 KB Output is correct
21 Correct 1404 ms 144328 KB Output is correct
22 Correct 1448 ms 145888 KB Output is correct
23 Correct 1431 ms 145948 KB Output is correct
24 Correct 1489 ms 145868 KB Output is correct
25 Correct 1333 ms 145884 KB Output is correct
26 Correct 1425 ms 145888 KB Output is correct
27 Correct 1358 ms 145756 KB Output is correct
28 Correct 1376 ms 145904 KB Output is correct
29 Correct 1354 ms 145824 KB Output is correct
30 Correct 1430 ms 145732 KB Output is correct