Friday, 15 May 2015

output
NO
YES

--------------------------------------------------------editorial---------------------------------------------------------------------------------------------
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> using namespace std; #include<bits/stdc++.h> vector<pair<long long int ,long long int > >v; list<long long int >li[10001]; int main() { long long int q; cin>>q; long long int ad=-1; while(q--) { long long int op,a,b; cin>>op>>a>>b; if(op==1) { ad++; v.push_back(make_pair(a,b)); //cout<<"ad "<<ad<<endl; for(long long int i=0;i<=ad-1;i++) { if((a>v[i].first && a<v[i].second) || (b>v[i].first && b<v[i].second)) { //cout<<"pairing "<<i<<" "<<ad<<endl; li[ad].push_back(i); } if( (v[i].first>a && v[i].first<b) || (v[i].second>a && v[i].second<b) ) { li[i].push_back(ad); } } } else { long long int visited[ad+10]; for(long long int i=0;i<ad+10;i++) visited[i]=0; long long int start=a-1; long long int end=b-1; stack<long long int > s; s.push(a-1); visited[a-1]=1; long long int f=0; while(!s.empty()) { long long int start=s.top(); s.pop(); list<long long int > :: iterator it; for(it=li[start].begin();it!=li[start].end();it++) { //cout<<"*it"<<*it<<endl; if(visited[*it]==0) { // cout<<"visiting "<<*it<<endl; visited[*it]=1; if(*it==end) { cout<<"YES"<<endl; f=1; break; } else { // cout<<"push"<<*it<<endl; s.push(*it); } } } if(f==1) break; } if(f==0) cout<<"NO"<<endl; } } return 0; }

No comments:

Post a Comment