Distinct Values
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1620 Accepted Submission(s): 509
Problem Description
Chiaki has an array of n positive integers. You are told some facts about the array: for every two elements ai and aj in the subarray al..r (l≤i<j≤r), ai≠ajholds.
Chiaki would like to find a lexicographically minimal array which meets the facts.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains two integers n and m (1≤n,m≤105) -- the length of the array and the number of facts. Each of the next m lines contains two integers liand ri (1≤li≤ri≤n).
It is guaranteed that neither the sum of all n nor the sum of all m exceeds 106.
Output
For each test case, output n integers denoting the lexicographically minimal array. Integers should be separated by a single space, and no extra spaces are allowed at the end of lines.
Sample Input
3 2 1 1 2 4 2 1 2 3 4 5 2 1 3 2 4
Sample Output
1 2 1 2 1 2 1 2 3 1 1
Source
2018 Multi-University Training Contest 1
#include <bits/stdc++.h>
using namespace std;
const int mn = 1e5 + 10;
int ans[mn];
struct node
{
int l, r;
} t[mn];
bool cmp(const node& a, const node& b)
{
return a.l < b.l;
}
priority_queue <int, vector <int>, greater<int> > que;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
memset(ans, 0, sizeof ans);
while (!que.empty())
que.pop();
int n, q;
scanf("%d %d", &n, &q);
for (int i = 1; i <= q; i++)
scanf("%d %d", &t[i].l, &t[i].r);
sort(t + 1, t + q + 1, cmp); /// 从左往右贪心考虑每个区间
for (int i = 1; i <= n; i++) // 所有可取的数
que.push(i);
for (int i = t[1].l; i <= t[1].r; i ++)
{
ans[i] = que.top();
que.pop(); // 已取
}
int l = t[1].l, r = t[1].r; // l、r双指针
for (int i = 2; i <= q; i++)
{
if (t[i].r <= r)
continue;
else if (t[i].l > r) // 前后区间完全不重叠
{
for (int j = l; j <= r; j++)
que.push(ans[j]);
l = t[i].l, r = t[i].r;
for (int j = l; j <= r; j++)
{
ans[j] = que.top();
que.pop();
}
}
else // 两区间有重叠部分
{
for (int j = l; j < t[i].l; j++)
que.push(ans[j]);
for (int j = r + 1; j <= t[i].r; j++)
{
ans[j] = que.top();
que.pop();
}
l = t[i].l, r = t[i].r;
}
}
for (int i = 1; i <= n; i++)
{
if (!ans[i])
printf("1");
else
printf("%d", ans[i]);
if (i == n)
printf("\n");
else
printf(" ");
}
}
return 0;
}