codeforces 1284D. New Year and Conference(线段树)
题意:有n场讲座,有两个场地a和b,如果在a场地开讲座则需要占用[sai,eai],在b场地开讲座则需要占用[sbi,ebi]这个时间段,假如开两场讲座,如果在a场地开不冲突,而b场地开冲突,则称其为敏感的,同理a和b反过来也是一样的,如果ab两场地都冲突则也不是敏感的,先求给定的n场讲座,任意的两两开设是否敏感。
思路:暴力枚举是O(n^2)复杂度,必定超时,这里可以用线段树或ST表做,达到一个nlogn的复杂度。对于任意一个讲座x,找出所有与x讲座在a场地冲突的讲座,再判断其是否在b场地冲突,如果不是则直接输出“NO”
首先对所有讲座的sa和ea进行升序,例如讲座 i 的时间片是[x,y],在其时间片上与 i 在a场地冲突的讲座用二分的方法可以枚举出来一个离散化后的区间,然后我们用线段树维护b场地的sb最大值和eb最小值,每次查询出这个区间的
的sb最大值和eb最小值,此时如果说sb最大值 > y 或者 eb最小值 < x,那么这些表演中必定存在b场地与表演i不冲突的情况,此时直接输出“NO”,把所有的讲座都check一遍,交换ab次序再check即可,上述只检验了在a场地冲突一定在b场地冲突的情况,但并未考虑在b场地冲突而在a场地不冲突的情况。
总体时间复杂度nlogn。
AC代码:
#include<iostream>
#include<vector>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
struct node{
int sa,ea,sb,eb;
node(){};
node(int a,int b,int c,int d){
sa = a,ea = b,sb = c,eb = d;
}
bool operator<(node cur)const{
if(sa == cur.sa ) return ea<cur.ea ;
return sa < cur.sa ;
}
}point[maxn];
int segt_max[*maxn],segt_min[*maxn];
void build(int l,int r,int k){//建两个线段树维护区间最大最小值
if(l == r) {
segt_max[k] = point[l].sb;
segt_min[k] = point[l].eb;
return ;
}
int mid = (l+r)/;
build(l,mid,*k);
build(mid+,r,*k+);
segt_max[k] = max(segt_max[*k],segt_max[*k+]);
segt_min[k] = min(segt_min[*k],segt_min[*k+]);
}
int queryMin(int l,int r,int al,int ar,int k){//查询区间最小值
if(l >= al && r <= ar) return segt_min[k];
int mid = (l+r)/;
if(ar<=mid) return queryMin(l,mid,al,ar,*k);
else if(al>mid){
return queryMin(mid+,r,al,ar,*k+);
}
else {
return min(queryMin(l,mid,al,mid,*k),queryMin(mid+,r,mid+,ar,*k+));
}
}
int queryMax(int l,int r,int al,int ar,int k){//查询区间最大值
if(l >= al && r <= ar) return segt_max[k];
int mid = (l+r)/;
if(ar<=mid) return queryMax(l,mid,al,ar,*k);
else if(al>mid){
return queryMax(mid+,r,al,ar,*k+);
}
else {
return max(queryMax(l,mid,al,mid,*k),queryMax(mid+,r,mid+,ar,*k+));
}
}
bool check(int n){
sort(point+,point++n);
build(,n,);
for(int i = ;i<=n;i++){
int pos = lower_bound(point+,point++n,node(point[i].ea ,1e9+,,))-point-;
//查找与第i场演讲冲突的集合
if(i+>pos) continue;//无冲突直接跳过
//check一下另一个场地的所有表演是否都冲突
if(queryMin(,n,i+,pos,)<point[i].sb || queryMax(,n,i+,pos,)>point[i].eb){
return false;
}
}
return true;
}
int main()
{
int n;
cin>>n;
int f = ;
for(int i = ;i<=n;i++){
cin>>point[i].sa>>point[i].ea>>point[i].sb>>point[i].eb;
}
if(check(n)) f++;
for(int i = ;i<=n;i++){
swap(point[i].sa,point[i].sb);
swap(point[i].ea,point[i].eb);
}
if(check(n)) f++;
if(f == ) cout<<"YES";
else cout<<"NO";
return ;
}
codeforces 1284D. New Year and Conference(线段树)的更多相关文章
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- Codeforces 444 C. DZY Loves Colors (线段树+剪枝)
题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...
- Codeforces Gym 100513F F. Ilya Muromets 线段树
F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...
- Codeforces 834D The Bakery【dp+线段树维护+lazy】
D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...
- codeforces 1017C - Cloud Computing 权值线段树 差分 贪心
https://codeforces.com/problemset/problem/1070/C 题意: 有很多活动,每个活动可以在天数为$[l,r]$时,提供$C$个价格为$P$的商品 现在从第一天 ...
- Codeforces 1045. A. Last chance(网络流 + 线段树优化建边)
题意 给你 \(n\) 个武器,\(m\) 个敌人,问你最多消灭多少个敌人,并输出方案. 总共有三种武器. SQL 火箭 - 能消灭给你集合中的一个敌人 \(\sum |S| \le 100000\) ...
- Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]
洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...
随机推荐
- Linux下搭建asp.net运行环境
最近有个项目,是在Windows平台下开发的,需要把 asp.net web应用移植到 CentOS下,甚是头疼: 翻阅资料,发现Jexus是个可行的方案,下面是官方对Jexus的定义: 什么是Jex ...
- Uva1635 二项式递推+质因子分解+整数因子分解
题意: 给定n个数a1,a2····an,依次求出相邻两个数值和,将得到一个新数列,重复上述操作,最后结果将变为一个数,问这个数除以m的余数与那些数无关? 例如n=3,m=2时,第一次得到a1+a2, ...
- 复原IP地址
这道题有点不好理解 export default (str) => { // 保存所有符合条件的IP地址 let r = [] // 分四步递归处理ip分段 let search = (cur, ...
- Spark学习之路 (四)Spark的广播变量和累加器[转]
概述 在spark程序中,当一个传递给Spark操作(例如map和reduce)的函数在远程节点上面运行时,Spark操作实际上操作的是这个函数所用变量的一个独立副本.这些变量会被复制到每台机器上,并 ...
- nginx反向代理(1)
目录 反向代理 概述 nginx代理 proxy_pass 加与不加/ ================================================================ ...
- 安卓android eclipse运行提示no compatible targets were found
在eclipse中开发安卓应用,运行项目时,右击项目名称---Run As---Android Application时, 系统提示"No compatible targets were f ...
- HTML与W3C
HTML:超文本标记语言 超文本包括:文字.图片.音频.视频.动画等 流程:写好HTML代码后通过浏览器(自动编译HTML代码)展现出效果 HTML优点: 世界知名浏览器厂商对HTML5的支持 微软 ...
- SpringBoot2.x打包成war(看这篇就够了)
springboot默认打包成jar,如果想打包成war,则需要做以下三步. 1.修改pom.xml文件 a.将jar改成war <groupId>com.test</groupId ...
- [转]TCP/IP 协议基础(一)
参考书籍为<图解tcp/ip>-第五版.这篇随笔,主要内容还是TCP/IP所必备的基础知识,包括计算机与网络发展的历史及标准化过程(简述).OSI参考模型.网络概念的本质.网络构建的设备等 ...
- ssh配置跳板机-带密钥
ssh配置跳板机堡垒机带密钥 ~/.ssh/config 添加以下配置: # 跳板机地址 Host jumper HostName jumper.com User jumper port 23333 ...