1. 程式人生 > >(多校)Distinct Values

(多校)Distinct Values

Distinct Values

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


 

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

給出n,m,表示一個有n個數組成的區間,把它分為m個分割槽間,每個分割槽間中不能有重複的數,為了保證區間中的值和最小。

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
int a[maxn];
//struct node用結構體排序時超時了。。
//{
//    int l;
//    int r;
//}p[maxn];
//bool cmp(node p1,node p2)
//{
//    if(p[1].l==p2.l) return p1.r>=p2.r;
//    else return  p1.l<=p2.l;
//}
struct node{
    int l,r;
    bool operator < (const node &a)const{//過載運算子<,可以對兩個node用<操作符進行比較
        if(l==a.l) return r<a.r;
        else return l<a.l;
    }
}p[maxn];
int main()
{
    int n,m,t;
    scanf("%d",&t);
    while(t--)
    {
        set<int>st;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
            scanf("%d%d",&p[i].l,&p[i].r);
        sort(p+1,p+m+1);
        for(int i=1;i<=n;i++)
        {
            st.insert(i);
            a[i]=1;
        }

        for(int i=p[1].l;i<=p[1].r;i++)
        {
            a[i]=*st.begin();
            st.erase(st.begin());
        }
        int l=p[1].l;
        int r=p[1].r;
        for(int i=2;i<=m;i++)
        {
            if(p[i].l>=p[i-1].l&&p[i].r<=p[i-1].r) continue;
            while(p[i].l>l)
            {
                st.insert(a[l++]);
            }
            while(p[i].r>r)
            {
                if(p[i].l<=r+1)
                {
                   a[++r]=*st.begin();
                   st.erase(st.begin());
                }
                else r++;
            }
        }
        printf("%d",a[1]);
        for(int i=2;i<=n;i++)
            printf(" %d",a[i]);
        printf("\n");
    }
}