hdu6301

Distinct Values

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 542    Accepted Submission(s): 129


 

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 li and 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

#include<bits/stdc++.h>
using namespace std;

#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ull unsigned long long
#define ll long long

const int maxn = 100010;

struct node
{
    int l,r;
    friend bool operator<( const node&a , const node&b )
    {
        return a.l<b.l;
    }
}data[maxn];
int a[maxn];
set<int>S;

int main()
{
    for ( int T ; scanf ( "%d" , &T )==1 ; )
    {
        for ( int Cas=1 ; Cas<=T ; Cas++ )
        {
            S.clear();
            int n,m;
            scanf ( "%d%d" , &n , &m );
            for ( int i=0 ; i<m ; i++ )
                scanf ( "%d%d" , &data[i].l , &data[i].r );
            for ( int i=1 ; i<=n ; i++ )
                S.insert(i);
            sort ( data , data+m );
            int l=1,r=1;
            for ( int i=0 ; i<m ; i++ )
            {
                while ( l<data[i].l )
                {
                    if ( l==r )
                    {
                        a[l++] = 1;
                        r++;
                    }
                    else
                        S.insert(a[l++]);
                }
                while ( r<=data[i].r )
                {
                    a[r++] = *(S.begin());
                    S.erase(*(S.begin()));
                }
            }
            while ( r<=n )
                a[r++] = 1;
            for ( int i=1 ; i<=n ; i++ )
            {
                printf ( "%d" , a[i] );
                if ( i==n )
                    printf ( "\n" );
                else
                    printf ( " " );
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yu121380/article/details/81174356