The Famous ICPC Team Again
Problem Description
When Mr. B, Mr. G and Mr. M were preparing for the 2012 ACM-ICPC World Final Contest, Mr. B had collected a large set of contest problems for their daily training. When they decided to take training, Mr. B would choose one of them from the problem set. All the problems in the problem set had been sorted by their time of publish. Each time Prof. S, their coach, would tell them to choose one problem published within a particular time interval. That is to say, if problems had been sorted in a line, each time they would choose one of them from a specified segment of the line.
Moreover, when collecting the problems, Mr. B had also known an estimation of each problem’s difficultness. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. To address this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore, he needed a way to know the median number in the given interval of the sequence.
Input
For each test case, the first line contains a single integer n (1 <= n <= 100,000) indicating the total number of problems. The second line contains n integers xi (0 <= xi <= 1,000,000,000), separated by single space, denoting the difficultness of each problem, already sorted by publish time. The next line contains a single integer m (1 <= m <= 100,000), specifying number of queries. Then m lines follow, each line contains a pair of integers, A and B (1 <= A <= B <= n), denoting that Mr. B needed to choose a problem between positions A and B (inclusively, positions are counted from 1). It is guaranteed that the number of items between A and B is odd.
Output
For each query, output a single line containing an integer that denotes the difficultness of the problem that Mr. B should choose.
Sample Input
5
5 3 2 4 1
3
1 3
2 4
3 5
5
10 6 4 8 2
3
1 3
2 4
3 5
5 3 2 4 1
3
1 3
2 4
3 5
5
10 6 4 8 2
3
1 3
2 4
3 5
Sample Output
Case 1:
3
3
2
Case 2:
6
6
4
3
3
2
Case 2:
6
6
4
题意:
给你n个数,m个询问
每次问你l,r这个区间内的 中位数是多少
题解:
离散化
再套可持久化线段树即可
#include<bits/stdc++.h>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e6+, mod = 1e6+, inf = 2e9; int n,a[N],q,san[N],fsan[N],b[N],c;
struct cooltree{
int root[N],l[N*],r[N*],v[N*];
int sz;
void init()
{
memset(root,,sizeof(root));
memset(l,,sizeof(l));
memset(r,,sizeof(r));
memset(v,,sizeof(v));
sz = ;
}
void update(int &k,int ll,int rr,int x)
{
++sz;
l[sz] = l[k];
r[sz] = r[k];
v[sz] = v[k] + ;
k = sz;
if(ll == rr) return ;
if(x <= mid) update(l[k],ll,mid,x);
else update(r[k],mid+,rr,x);
}
int ask(int x,int y,int k)
{
int ll = , rr = c;
x = root[x-], y = root[y];
while(ll != rr)
{
int md = (ll+rr)>>, now = v[l[y]] - v[l[x]];
if(k <= now) x = l[x], y = l[y], rr = md;
else x = r[x], y = r[y], ll = mid + ,k-=now;
}
return b[ll];
}
}T;
int main() {
int cas = ;
while(scanf("%d",&n)!=EOF) {
T.init();
for(int i = ; i <= n; ++i) scanf("%d",&a[i]), b[i] = a[i];
sort(b+,b+n+);
c = unique(b+,b+n+) - b - ; for(int i = ; i <= n; ++i) san[i] = lower_bound(b+,b+c+,a[i]) - b;
for(int i = ; i <= n; ++i) T.update(T.root[i] = T.root[i-],,c,san[i]); scanf("%d",&q);
printf("Case %d:\n",cas++);
for(int i = ; i <= q; ++i) {
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",T.ask(x,y,(T.v[T.root[y]] - T.v[T.root[x-]] +)/));
}
}
return ;
}