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 ;
}

最新文章

  1. mina通信 demo
  2. python常用文件处理函数_1
  3. Cocoapods的使用教程
  4. NYOJ题目596谁是最好的Coder
  5. linux系统非ROOT用户80端口不能启动tomcat问题的变通办法——通过Iptables端口转发
  6. JS动画理论
  7. 利用CSS实现居中对齐
  8. Dining(最大流)
  9. UVALive 7070 The E-pang Palace(暴力)
  10. 201621123031 《Java程序设计》第12周学习总结
  11. 设备唯一标识方法(Unique Identifier):如何在Windows系统上获取设备的唯一标识 zz
  12. laravel+Redis简单实现队列通过压力测试的高并发处理
  13. 通过RequestContextHolder直接获取HttpServletRequest对象
  14. 【源码解读】EOS测试插件:txn_test_gen_plugin.cpp
  15. 喵哈哈村的魔法考试 Round #21 (Div.2) 题解
  16. Linux下ip地址查询
  17. HDU1029(KB12-B)
  18. 浅谈iOS 自动调节文本高度
  19. 升级项目到Vs2010,编译时出现:MSB6006: “LC.exe”已退出,解决方法
  20. Splash images_enabled 属性

热门文章

  1. Mac Eclipse 打包可执行jar文件
  2. Codeforces 1299A/1300C - Anu Has a Function
  3. ubuntu下面嘚一些常用基本命令
  4. 日志处理中一些shell命令技巧
  5. Java8集合框架——HashMap源码分析
  6. Python的 5 种高级用法,效率提升没毛病!
  7. java8 String intern()
  8. SQL基础教程(第2版)第2章 查询基础:2-2 算数运算符和比较运算符&amp;2-3 逻辑运算符
  9. “杀死”纸质名片!HiHello能重构商业关系网吗?
  10. UOJ #2 【NOI2014】起床困难综合症