[codeforces1284D]New Year and Conference 离散化+multiset【或排序+线段树】

【题目】:题目链接

time limit per test

2 seconds

memory limit per test

1024 megabytes

input

standard input

output

standard output

Filled with optimism, Hyunuk will host a conference about how great this new year will be!

The conference will have nn lectures. Hyunuk has two candidate venues aa and bb. For each of the nn lectures, the speaker specified two time intervals [sai,eai][sai,eai] (sai≤eaisai≤eai) and [sbi,ebi][sbi,ebi] (sbi≤ebisbi≤ebi). If the conference is situated in venue aa, the lecture will be held from saisai to eaieai, and if the conference is situated in venue bb, the lecture will be held from sbisbi to ebiebi. Hyunuk will choose one of these venues and all lectures will be held at that venue.

Two lectures are said to overlap if they share any point in time in common. Formally, a lecture held in interval [x,y][x,y] overlaps with a lecture held in interval [u,v][u,v] if and only if max(x,u)≤min(y,v)max(x,u)≤min(y,v).

We say that a participant can attend a subset ss of the lectures if the lectures in ss do not pairwise overlap (i.e. no two lectures overlap). Note that the possibility of attending may depend on whether Hyunuk selected venue aa or venue bb to hold the conference.

A subset of lectures ss is said to be venue-sensitive if, for one of the venues, the participant can attend ss, but for the other venue, the participant cannot attend ss.

A venue-sensitive set is problematic for a participant who is interested in attending the lectures in ss because the participant cannot be sure whether the lecture times will overlap. Hyunuk will be happy if and only if there are no venue-sensitive sets. Determine whether Hyunuk will be happy.

Input

The first line contains an integer nn (1≤n≤1000001≤n≤100000), the number of lectures held in the conference.

Each of the next nn lines contains four integers saisai, eaieai, sbisbi, ebiebi (1≤sai,eai,sbi,ebi≤1091≤sai,eai,sbi,ebi≤109, sai≤eai,sbi≤ebisai≤eai,sbi≤ebi).

Output

Print "YES" if Hyunuk will be happy. Print "NO" otherwise.

You can print each letter in any case (upper or lower).

Examples

input

Copy

2
1 2 3 6
3 4 7 8

output

Copy

YES

input

Copy

3
1 3 2 4
4 5 6 7
3 4 5 5

output

Copy

NO

input

Copy

6
1 5 2 9
2 4 5 8
3 6 7 11
7 10 12 16
8 11 13 17
9 12 14 18

output

Copy

YES

Note

In second example, lecture set {1,3}{1,3} is venue-sensitive. Because participant can‘t attend this lectures in venue aa, but can attend in venue bb.

In first and third example, venue-sensitive set does not exist.

【题意】

有n个演讲,两个场地。每个演讲可以在 [??????,??????] (??????≤??????)  时间段在a场地举行,或[??????,??????] (??????≤??????)时间段在b场地举行。

在同一个场地,两个会议时间分别为[x,y],[u,v]  如果 max(??,??)≤min(??,??)两个会议就冲突。

求任意两个会议要么同时在ab两地冲突,要么都不冲突(即如果在a冲突则在b也要冲突)

满足输出YES,不满足输出NO

【题解】

参考 xht37‘s blog

方法1:离散化+multiset

扫描时间轴,检查每一个时间点,在a场地发生的事件,则在b场地也应该都发生。

再做一遍,检查每一个时间点,在b场地发生的事件,在a场地也应该发生。

扫描过程:

因为要扫描时间,所以先对时间离散化

把每个时间a场地发生、终止的事件记录下来。

从前往后扫描,对于发生的事,把b事件的起始时间加到从大到小的multiset,终止时间加到从小到大的multiset;当前时间点b中事件两两冲突的条件时maxend>=minstar (事件之间没有间隔);终止的事从集合中删掉

一旦maxend<minstar,则输出NO.

ac代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int const maxn=100005;
 4 int sa[maxn],sb[maxn],ea[maxn],eb[maxn],a[maxn<<2],tot,m,n;
 5 vector<int>st[maxn<<2],en[maxn<<2];
 6 multiset<int>rmin;
 7 multiset<int,greater<int>>lmax;//默认从小到大
 8 bool work(){
 9     for(int i=1;i<=m;i++)st[i].clear(),en[i].clear();
10     for(int i=1;i<=n;i++)st[sa[i]].push_back(i),en[ea[i]].push_back(i);
11     rmin.clear(),lmax.clear();
12     for(int i=1;i<=m;i++){
13         for(int z:st[i]){lmax.insert(sb[z]);rmin.insert(eb[z]);}
14         if(lmax.size()&&*lmax.begin()>*rmin.begin())return 1;//取元素的值用 *
15         for(int z:en[i])lmax.erase(lmax.find(sb[z])),rmin.erase(rmin.find(eb[z]));
16             //erase(pos) 会删掉这个位置的元素(只删1个);erase(element)删掉这个元素,返回删去元素个数(删掉了多个)
17     }
18     return 0;
19 }
20 inline int get_num(){
21     char ch=getchar();
22     int num=0;
23     while(ch<‘0‘||ch>‘9‘){ch=getchar();}
24     while(ch>=‘0‘&&ch<=‘9‘){num=(num<<1)+(num<<3)+ch-‘0‘;ch=getchar();}
25     return num;
26 }
27 int main(){
28     scanf("%d",&n);
29     for(int i=1;i<=n;i++){
30         sa[i]=get_num(),ea[i]=get_num(),sb[i]=get_num(),eb[i]=get_num();
31         a[++tot]=sa[i];a[++tot]=ea[i];a[++tot]=sb[i];a[++tot]=eb[i];
32     }
33     sort(a+1,a+1+tot);
34     m=unique(a+1,a+1+tot)-a-1;
35     for(int i=1;i<=n;i++){
36         sa[i]=lower_bound(a+1,a+1+m,sa[i])-a;
37         ea[i]=lower_bound(a+1,a+1+m,ea[i])-a;
38         sb[i]=lower_bound(a+1,a+1+m,sb[i])-a;
39         eb[i]=lower_bound(a+1,a+1+m,eb[i])-a;
40     }
41     bool ans=work();
42     swap(sa,sb);swap(ea,eb);
43     if(ans||work())printf("NO\n");
44     else printf("YES\n");
45     return 0;
46 }

方法2:

参考博客:https://www.cnblogs.com/zsben991126/p/12155688.html

线段树+排序

要比上面用stl的方法快些

原文地址:https://www.cnblogs.com/conver/p/12196154.html

时间: 2024-10-17 08:02:42

[codeforces1284D]New Year and Conference 离散化+multiset【或排序+线段树】的相关文章

【权值线段树】离散化介绍 (+利用 线段树 求逆序对)

先介绍一下离散化 桶排大家应该知道,就是开一个数组(下标为数值,记录了该数值的出现次数)然后遍历过去如果出现次数不为零,那就输出这些数字,理论时间复杂度可以达到O(N)但是由于内存限制,不能开很大的数组. 然而 如果某个数列中的数字不要求大小确定,只要求这些数字有相对的大小就够了的话,离散化就有了用武之地 举个例子:数列 3 8 7 5 2000000000000000 我们发现有几个数之间差距很大,但是我们用不到数值的大小,只要求相对大小,那怎么办呢? 观察下面的数列: 1 4 3 2 5 真

HDU 5700 优先队列(或者multiset) 或 线段树

题目大意:有n个区间,求k个区间,使得这k个区间相交的区间内数字之和最大.数列的数字均>=0 优先队列思路: 按照左端点sort,然后枚举左端点,假设他被覆盖过k次,然后用优先队列来维护最右端即可. //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespace std; #pragma comment(linker,"/STACK:102400000,102400000&qu

HDU5124:lines(线段树+离散化)或(离散化思想)

http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A. Input The first line conta

POJ 2528 Mayor&amp;#39;s posters 离散化+线段树

题目大意:给出一些海报和贴在墙上的区间.问这些海报依照顺序贴完之后,最后能后看到多少种海报. 思路:区间的范围太大,然而最多仅仅会有10000张海报,所以要离散化. 之后用线段树随便搞搞就能过. 关键是离散化的方法,这个题我时隔半年才A掉,之前一直就TTT,我还以为是线段树写挂了. 当我觉得我自己的水平这样的水线段树已经基本写不挂的时候又写了这个题,竟然还是T. 后来我对照别人的代码,才发现是我的离散化写渣了. 以下附AC代码(79ms),这个离散化写的比較优雅.时间也非常快,以后就这么写了.

线段树+区间离散化

校赛1007 题意 给你一个n(n<1e5),表示n个比赛直播,然后n个区间,l,r(0<=l,r<=1e9),表示比赛开始的时间和结束的时间,要同时把所有比赛看完,问最少要借多少台电脑(自己有一台电脑) 其实就是求区间重叠的最大值由于区间太大,所以离散化处理 思路:线段树区间更新 + 离散化 以区间的端点作为标记离散化,离散化后映射到线段树,将重复的端点去掉,然后重新排序,对每个区间二分确定映射后的区间,这里注意二分的时候左端点和右端点二分的判断条件是不同的 AC代码: #includ

POJ 2528 Mayor&#39;s posters 区间离散化线段树

点击打开链接 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45894   Accepted: 13290 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their elector

离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers

题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前花开的数量,放张图易理解: 还有一种做法用尺取法的思想,对暴力方法优化,对询问点排序后再扫描一遍,花开+1,花谢-1.详细看代码. 收获:一题收获很多:1. 降低复杂度可以用二分 2. 线段计数问题可以在端点标记1和-1 3. 离散化+线段树 终于会了:) (听说数据很水?) 代码1:离散化+线段树

POJ - 2528Mayor&#39;s posters (离散化+线段树区间覆盖)

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the

ZOJ--3612--Median【线段树+离散化】

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4736 题意:有最多10000次操作,在一个初始为空的数列中添加或移除元素并保持数列有序,每次操作后,如果数列个数为奇数就输出中间值,如果为偶数就输出中间两个值得平均值. 思路:刚开始写了一发multiset模拟,看吴琦TLE了估计他也是multiset写的,就放弃这个思路了,不过最后证明multiset写的机智一点这题也是能过的.线段树.树状数组都考虑了,最后还是用线段