Problem Solution

1 solutions

  • 0
    @ 2025-04-08 10:22:48

    Author : Kamonasish Roy
    Editorial :
    Key : Just use scan line method and sorting segment by their ending points instead of starting points.

    Code (C++) :

    // Author : Kamonasish Roy (Bullet)
    // Time : 2025-03-24 04:20:12
    #include<bits/stdc++.h>
    using namespace std;
    const long long M=1e5+1,MOD=1e9+7;
    typedef long long ll;
    
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int t=1;
        cin>>t;
        while(t--){
        	int n;
        	cin>>n;
        	vector<pair<int,int>>vec;
        	vector<int>id;
        	for(int i=1;i<=n;i++){
        		int l,r;
        		cin>>l>>r;
        		vec.push_back({r,l});
        		id.push_back(l);
        	}
        	
        	sort(vec.begin(),vec.end());
        	sort(id.begin(),id.end());
        	int cur=0;
        	set<int>st;
        	vector<int>res;
        	for(int i=0;i<n;i++){
        		if(id[i]>cur){
        			cur=id[i];
        		}
        		else cur++;
        		st.insert(cur);
        		res.push_back(cur);
        	}
        	int x=1;
        	for(int i=0;i<n;i++){
        		int cur_r=vec[i].first;
        		int cur_l=vec[i].second;
        		auto it=st.lower_bound(cur_l);
        		if(it==st.end()){
        			x=0;
        			break;
        		}
        		if(*it>cur_r || *it<cur_l){
        			x=0;
        			break;
        		}
        		st.erase(it);
        	}
        	
        	if(!x){
        		cout<<"NO\n";
        		continue;
        	}
        	else{
        		cout<<"YES\n";
        		for(auto i:res)cout<<i<<" ";
        		cout<<"\n";
        	}
        	
        	
        	
        	}
        	
        	   
        return 0;
     
    }
    
  • 1

Information

ID
1185
Difficulty
8
Category
Data_Structure | Math Click to Show
Tags
# Submissions
28
Accepted
6
Accepted Ratio
21%
Uploaded By