1101C - Division and Union - CodeForces Solution


sortings *1500

Please click on ads to support us..

C++ Code:

//      بسم الله الرحمن الرحيم
//      All praise is due to ALLAH alone
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define go continue
#define exit return 0
#define ll long long int
#define ull unsigned long long int
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define print_vec(vec) for(int i=0;i<vec.size();i++)cout<<vec[i]<<' ';
#define print_set(st) for(auto it=st.begin();it!=st.end();it++)cout<<*it<<' ';
#define print_pair(vec) for(int i=0;i<vec.size();i++)cout<<vec[i].first<<' '<<vec[i].second<<endl;
#define dbg(num) cerr<< "Line "<<__LINE__ <<": "<< #num <<" = "<<(num)<<endl
//int dx[]={-2, -2, -1, -1,  1,  1,  2,  2};
//int dy[]={-1,  1, -2,  2, -2,  2, -1,  1};
int dx[] = { -1, 0, +1, 0};
int dy[] = {0, +1, 0, -1};
#define mod 1000000007
#define sz 1000000
struct query {
	int l;
	int r;
	int id;
};
query qr[sz + 1];
bool cmp(query a, query b)
{
	if (a.l == b.l)
		return a.r > b.r;
	else return a.l < b.l;
}
int main()
{
	fast;
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			int l1, r1;
			cin >> l1 >> r1;
			qr[i].l = l1;
			qr[i].r = r1;
			qr[i].id = i;
		}
		sort(qr + 1, qr + 1 + n, cmp);
		vector<int>left, index;
		for (int i = 1; i <= n; i++)
			left.push_back(qr[i].l), index.push_back(qr[i].id);
		if (qr[1].r >= qr[n].l)
		{
			cout << -1 << endl;
			go;
		}
		vector<int>v1, v2;
		int pos = 0;
		int i = 1;
		while (i <= n)
		{
			int pos = 0;
			int next_val = qr[i].r;
			int maxi = next_val;
			int next_pos = pos;
			int j = i;
			while (1)
			{
				int next = upper_bound(left.begin(), left.end(), next_val) - left.begin();
				int initial_maxi = maxi;
				while (next_pos < next)
					maxi = max(maxi, qr[j].r), j++, next_pos++;
				if (maxi > initial_maxi)
					next_val = maxi;
				else
				{
					break;
				}
			}
			if (v1.size() == 0)
			{
				while (pos < next_pos)
					v1.push_back(qr[i].id), i++, pos++;
			}
			else if (v2.size() == 0)
			{
				while (pos < next_pos)
					v2.push_back(qr[i].id), i++, pos++;
			}
			else
			{
				while (pos < next_pos)
					v1.push_back(qr[i].id), i++, pos++;
			}
		}
		if (v1.size() == 0 or v2.size() == 0)
		{
			cout << -1 << endl;
			go;
		}
		int ans[n + 1] = {0};
		for (int i = 0; i < v1.size(); i++)
			ans[v1[i]] = 1;
		for (int i = 0; i < v2.size(); i++)
			ans[v2[i]] = 2;
		for (int i = 1; i <= n; i++)
			cout << ans[i] << ' ';
		cout << endl;
	}
//.............................................. ٱلْحَمْدُ لِلَّٰ............................................
}


Comments

Submit
0 Comments
More Questions

1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111