B. Ping-Pong (Easy Version)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
For each query of the second type print "YES" or "NO" on a separate line depending on the answer.
Sample test(s)
input
5 1 1 5 1 5 11 2 1 2 1 2 9 2 1 2
output
NO YES
Though the language of the question might sound uneasy. It asks simply if we can go from interval (a,b) to (c,d). The condition of travelling from (a,b) to (c,d) is if c<a<d or c<b<d.
Remeber if we can travel from (a,b) to (c,d) that doesnot necessarily mean we can travel from (c,d) to (a,b) . An example of such a case is if(a,b) entirely lies in the interval (c,d).
Now the question is divided into two parts
(1) adding an interval
Here eaceh interval is represented as a node. Like (1,5) is node 1 , (5,11) is node 2.
We push (a,b) to vector and check throughout the vector if (a,b) lies between any interval already present in the set of intervals. If present we add a directed edge between interval (a,b) to (c,d). We also check if we can travel from (c,d) to (a,b) [if((intervals[i].ff>a && intervals[i].ff<b )||(intervals[i].ss>a && intervals[i].ss<b))] . If we can also travel from (c,d) to (a,b) then we add a directed edge between (c,d) to (a,b).
(2) Second part is the query part where we do a simple DFS/BFS traversal from the given interval(represented as node) to the second interval . If reachable we print YES else we print NO.
The code is as follows
*** code begins here**
#include<iostream>
#include<stdio.h>
#include<vector>
#include<list>
#include<queue>
#include<set>
#include<stack>
#define ff first
#define ss second
using namespace std;
int visited[1000];
vector<pair<int ,int > >intervals;
list<int> arr[1000];
int top=0;
void add(int a , int b)
{
intervals.push_back(make_pair(a,b));
int l=intervals.size();
for(int i=0;i<l;i++)
{
if((a>intervals[i].ff && a<intervals[i].ss) || (b>intervals[i].ff && b<intervals[i].ss))
{
arr[l].push_back(i+1);
//cout<<"pushing "<<i+1 <<" to "<<l<<endl;
}
if((intervals[i].ff>a && intervals[i].ff<b )||(intervals[i].ss>a && intervals[i].ss<b))
{
arr[i+1].push_back(l);
//cout<<"pushing "<<l <<" to "<<i+1<<endl;
}
}
}
void query(int a , int b)
{
for(int i=1;i<=100+5;i++)
{
visited[i]=0;
}
stack<long long int> s;
s.push(a);
visited[a]=1;
while(!s.empty())
{
int node=s.top();
if(node==b)
{
cout<<"YES"<<endl;
return ;
}
s.pop();
list<int>::iterator it;
for(it=arr[node].begin();it!=arr[node].end();it++)
{
if(visited[*it]==0)
{
s.push(*it);
visited[*it]=1;
}
}
// step++;
} cout<<"NO"<<endl;
}
int main()
{
int n;
cin>>n;
int a ,b,c;
for(int i=1;i<=n;i++)
{
cin>>a>>b>>c;
if(a==1)
{
add(b,c);
}
else
{
query(b,c);
}
}
}
*** code ends her**
No comments:
Post a Comment