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.ios
Your program should handle the queries of the following two types:ide
Answer all the queries. Note, that initially you have an empty set of intervals.this
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.spa
It's guaranteed that all queries are correct.code
For each query of the second type print "YES" or "NO" on a separate line depending on the answer.blog
近來很頹。。。ci
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <map> 5 #include <vector> 6 #include <cstdio> 7 #include <cmath> 8 #include <cstring> 9 using namespace std; 10 11 const int MAXN = 102; 12 int a[MAXN], b[MAXN]; 13 bool f[MAXN]; 14 int n, cnt; 15 16 void dfs(int s, int e) 17 { 18 f[s] = 1; 19 if(f[e]) return ; 20 for(int i = 1; i < cnt; i++) 21 { 22 if(f[i]) continue; 23 if(a[s] > a[i] && a[s] < b[i]) dfs(i, e); 24 if(f[e]) return ; 25 if(b[s] > a[i] && b[s] < b[i]) dfs(i, e); 26 if(f[e]) return ; 27 } 28 } 29 30 int main() 31 { 32 while(scanf("%d", &n) != EOF) 33 { 34 cnt = 1; 35 int m, x, y; 36 while(n--) 37 { 38 scanf("%d", &m); 39 if(m == 1) 40 { 41 scanf("%d %d", &a[cnt], &b[cnt]); 42 cnt++; 43 } 44 else 45 { 46 scanf("%d %d", &x, &y); 47 memset(f, 0, sizeof(f)); 48 dfs(x, y); 49 if(f[y]) puts("YES"); 50 else puts("NO"); 51 } 52 } 53 } 54 return 0; 55 }