Distinct Values

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

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
 
Source
 
思路:
打的好菜啊。。最后看了学长的思路才写出来了,完全没想到线段树的思路aaaaa。
这道题难点就是在处理之前区间与当前区间重叠区间的数。。。
线段树写法就是将现在这个区间的赋值作为下标存进线段树+1,当访问下一个区间时将重叠部分之前的部分全部变为0,
这样重叠部分的下标依旧是1然后求区间mex依此赋值,并依此更新下下标(也就是把访问过的标为1),讲的有点乱,看下代码把。
 
实现代码:
#include<bits/stdc++.h>
using namespace std;
const int M = 1e5+;
int sum[M<<];
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1 void pushup(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
} void update(int p,int c,int l,int r,int rt){
if(l == r){
sum[rt] += c;
return ;
}
mid;
if(p <= m) update(p,c,lson);
else update(p,c,rson);
pushup(rt);
} void build(int l,int r,int rt){
if(l == r){
sum[rt] = ;
return ;
}
mid;
build(lson);
build(rson);
pushup(rt);
} int query(int l,int r,int rt){
if(l == r){
return l;
}
mid;
if(sum[rt<<] < m-l+) query(lson);
else query(rson);
}
struct node{
int l,r;
}a[M];
int b[M]; bool cmp(node a,node b){
if(a.l == b.l) return a.r > b.r;
return a.l < b.l;
} int main()
{
int t,n,m;
while(scanf("%d",&t)!=EOF){
while(t--){
scanf("%d%d",&n,&m);
for(int i = ;i <= m;i ++){
scanf("%d%d",&a[i].l,&a[i].r);
}
sort(a+,a++m,cmp);
build(,n,);
memset(b,,sizeof(b));
int L = ,R = ,u = ;
for(int i = ;i <= m;i ++){
if(a[i].r < R+) continue;
for(int j = L;j < a[i].l;j ++){
if(b[j] == ) continue;
update(b[j],-,,n,);
}
if(R >= a[i].l) u = R+;
else u = a[i].l;
for(int j = u;j <= a[i].r;j ++){
int cnt = query(,n,);
b[j] = cnt;
update(cnt,,,n,);
}
L = a[i].l; R = a[i].r;
} for(int i = ;i <= n;i ++){
if(b[i]==) printf("");
else printf("%d",b[i]);
if(i == n) printf("\n");
else printf(" ");
}
}
}
return ;
}

最新文章

  1. MVVM简介
  2. Android开发之Git配置
  3. csharp: Sound recording
  4. 【poj2234】 Matches Game
  5. 开班典礼-老师玩命的教,大家玩命的学,沉静,18K
  6. [转]centos7 配置yum源(本地+光盘)
  7. python爬虫-urllib模块
  8. 获取通讯录的信息(关于iOS9.0之后新的框架-ContactFramework)
  9. 带控制端的逻辑运算电路_分别完成正整数的平方、立方和阶乘的运算verilog语言
  10. 安装sql server 2008 management studio时,提示升级VS2008 到 SP1
  11. python开发的一些tips
  12. 【集训笔记】母函数【母函数模板】【HDOJ1028【HDOJ1085
  13. HDU 4028 The time of a day STL 模拟题
  14. C语言之成绩转换
  15. DWM1000 测距原理简单分析 之 SS-TWR代码分析1 -- [蓝点无限]
  16. update_or_create()
  17. Python全栈开发之路 【第十八篇】:Ajax技术
  18. springboot06-swagger2 自动化api文档
  19. goaccess geoip 测试
  20. [Lua]table(二):删除与排序

热门文章

  1. odoo销售转生产
  2. 利尔达推出工控解决方式 串口转以太网模块LSD1ES-W5500_S2E0
  3. 林帆:Docker运行GUI软件的方法
  4. MSF里MS17_010利用模块笔记
  5. 在Docker中安装.NET Core(使用命令行工具)
  6. WPF的DataTrigger绑定自身属性
  7. python 回溯法 子集树模板 系列 —— 6、排课问题
  8. EJB开发第一期---EJB开发配置
  9. python 获取文件和文件夹大小
  10. JavaScript组成—— DOM、BOM、ECMAScript