2018杭电多校训练Contest 1 .1004 Distinct Values (Set 维护)

Distinct Values

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


 

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 个区间[l,r]  内 保证 不能重复,  但 相交区域外的可以重复, 问 最小字典序的 这样一个序列

[思路]

采用 Set 维护,  Set 中 存放 1-n   ,  每次 在 [L,R]  中 从set 取一个,删除一个,  

然后 将L 之前的 在存入set 中.

一直维护set, 用 两个下标 left 和 now  标记,     

注意  在 Set 删数时, 指针要先自加, 在删除, 不然会内存访问错误.

[code]

#include <iostream>
#include <bits/stdc++.h>
/*
*
* Author : siz
*
*/
using namespace std;


typedef long long ll;

const int MAXN = 1e6+200;
int ans[MAXN];

typedef pair<int,int> PAIR;
set<int> S;
set<int>::iterator it,te;
vector<PAIR> p;

int main()
{
    int t;
    int n,m;
    scanf("%d",&t);
    while(t--)
    {

        int left = 1 ,now = 1;
        scanf("%d%d",&n,&m);
        S.clear();
        p.clear();
        for(int i = 1 ; i <= n; i++)
            S.insert(i);
        for(int i = 1 ;i <= n ; i++)
            p.push_back( make_pair(i,i) );
        for(int i = 0 ; i < m ; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            p.push_back( make_pair(x,y) );
        }
        sort(p.begin(),p.end());
        for(int i = 0 ; i < p.size(); i++)
        {
            while(left < p[i].first)
                S.insert(ans[left++]);
            for( it = S.begin(); now <= p[i].second && it != S.end(); )
            {
                ans[now++] = *it;
                int te = *it;
                it++;
                S.erase(te);
            }
        }
        for(int i = 1 ; i <= n; i++)
        {
            if(i == n)
                printf("%d\n",ans[i]);
            else
                printf("%d ",ans[i]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sizaif/article/details/81169906