zThere are n rectangle radar scanners on the ground. The sides of them are all paralleled to the axes. The i-th scanner's bottom left corner is square (ai,bi) and its top right corner is square (ci,di)

. Each scanner covers some squares on the ground.

You can move these scanners for many times. In each step, you can choose a scanner and move it one square to the left, right, upward or downward.

Today, the radar system is facing a critical low-power problem. You need to move these scanners such that there exists a square covered by all scanners.

Your task is to minimize the number of move operations to achieve the goal.

Input

The first line of the input contains an integer T(1≤T≤1000)

, denoting the number of test cases.

In each test case, there is one integer n(1≤n≤100000)

in the first line, denoting the number of radar scanners.

For the next n

lines, each line contains four integers ai,bi,ci,di(1≤ai,bi,ci,di≤109,ai≤ci,bi≤di)

, denoting each radar scanner.

It is guaranteed that ∑n≤106

.

Output

For each test case, print a single line containing an integer, denoting the minimum number of steps.

Example

Input
1
2
2 2 3 3
4 4 5 5
Output
2

题解:由于横纵方向地位相同,我们不妨来看横方向,题目要找一点x使得n条线段经过平移最少次数,至少重合一点。
假设那一点就为x,那么一条线段至少与x有交点的话,所需距离为:d=(|l-x|+|r-x|-|r-l|)/2,纸上画一遍即可。我们要找的是所有线段移动的距离之和最小,那么只需Σd最小,
由于d中的|r-l|为常数,所以我们只需要求Σ(|l-x|+|r-x|)最小,那么x就是所有l,r的中位数了~~。题目难得就是转化~~
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<stdio.h>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int maxn=;
struct node
{
ll l,r;
}q[maxn],w[maxn];
ll n,a[maxn*];
ll ok(node q[])
{
int top=;
for(int i=;i<=n;i++){
a[++top]=q[i].l;
a[++top]=q[i].r;
}
sort(a+,a++top);
ll x=(a[top/]+a[top/+])/;
ll ans=;
for(int i=;i<=n;i++){
ans+=(abs(q[i].l-x)+abs(q[i].r-x)-(q[i].r-q[i].l))/;
}
return ans;
}
int main()
{
ios::sync_with_stdio();
int T;
cin>>T;
while(T--){
cin>>n;
for(int i=;i<=n;i++){
cin>>q[i].l>>w[i].l>>q[i].r>>w[i].r;
}
ll ans=;
ans+=ok(q);
ans+=ok(w);
cout<<ans<<endl;
}
return ;
}

G - Radar Scanner Gym - 102220G(中位数~~)的更多相关文章

  1. Radar Scanner Gym - 102220G

    题目链接:https://vjudge.net/problem/Gym-102220G 题意:在水平直角坐标系中有n个矩形,你可以将矩形沿着平行于X轴和Y轴水平移动,问至少经过几次移动可以使得所有的矩 ...

  2. G - WiFi Password Gym - 101608G (异或思维题+曲尺)

    题目链接:https://cn.vjudge.net/contest/285962#problem/G 题目大意:给你n和m,n代表有n个数,然后让你找出一个最长的区间,使得这个区间内的所有的数的‘’ ...

  3. G - Green-Red Tree Gym - 102190G

    题目链接:http://codeforces.com/gym/102190/attachments 题解:我们先将前5个点分别涂上红色或者绿色,使得这两棵树在5个点中都是连通,并不存在自环(建边方式不 ...

  4. The 13th Chinese Northeast Collegiate Programming Contest(B C E F H J)

    B. Balanced Diet 思路:把每一块选C个产生的价值记录下来,然后从小到大枚举C. #include<bits/stdc++.h> using namespace std; ; ...

  5. The 13th Chinese Northeast Collegiate Programming Contest

    题解: solution Code: A. Apple Business #include<cstdio> #include<algorithm> #include<ve ...

  6. [Swift]Scanner字符串扫描类

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. 2018 Multi-University Training Contest 3 - HDU Contest

    题解: solution Code: A. Ascending Rating #include<cstdio> const int N=10000010; int T,n,m,k,P,Q, ...

  8. 2018 Multi-University Training Contest 3 Solution

    A - Problem A. Ascending Rating 题意:给出n个数,给出区间长度m.对于每个区间,初始值的max为0,cnt为0.遇到一个a[i] > ans, 更新ans并且cn ...

  9. 最小生成树(Kruskal算法-边集数组)

    以此图为例: package com.datastruct; import java.util.Scanner; public class TestKruskal { private static c ...

随机推荐

  1. JavaEE--JNDI(上,简介)

    参考:https://blog.csdn.net/yan372397390/article/details/50450332 https://www.landui.com/help/show-6158 ...

  2. Arduino学习——u8glib库资料整理

    第一部分,u8glib标准语法格式: 本文使用的是DFRobot出品的LCD12864 Shield V1.0 端口占用情况: SPI Com: SCK = 13, MOSI = 11, CS = 1 ...

  3. JavaScript 之 数据在内存中的存储和引用

    栈和堆 大家都知道,JS中的数据类型包括两种:简单数据类型(String.Number.Boolean.undefined.null)和复杂数据类型(object). 在内存中分为栈区(stack)和 ...

  4. 吴裕雄--天生自然MySQL学习笔记:MySQL 正则表达式

    下表中的正则模式可应用于 REGEXP 操作符中. 实例 查找name字段中以'st'为开头的所有数据: mysql> SELECT name FROM person_tbl WHERE nam ...

  5. SQL基础教程(第2版)第2章 查询基础:2-2 算数运算符和比较运算符&2-3 逻辑运算符

    ● 包含NULL的运算,其结果也是NULL. ● 判断是否为NULL,需要使用IS NULL或者IS NOT NULL运算符. ■算术运算符 ■需要注意NULL ■比较运算符 这些比较运算符可以对字符 ...

  6. B - Given Length and Sum of Digits... CodeForces - 489C (贪心)

    You have a positive integer m and a non-negative integer s. Your task is to find the smallest and th ...

  7. 寒假day16

    今天优化了管理员界面,人才标签模块遇到了一点问题,部分结果无法显示,正在寻找原因

  8. 安装lombok插件IDEA的插件栏加载不出来

    打开 Setting-->Appearance & Behavior -->Syetem Setting -->Updates,将Use secure connection  ...

  9. UIWindow statusBar消失

    1.新建UIWindow 程序崩溃 报无根控制器错误 Xcode7环境下,新建UIWindow需添加rootViewController 2.新建UIWindow后 statusBar消失 Info. ...

  10. Oracle数据库中表的imp&exp

    在Oracle数据库中可以使用imp和exp命令来执行数据的导入导出(包括表结构和数据),使用imp和exp命令执行导入导出操作必需的是需要安装Oracle数据库,系统安装Oracle数据库,可以识别 ...