题意:给你一个一串已经排好序的数,再给你两个区间L1,R1,L2,R2
问这两个区间组成的新的一串数的中位数是多少。
思路:刚开始想可以分成三种情况包含,分离和相交
后来仔细一想,包含和相交可以通过交换R1,R2来合并,这样就只有两种情况了,模拟即可。
因为我在写新串偶数情况时先将两个数加起来,这样的话就暴int了,改成long long就过了,WA了无数发..
#include<bits/stdc++.h>
using namespace std;
long long a[100005];
int main(){
int t,n,m,l1,l2,r1,r2;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
scanf("%lld",&a[i]);
}
while(m--){
scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
if(l2<l1){
swap(l1,l2);
swap(r1,r2);
}
if(l2>=l1&&r2<=r1){
swap(r1,r2);
}
long long len,L1,L2,L3;
long long ans=0;
if(l2>r1){
len=r1-l1+1+r2-l2+1;L1=r1-l1+1;L2=r2-l2+1;
if(len%2==0){
if(len/2<=L1){
ans+=a[l1+len/2-1];
}else{
ans+=a[l2+len/2-L1-1];
}
if(len/2+1<=L1){
ans+=a[l1+len/2];
}else{
ans+=a[l2+len/2-L1];
}
printf("%.1lf\n",ans*1.0/2);
}else{
if(len/2+1<=L1){
ans+=a[l1+len/2];
}else{
ans+=a[l2+len/2-L1];
}
printf("%.1lf\n",ans*1.0);
}
}else{
len=(r1-l2+1)*2+l2-l1+r2-r1;L1=l2-l1;L2=r2-r1;L3=(r1-l2+1)*2;
//cout<<len<<" "<<L1<<" "<<L2<<" "<<L3<<endl;
if(len%2==0){
if(len/2>L1&&len/2<=L1+L3){
ans+=a[l2+(len/2-L1)/2-((len/2-L1)%2==0?1:0)];
}else if(len/2<=L1){
ans+=a[l1+len/2-1];
}else if(len/2>L1+L3){
ans+=a[r1+(len/2-L3-L1)];
}
if(len/2+1>L1&&len/2+1<=L1+L3){
ans+=a[l2+(len/2+1-L1)/2-((len/2+1-L1)%2==0?1:0)];
}else if(len/2+1<=L1){
ans+=a[l1+len/2];
}else if(len/2+1>L1+L3){
ans+=a[r1+(len/2+1-L3-L1)];
}
printf("%.1lf\n",ans*1.0/2);
}else{
if(len/2+1>L1&&len/2+1<=L1+L3){
ans+=a[l2+(len/2+1-L1)/2-((len/2+1-L1)%2==0?1:0)];
}else if(len/2+1<=L1){
ans+=a[l1+len/2];
}else if(len/2+1>L1+L3){
ans+=a[r1+(len/2+1-L3-L1)];
}
printf("%.1lf\n",ans*1.0);
}
}
}
}
}