Ping-Pong (Easy Version)(DFS)
2 seconds
256 megabytes
standard input
standard output
In this problem at each moment you have a set of intervals. You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2.
Your program should handle the queries of the following two types:
- "1 x y" (x < y) — add the new interval (x, y) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals.
- "2 a b" (a ≠ b) — answer the question: is there a path from a-th (one-based) added interval to b-th (one-based) added interval?
Answer all the queries. Note, that initially you have an empty set of intervals.
The first line of the input contains integer n denoting the number of queries, (1 ≤ n ≤ 100). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 109 by their absolute value.
It's guaranteed that all queries are correct.
For each query of the second type print "YES" or "NO" on a separate line depending on the answer.
近来很颓。。。
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <map>
- #include <vector>
- #include <cstdio>
- #include <cmath>
- #include <cstring>
- using namespace std;
- const int MAXN = ;
- int a[MAXN], b[MAXN];
- bool f[MAXN];
- int n, cnt;
- void dfs(int s, int e)
- {
- f[s] = ;
- if(f[e]) return ;
- for(int i = ; i < cnt; i++)
- {
- if(f[i]) continue;
- if(a[s] > a[i] && a[s] < b[i]) dfs(i, e);
- if(f[e]) return ;
- if(b[s] > a[i] && b[s] < b[i]) dfs(i, e);
- if(f[e]) return ;
- }
- }
- int main()
- {
- while(scanf("%d", &n) != EOF)
- {
- cnt = ;
- int m, x, y;
- while(n--)
- {
- scanf("%d", &m);
- if(m == )
- {
- scanf("%d %d", &a[cnt], &b[cnt]);
- cnt++;
- }
- else
- {
- scanf("%d %d", &x, &y);
- memset(f, , sizeof(f));
- dfs(x, y);
- if(f[y]) puts("YES");
- else puts("NO");
- }
- }
- }
- return ;
- }
Ping-Pong (Easy Version)(DFS)的更多相关文章
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
- Codeforces 1077F1 Pictures with Kittens (easy version)(DP)
题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...
- E1. String Coloring (easy version)(贪心)
E1. String Coloring (easy version) time limit per test 1 second memory limit per test 256 megabytes ...
- [ACM] POJ 3740 Easy Finding (DFS)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16202 Accepted: 4349 Description Give ...
- Codeforces Global Round 7 D1. Prefix-Suffix Palindrome (Easy version)(字符串)
题意: 取一字符串不相交的前缀和后缀(可为空)构成最长回文串. 思路: 先从两边取对称的前后缀,之后再取余下字符串较长的回文前缀或后缀. #include <bits/stdc++.h> ...
- E1. Array and Segments (Easy version)(暴力) && E2. Array and Segments (Hard version)(线段树维护)
题目链接: E1:http://codeforces.com/contest/1108/problem/E1 E2:http://codeforces.com/contest/1108/problem ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
随机推荐
- 利用nodejs模块缓存机制创建“全局变量”
在<深入浅出nodejs>有这样一段(有部分增减): 1.nodejs引入模块分四个步骤 路径分析 文件定位 编译执行 加入内存 2.核心模块部分在node源代码的编译过程中就编译成了二级 ...
- How to install ZeroMQ on Ubuntu14.04
Prepare: sudo apt-get install libtool autoconf automake uuid-dev sudo apt-get install python-dev sud ...
- ecshop2.72文件结构说明
ECShop 2.7.2 的结构图及各文件相应功能介绍 ECShop 2.7.2 upload 的目录┣ activity.php 活动列表┣ affiche.php 广告处理文件┣ affiliat ...
- shell 时间统计脚本
#!/bin/sh #Today=`date +%Y%m%d` YEAR=`echo $|cut -c -` MONTH=`echo $|cut -c -` DAY=`echo $|cut -c -` ...
- C#程序中注释过多的8条理由
程序中中的注释,一般是有益处的,可以知晓程序的一些逻辑说明,或是参数解释.但是有些程序,因为注释太多,反而引起维护上的不方便,删掉了怕以后不能出现问题不好查找原因,不删除留在代码中,对程序的维护人员, ...
- Windows环境下面搭建Object C开发环境[转]
1.安装编译器 Objective-C的编译器有很多,其中LLVM属于从GCC发展出来的,主要使用在苹果的平台中,GNU可以使用GnuStep,网址是http://wwwmain.gnustep.or ...
- LDO/DC-DC区别总结(转)
电源是一个电子系统中不可缺少的非常重要的一部分.但是外接的电源通常不能够完全提供系统中需要的所有的电源种类.因此带来了电源电压的变换问题.常用的电源电压的变换芯片包括LDO和DC-DC两种.下面对这两 ...
- mac os 中安装memcahed
1.先安装macport sudo port selfupdate #更新当前Marport (如果port 不可以时可以考虑此操作) sudo prot -d selfupdate #替换更 ...
- 修改oracle密码有效期限制
racle11g,静默安装后用户的密码有效期默认设置为180天,180天后密码将失效,oracle会提示要修改密码. 我们项目用的是jdbc连接oracle数据库,没法自动处理oracle的这种密 ...
- 在gradle 中使用ant 执行 “命令行”(CMD)不出日志解决方案
因为gradle 好恶心,声明的任务,一定会事先运行一次,而任务追加的话就不会 例如: task hello(){ println "HelloWorld" } task hell ...