Median

HDU - 5857

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.

InputFirst 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. 
OutputFor 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

给你一个有序序列,让你再两个区间内找到构造成新序列的其中位数

这个题,我们可以把它转换为两个平衡的区间,即确保第一个l和r都比第二个的小

然后就有是否相交,相交了在左边,在中间,在右边

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
int l1,r1,l2,r2;
int a[N];
int la(int s)
{
if(r1<=l2)
{
if(r1-l1+>=s)return a[l1+s-];
s-=r1-l1+;
return a[l2+s-];
}
if(l2-l1>=s)return a[l1+s-];
if(l2-l1>=s-(r1-l2+)*)
{
s-=l2-l1+;
return a[l2+s/];
}
s-=r1-l1+;
return a[l2+s-]; }
int main()
{
ios::sync_with_stdio(false),cin.tie(),cout.tie();
int T;
cin>>T;
while(T--)
{
int n,q;
cin>>n>>q;
for(int i=; i<=n; i++)cin>>a[i];
for(int i=; i<q; i++)
{
cin>>l1>>r1>>l2>>r2;
if(l2<l1)swap(l1,l2);
if(r2<r1)swap(r1,r2);
int cd=r2+r1-l1-l2+;
if(cd&)
{
printf("%.1f\n",1.0*la(cd/+));
}
else
{
printf("%.1f\n",0.5*la(cd/+)+0.5*la(cd/));
}
}
}
return ;
}

最新文章

  1. 剖析AngularJS作用域
  2. iOS 从某个页面返回然后刷新当前页面
  3. leetcode-Combinations 复习复习排列组合
  4. 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
  5. 【JQGRID DOCUMENTATION】.学习笔记.3.Pager
  6. LeetCode题解——ZigZag Conversion
  7. 查找mysql数据文件存放路径
  8. 我对DFS的理解
  9. nginx,作为前端的你会多少?
  10. February 15th, 2018 Week 7th Thursday
  11. Windows PowerShell 入門(10)-デバッグ編
  12. 【Arduino】开源开发板说明
  13. docker单机网络类型
  14. Vim 命令、操作、快捷键
  15. ESLint 使用方法
  16. Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
  17. 9.简单理解ajax
  18. Delphi : keydown与keypress的区别,组合键
  19. CentOS 7升级php5.4到php7.2
  20. robot framework学习笔记之三—Scalar变量

热门文章

  1. 进程peb结构、获得peb的方法
  2. SQLSERVER 创建ODBC 报错的解决办法 SQLState:&#39;01000&#39;的解决方案
  3. 论overflow滚动的重要性
  4. C#中?和??用法
  5. wu2198:难得的波段抄底机会
  6. 51+Nokia5110
  7. C#Json数据交互
  8. Windows服务调试
  9. java基础面试题:请说出作用域public,private,protected,以及不写时的区别
  10. ZRDay6A. 萌新拆塔(三进制状压dp)