HDU - 5857 Median 细心+细节

/* 分类讨论不明确 */ 
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
#define l first
#define r second
typedef long long ll;
int a[100002];
pair<unsigned int,unsigned int> p[2],tp;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
	   int n,m;
	   scanf("%d%d",&n,&m);
	   for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	   while(m--)
	   {
	   	  scanf("%d%d%d%d",&p[0].l,&p[0].r,&p[1].l,&p[1].r);
	   	  sort(p,p+2);
	   	unsigned int d0=p[0].r-p[0].l+1;
	   	unsigned int d1=p[1].r-p[1].l+1;
	   	unsigned int m1=(d0+d1)/2+1;
	   	  double show=0;
	   	  if(p[0].r<p[1].l)
	   	  {
	   	  	  if(m1<=d0) show +=a[p[0].l+m1-1];
	   	  	  else show +=a[p[1].l+m1-d0-1];
	   	  	  if((d0+d1)%2==0)
	   	  	  {
	   	  	  	 m1--;
	   	  	  	if(m1<=d0) show +=a[p[0].l+m1-1];
	   	  	    else show +=a[p[1].l+m1-d0-1];
	   	  	    show /=2;
	   	  	  }
		  }
		  else
		  {
		  	  tp.l=p[1].l;
		  	  if(p[0].r>p[1].r) swap(p[0].r,p[1].r);
		  	  tp.r=p[0].r;
		  	  unsigned int d2=tp.l-p[0].l;
		  	  unsigned int d3=tp.r-tp.l+1;
		  	  unsigned int dd=d3; dd*=2;dd +=d2;
		  	  if(m1<=d2) show +=a[p[0].l+m1-1];
		  	  else if(m1<=dd) show +=a[tp.l+(m1-d2-1)/2];
		  	  else 
		  	  {
		  	  	  m1 -=dd;
		  	  	 show +=a[tp.r+m1];
			  }
			  if((d0+d1)%2==0)
			  {
			  	 m1=(d0+d1)/2;
			     if(m1<=d2) show +=a[p[0].l+m1-1];
		  	     else if(m1<=dd) show +=a[tp.l+(m1-d2-1)/2];
		  	     else 
		  	    {
		  	  	   ll dd=d3; dd*=2;dd +=d2;
		  	  	   m1 -=dd;
		  	  	   show +=a[tp.r+m1];
			    }
			     show /=2;
			  }
		  }
		  printf("%.1f\n",show);
	   }
	} 
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值