Problem Description
There is a sorted sequence A of length n. Give you m queries, each one contains four integers, l1, r1, l2, r2. You should use the elements A[l1], A[l1+1] ... A[r1-1], A[r1] and A[l2], A[l2+1] ... A[r2-1], A[r2] to form a new sequence, and you need to find the median of the new sequence.
Input
First line contains a integer T, means the number of test cases. Each case begin with two integers n, m, means the length of the sequence and the number of queries. Each query contains two lines, first two integers l1, r1, next line two integers l2, r2, l1<=r1 and l2<=r2.
T is about 200.
For 90% of the data, n, m <= 100
For 10% of the data, n, m <= 100000
A[i] fits signed 32-bits int.
T is about 200.
For 90% of the data, n, m <= 100
For 10% of the data, n, m <= 100000
A[i] fits signed 32-bits int.
Output
For each query, output one line, the median of the query sequence, the answer should be accurate to one decimal point.
Sample Input
1 4 2 1 2 3 4 1 2 2 4 1 1 2 2
Sample Output
2.0 1.5
一开始直接写了主席树然后又T又WA不知道挂在哪里了。
然后发现是sorted的。所以重新弄了个大讨论
#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[100010];
inline double query(int l1,int r1,int l2,int r2,int x)
{
if(r1<l2)
return x<=r1-l1+1?a[l1+x-1]:a[l2+(x-(r1-l1+1))-1];
else if(r1>=r2)
{
if(x<=l2-l1)
return a[l1+x-1];
else if(x>r2-l1+1+r2-l2+1)
return a[r2+x-(r2-l1+1+r2-l2+1)];
else
return a[l2-1+(x-(l2-l1)+1)/2];
}
else
{
if(x<=l2-l1)
return a[l1+x-1];
else if(x>r1-l1+1+r1-l2+1)
return a[r1+x-(r1-l1+1+r1-l2+1)];
else
return a[l2-1+(x-(l2-l1)+1)/2];
}
}
int main()
{
int T;
scanf("%d",&T);
while(T>0)
{
T--;
int n,m;
scanf("%d%d",&n,&m);
int i;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
int l1,l2,r1,r2;
for(i=1;i<=m;i++)
{
scanf("%d%d",&l1,&r1);
scanf("%d%d",&l2,&r2);
if(l1>l2) swap(l1,l2),swap(r1,r2);
int sx=r1-l1+1+r2-l2+1;
if(sx%2==1)
printf("%.1lf\n",query(l1,r1,l2,r2,sx/2+1));
else
printf("%.1lf\n",(query(l1,r1,l2,r2,sx/2)+query(l1,r1,l2,r2,sx/2+1))/2);
}
}
return 0;
}