杭电2018多校——D题:Distinct Values分析大佬的代码(代码是大神的)

 

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≠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 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

 

Source

2018 Multi-University Training Contest 1

 

Recommend

liuyiding

// D
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=101000;
int _,n,m,pre[N],l,r,ret[N];
int main() {
	/*分析样例5 2
			  1 3
			  2 4 
	*/ 
	for (scanf("%d",&_);_;_--) {
		scanf("%d%d",&n,&m);
		rep(i,1,n+1) pre[i]=i;//pre : 1 2 3 4 5 
		rep(i,0,m) {
			scanf("%d%d",&l,&r);
			pre[r]=min(pre[r],l);//pre :1 2 1 2 5初步得到对应位置最开始影响的坐标位置 
		}
		per(i,1,n) pre[i]=min(pre[i],pre[i+1]); //pre :1 1 1 2 5 得到最终的影响区间 
		int pl=1;
		set<int> val;
		rep(i,1,n+1) val.insert(i); //  val : 1 2 3 4 5   
		rep(i,1,n+1) {              //  到4坐标之前都是在同一个区间所以前面是1 2 3 
			while (pl<pre[i]) {     //  当它到达4位置时可以知道它最早的影响区间为2下标, 
				val.insert(ret[pl]);//  所以它所在位置在小于ret[2]都可以赋值(也就是在val中加入x,x<ret[2]
									//  所以ret为1,pl为2 
				pl++;               //  当到5下标同理可加入pl到pre[5]之前的 
			}						//  其实比如5时输入区间没有包含的所以它一定时1,而 
			ret[i]=*val.begin();    //  从pl到改位置的ret又使得val又变成了1~n 
			val.erase(ret[i]);		//  为什么呢?因为到5时(也就是到输入没有包含的区间时,
									//  发生了断层,所以所以后面的都是从1开始赋值 
		}
		rep(i,1,n+1) printf("%d%c",ret[i]," \n"[i==n]);
	}
}

猜你喜欢

转载自blog.csdn.net/doublekillyeye/article/details/81179408