# 【HDU - 6301】Distinct Values（思維+優先隊列）

Chiaki has an array of nn positive integers. You are told some facts about the array: for every two elements aiai and ajaj in the subarray al..ral..r (l≤i<j≤rl≤i<j≤r), ai≠ajai≠aj holds.
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 TT, indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1≤n,m≤1051≤n,m≤105) -- the length of the array and the number of facts. Each of the next mm lines contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n).

It is guaranteed that neither the sum of all nn nor the sum of all mm exceeds 106106.

Output

For each test case, output nn 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```

ac代碼：

``````#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
#define mod (1000000007)
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int MAX=110000;
struct node{
int l,r;
friend bool operator<(node a,node b){
if(a.l==b.l)
return a.r>b.r;//?
return a.l<b.l;
}
}se[MAX];
int a[MAX];
int main()
{
int t;
cin>>t;
while(t--)
{
priority_queue<int,vector<int>,greater<int> > que;
memset(a,-1,sizeof(a));
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
scanf("%d%d",&se[i].l,&se[i].r);
sort(se,se+m);
for(int i=1;i<=n+10;i++)
que.push(i);
for(int i=se[0].l;i<=se[0].r;i++)
{
int tm=que.top();
que.pop();
a[i]=tm;
}
int L=se[0].l,R=se[0].r;
for(int i=1;i<m;i++)
{
if(se[i].l==L){
continue;
}
if(se[i].r<=R) continue;
int minn=min(se[i].l,R+1);
int maxx=max(se[i].l,R+1);
for(int j=L;j<minn;j++)
que.push(a[j]);
for(int j=maxx;j<=se[i].r;j++)
{
int tm=que.top();
que.pop();
a[j]=tm;
}
L=se[i].l;R=se[i].r;
}
for(int i=1;i<=n;i++)
{
printf("%d",(a[i]==-1)?1:a[i]);
if(i==n)puts("");
else printf(" ");
}
}

return 0;
}
/*
1
15 4
3 5
1 3
2 2
7 8
*/``````