2015
05-23

# The Famous ICPC Team Again

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.

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.

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.

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

Case 1:
3
3
2
Case 2:
6
6
4

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<cmath>
using namespace std;
#define M 100007

int a[20][M], toleft[20][M], sorted[M], n;
void build( int p, int l, int r )
{
if( l == r ) return;
int m = (l+r)>>1, lp = l, rp = m+1, num = 0;

for( int i = l; i <= r; i++ ){
if( a[p][i] < sorted[m] ) num++;
}
num = m - l + 1 - num;       //用于处理多个中位数，表示放在左边的中位数的个数
for( int i = l; i <= r; i++ ){
if( i == l ) toleft[p][i] = 0;
else toleft[p][i] = toleft[p][i-1];
if( a[p][i] < sorted[m] ){
toleft[p][i]++;
a[p+1][lp++] = a[p][i];
}
else if( a[p][i] > sorted[m] ){
a[p+1][rp++] = a[p][i];
}
else if( num > 0 ){   //中位数放在左边
toleft[p][i]++;
a[p+1][lp++] = a[p][i];
num--;
}
else a[p+1][rp++] = a[p][i]; //中位数放在右边
}
build( p+1, l, m );
build( p+1, m+1, r );
}

int query( int p, int l, int r, int x, int y, int k )
{
if( l==r ) return a[p][l];
int s, ss, newl, newr, newk, m = (l+r)>>1;
if( l == x ){
s = 0;
ss = toleft[p][y];
}
else{
s = toleft[p][x-1];
ss = toleft[p][y];
}
if( k <= ss - s ){
return query( p+1, l, m, l+s, l+ss-1, k );
}
else{
newl = m + 1 + x - l - s;//相对位置
newr = m + 1 + y - l - ss;
newk = k - ( ss - s );
return query( p+1, m+1, r, newl, newr, newk );
}
}
int main()
{
int m, t = 0, x, y, k;
while( scanf( "%d", &n ) == 1 ){
printf( "Case %d:\n", ++t );
for( int i = 1; i <= n; i++ ){
scanf( "%d", &a[0][i] );
sorted[i] = a[0][i];
}
sort( sorted + 1, sorted + n + 1); //排序用于求中位数
build( 0, 1, n );
scanf( "%d", &m );
while( m-- ){
scanf( "%d%d", &x, &y );
k = (y-x)/2 + 1;
int ans = query( 0, 1, n, x, y, k );
printf( "%d\n", ans );
}
}
}


1. 第一，没章的话只是一张普通纸；第二，有公章的话，以我浅薄的创业经历来看，目的性有以下2种：软文、辅助筛选不合适的成员开掉。总之作为公司来说，图上政策是不可能实现的，因为太极端了，属于说了也不可能执行的事情，尤其是1、3、5，有人可能在上班时间去捏脚？真的