216人阅读 评论(0)

# Median

.
.

.
.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int maxn = 100010;
long long a[maxn];
int l, r, l1, r1, l2, r2, mergel, merger, sum, n, m;

long long find1(int t) {
if (l1+t-1 <= r1) return a[l1+t-1];
t = t-(r1-l1+1);
return a[l2+t-1];
}

long long find2(int t) {
l = l1;
r = max(r1, r2);
mergel = l2;
merger = min(r1, r2);
if (l+t-1 < mergel) return a[l+t-1];
t = t-(mergel-l);
if (mergel+(t-1)/2 <= merger) return a[mergel+(t-1)/2];
t = t-(merger-mergel+1)*2;
return a[merger+t];
}

int main() {
int tt;
scanf("%d", &tt);
while (tt--) {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
while (m--) {
scanf("%d %d", &l1, &r1);
scanf("%d %d", &l2, &r2);
if (l1 > l2) {
swap(l1, l2);
swap(r1, r2);
}
sum = (r1-l1+1)+(r2-l2+1);
if (r1 < l2) {
double ans = find1((sum+1)/2)+find1((sum+2)/2);
printf("%.1lf\n", ans/2.0);
} else {
double ans = find2((sum+1)/2)+find2((sum+2)/2);
printf("%.1lf\n", ans/2.0);
}
}
}
}

个人资料
等级：
访问量： 2万+
积分： 2395
排名： 1万+
文章分类
最新评论