原文链接:http://codeforces.com/problemset/problem/320/B
思路:
这个题告诉你不断的告诉你一些区间,然后如果两个区间(a,b)(c,d)满足 c<a<d or c<b<d.那么这两个区间就是连通的,如果区间1和2 连通,区间2和3连通,那么1和3也是连通的。
是一道常规的搜索算法,就是题意不太好懂,这里运用DFS解
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>p;
bool vis[110];
p pp[110];
int counter=1;
void DFS(int x,int y)//x:开始区间,y终点区间
{
if(vis[x]||vis[y])//如果vis[x]为1表示走遍了所有x的连通区间,又回到了x这里,所以结束操作;vis[y]为1表示走遍x的所有连通区间,到达了终点y,结束搜索。
return;
vis[x]=true;
for(int i=0;i<counter;i++)
{
if(vis[i])
continue;
if((pp[x].first<pp[i].second&&pp[x].first>pp[i].first)||(pp[x].second<pp[i].second&&pp[x].second>pp[i].first))//连通性判断.找x的连通区间
{
DFS(i,y);
}
}
}
int main()
{
int n;
cin>>n;
int type,a,b;
while(n--)
{
scanf("%d%d%d",&type,&a,&b);
if(type==1)
{
pp[counter].first=a;
pp[counter++].second=b;
}
else if(type==2)
{
memset(vis,0,sizeof(vis));
DFS(a,b);
if(vis[b])
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}