AIM Tech Round 5 (rated, Div. 1 + Div. 2) C. Rectangles 【矩阵交集】
题目传传传送门:http://codeforces.com/contest/1028/problem/C
C. Rectangles
2 seconds
256 megabytes
standard input
standard output
You are given nn rectangles on a plane with coordinates of their bottom left and upper right points. Some (n−1)(n−1) of the given nn rectangles have some common point. A point belongs to a rectangle if this point is strictly inside the rectangle or belongs to its boundary.
Find any point with integer coordinates that belongs to at least (n−1)(n−1) given rectangles.
Input
The first line contains a single integer nn (2≤n≤1326742≤n≤132674) — the number of given rectangles.
Each the next nn lines contains four integers x1x1, y1y1, x2x2 and y2y2 (−109≤x1<x2≤109−109≤x1<x2≤109, −109≤y1<y2≤109−109≤y1<y2≤109) — the coordinates of the bottom left and upper right corners of a rectangle.
Output
Print two integers xx and yy — the coordinates of any point that belongs to at least (n−1)(n−1) given rectangles.
Examples
3
0 0 1 1
1 1 2 2
3 0 4 1
1 1
3
0 0 1 1
0 1 1 2
1 0 2 1
1 1
4
0 0 5 5
0 0 4 4
1 1 4 4
1 1 4 4
1 1
5
0 0 10 8
1 2 6 7
2 3 5 6
3 4 4 5
8 1 9 2
3 4
The picture below shows the rectangles in the first and second samples. The possible answers are highlighted.

The picture below shows the rectangles in the third and fourth samples.

题意概括:
N个矩阵,每个矩阵的表示方法是给左下角和右上角的坐标,求一个点至少在(N-1)个矩阵内部,求这个点的坐标(如果有多个输出其中一个就可以了)。
解题思路:
比赛时TLE的思路是二维树状数组标记,然后查询找被标记了至少(N-1)次的点。
TLE code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#define ll long long int;
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = ;
const int MAX = 1e9;
int x[MAXN], y[MAXN];
map<int,map<int, int> >mmp;
int N, T; int lowbit(int x)
{
return x&(-x);
} void add(int x, int y, int value)
{
for(int i = x; i <= MAX; i += lowbit(i))
for(int j = y; j <= MAX; j += lowbit(j))
mmp[i][j] += value;
} int sum(int x, int y)
{
int res = ;
for(int i = x; i > ; i -= lowbit(i))
for(int j = y; j > ; j -= lowbit(j))
res+=mmp[i][j];
return res;
} void init()
{
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++)
mmp[i][j] = ;
} int main()
{
int x1, y1, x2, y2;
scanf("%d", &N);
for(int i = ; i <= N; i++)
{
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
x1++, x2++, y1++, y2++;
x[i] = x1;
y[i] = y1;
add(x1, y1, );
add(x2+, y1, -);
add(x1, y2+, -);
add(x2+, y2+, -);
}
for(int i = ; i <= N; i++)
{
if(sum(x[i], y[i]) >= N-)
{
printf("%d %d", x[i]-, y[i]-);
break;
}
}
return ;
}
然而这道题其实是道YY题...求矩阵前缀交集和后缀交集,然后 O(n) 枚举每一个点不在交集中的情况(也就是该点前缀交后缀的情况),如果该点不在时存在合法交集,那么答案就出来了。
AC code:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long int
using namespace std;
const int MAXN = ;
typedef struct Date{
int x1, x2, y1, y2;
};
Date P[MAXN], st[MAXN], ed[MAXN];
int N; inline Date add(Date a, Date b)
{
Date res;
res.x1 = max(a.x1, b.x1);
res.y1 = max(a.y1, b.y1);
res.x2 = min(a.x2, b.x2);
res.y2 = min(a.y2, b.y2);
return res;
} int main()
{
scanf("%d", &N);
for(int i = ; i <= N; i++)
{
scanf("%d%d%d%d", &P[i].x1, &P[i].y1, &P[i].x2, &P[i].y2);
}
st[] = P[]; ed[N] = P[N];
for(int i = ; i <= N; i++) st[i] = add(st[i-], P[i]);
for(int i = N-; i >= ; i--) ed[i] = add(ed[i+], P[i]); for(int i = ; i <= N; i++){
Date cur;
if(i == ) cur = ed[];
else if(i == N) cur = st[N-];
else cur = add(st[i-], ed[i+]);
if(cur.x1 <= cur.x2 && cur.y1 <= cur.y2){
printf("%d %d\n", cur.x1, cur.y1);
break;
}
}
return ;
}
AIM Tech Round 5 (rated, Div. 1 + Div. 2) C. Rectangles 【矩阵交集】的更多相关文章
- AIM Tech Round 5 (rated, Div. 1 + Div. 2) (A, B, E)
B.Unnatural Conditions 题目链接 : http://codeforces.com/contest/1028/problem/B #include<iostream> ...
- AIM Tech Round 5 (rated, Div. 1 + Div. 2)
A. Find Square 找到对角线的两个点的坐标,这道题就迎刃而解了. inline void work(int n) { int m; cin >> m; memset(str, ...
- AIM Tech Round 5 (rated, Div. 1 + Div. 2) E(思维,构造)
#include<bits/stdc++.h>using namespace std;long long a[150007];long long ans[150007];int main( ...
- AIM Tech Round 5 (rated, Div. 1 + Div. 2) D(SET,思维)
#include<bits/stdc++.h>using namespace std;const long long mod = 1e9+7;char s[370007][27];long ...
- 【AIM Tech Round 5 (rated, Div. 1 + Div. 2) 总结】【题解往前或往后翻,不在这】
又是爆炸的一场 心态有点小崩.但问题不大.. 看A题,一直担心有多个正方形..小心翼翼地看完之后,毅然地交上去了. [00:08] A[Accpted] 然后开始看B题. 觉得和之前做的某题很像,但翻 ...
- 【AIM Tech Round 5 (rated, Div. 1 + Div. 2) A】 Find Square
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找到左上角.往下一直走,往右一直走走到B边界就好. 中点的话.直接输出中位数 [代码] #include <bits/stdc ...
- 【AIM Tech Round 5 (rated, Div. 1 + Div. 2) B】Unnatural Conditions
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 让a+b的和为100000000...0这样的形式就好了 这样s(a+b)=1<=m就肯定成立了(m>=1) 然后至于s ...
- 【 AIM Tech Round 5 (rated, Div. 1 + Div. 2) C】Rectangles
[链接] 我是链接,点我呀:) [题意] 给你n个矩形. 让你找出一个点(x,y) 使得这个点在其中至少(n-1)个矩形中. [题解] 若干个矩形交在一起的话. 它们所有的公共区域也会是一个矩形. 这 ...
- Codeforces AIM Tech Round 5 (rated, Div. 1 + Div. 2)
A. Find Square time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ...
随机推荐
- mac下安装ionic
我的mac系统是EI Capitan,如下图所示. ionic是一个跨平台的框架,能够提供高效hybrid app的开发,而且性能接近于原生态,具体请参考:http://ionicframework. ...
- centos7 中文乱码问题解决方法
1.查看是否安装中文包 可以使用下面的命名查看系统是否安装了中文安装包. locale -a |grep "zh_CN" 没有输出,说明没有安装,输入下面的命令安装: yum gr ...
- 修改ThinkPHP的验证码类
今天用ThinkPHP重新开发一个系统,用到了ThinkPHP的验证码类,由于我希望验证码别太复杂,希望验证码里边只有数字,却发现该Verify类并未提供设置验证码中使用的字符的配置的方法,于是查看源 ...
- [转]<加密算法c#>——— 3DES加密之ECB模式 和 CBC模式
本文转自:http://www.cnblogs.com/qq278360339/archive/2013/06/05/3119222.html 最近 一个项目.net 要调用JAVA的WEB SERV ...
- mvc中在Action里调用另一个Action
今天做东西时发现一个新东西.即在一个Action调用另一Action.前提是同一个控制器.(没在一个控制里的没试过) 调用方法: public ActionResult Test1(){ //to ...
- hibernate的查询 (比较get 与load)
hibernate的查询的比较hibernate的查询有很多,Query,find,Criteria,get,load query使用hsql语句,可以设置参数是常用的一种方式 criteria的方式 ...
- iOS开发之GCD基础
重新回顾.学习GCD.Block.先贴出一篇不错的讲解GCD基础使用的文章 原文地址:http://blog.csdn.net/aolan1108/article/details/17283415 做 ...
- Csharp: Send Email
/// <summary> /// 發送郵件 /// 塗聚文 /// 20130816 /// </summary> /// <param name="to&q ...
- BZOJ5249: [2018多省省队联测]IIIDX(线段树 贪心)
题意 题目链接 Sol 不难发现题目给出的是一个树,其中\(\frac{i}{K}\)是\(i\)的父亲节点 首先,当\(d_i\)互不相同时,一个显然的贪心策略就是优先给编号小的分配较大的权值.可以 ...
- java参数传递之值传递
一 概述 1.什么是参数传递? 调用方法时向形参传递数据的过程叫做参数传递.在编程语言中有两种传递方式:值传递与引用传递.必须强调的是,这里提到的两种传递方式不是仅限于java使用到的传递方式,而是出 ...