标签:rom -- cas ota icpc needed esc publish content
。
。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100000+10;
int n,m;
struct node{
int lson,rson;
int mid(){
return (lson+rson)>>1;
}
}tree[maxn*4];
int num[maxn];
int seg[25][maxn];
int lftnum[25][maxn];
void build(int L,int R,int rt,int dep){
tree[rt].lson = L;
tree[rt].rson = R;
if(L==R) return;
int mid = tree[rt].mid(),key = num[mid];
int lcnt = mid-L+1;
for(int i = L; i <= R; i++){
if(seg[dep][i] < key){
lcnt--;
}
}
int sta = L, ed = mid+1;
for(int i = L;i <= R; i++){
if(i==L){
lftnum[dep][i] = 0;
}else{
lftnum[dep][i] = lftnum[dep][i-1];
}
if(seg[dep][i] < key){
lftnum[dep][i]++;
seg[dep+1][sta++] = seg[dep][i];
}
else if(seg[dep][i] > key){
seg[dep+1][ed++] = seg[dep][i];
}
else{
if(lcnt>0){
lftnum[dep][i]++;
lcnt--;
seg[dep+1][sta++] = seg[dep][i];
}else{
seg[dep+1][ed++] = seg[dep][i];
}
}
}
build(L,mid,rt<<1,dep+1);
build(mid+1,R,rt<<1|1,dep+1);
}
int query(int L,int R,int rt,int dep,int k){
if(tree[rt].lson ==tree[rt].rson) return seg[dep][tree[rt].lson];
int ucnt,ncnt;
int mid = tree[rt].mid();
if(tree[rt].lson == L){
ncnt = 0;
ucnt = lftnum[dep][R];
}else{
ncnt = lftnum[dep][L-1];
ucnt = lftnum[dep][R] - lftnum[dep][L-1];
}
if(ucnt >= k){
L = tree[rt].lson + ncnt;
R = tree[rt].lson + ncnt + ucnt-1;
return query(L,R,rt<<1,dep+1,k);
}else{
int a = L - tree[rt].lson - ncnt;
int b = R - L - ucnt + 1;
L = mid+a+1;
R = mid+a+b;
return query(L,R,rt<<1|1,dep+1,k-ucnt);
}
}
int main(){
int T = 1;
while(cin >> n){
memset(seg,0,sizeof seg);
memset(lftnum,0,sizeof lftnum);
for(int i = 1; i <= n; i++){
scanf("%d",&num[i]);
seg[0][i] = num[i];
}
sort(num+1,num+n+1);
build(1,n,1,0);
scanf("%d",&m);
printf("Case %d:\n",T++);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",query(a,b,1,0,(b-a)/2+1));
}
}
return 0;
}
HDU4251-The Famous ICPC Team Again(划分树)
标签:rom -- cas ota icpc needed esc publish content
原文地址:https://www.cnblogs.com/ldxsuanfa/p/10494999.html