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 ;
}

hdu 6301 Distinct Values (2018 Multi-University Training Contest 1 1004)的更多相关文章

  1. hdu 6301 Distinct Values (思维+set)

    hdu 6301 Distinct Values 题目传送门 题意: 给你m个区间,让你求出一个长度为n的区间且满足在这些区间的数不重复, 并且要求字典序最小 思路: 如果我们已经求出这个序列了,你会 ...

  2. hdu 6301 Distinct Values (贪心)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  3. hdu 6301 Distinct Values(贪心)题解

    题意:长为n的串,给你m个区间,这些区间内元素不重复,问这样的串字典序最小为? 思路:用set保存当前能插入的元素,这样就能直接插入最小元素了.对操作按l排序,因为排过的不用排,所以两个指针L,R是一 ...

  4. HDU 6301 Distinct Values

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6301 多校contest1 题目大意是有一个长度为N的数组,给出M个"事实",每个 ...

  5. hdu 6301 Distinct Values (双指针,水题)

    大意: 给定m个区间, 求构造一个长n且字典序最小的序列, 使得每个区间内的数各不相同 求出每个位置为左端点时向右延伸最大距离, 然后双指针, 每次从set中取最小 #include <iost ...

  6. HDU 6301.Distinct Values-贪心、构造字典序最小的数列 (2018 Multi-University Training Contest 1 1004)

    HDU6301.Distinct Values 这个题就是给你区间要求区间内的数都不相同,然后要求是字典序最小,直接贪心走一遍,但是自己写的时候,思路没有错,初始化写挫了... 将区间按左端点小的排序 ...

  7. HDU6301 Distinct Values (多校第一场1004) (贪心)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. HDU 1045 Fire Net(dfs,跟8皇后问题很相似)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)   ...

  9. HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. DQN(Deep Reiforcement Learning) 发展历程(二)

    目录 动态规划 使用条件 分类 求解方法 参考 DQN发展历程(一) DQN发展历程(二) DQN发展历程(三) DQN发展历程(四) DQN发展历程(五) 动态规划 动态规划给出了求解强化学习的一种 ...

  2. 20155218 Exp1 PC平台逆向破解(5)M

    20155218 Exp1 PC平台逆向破解(5)M 1. 掌握NOP.JNE.JE.JMP.CMP汇编指令的机器码 NOP:NOP指令即"空指令".执行到NOP指令时,CPU什么 ...

  3. Solr 后台查询实例 (工作备查)

    有时间再进行整理package xxx.service.impl; import java.util.HashMap; import java.util.Map; import java.util.M ...

  4. 深入了解Kubernetes REST API的工作方式

    关于Kubernetes REST API的工作方式: 在哪里以及如何定义从REST路径到处理REST调用的函数的映射? 与etcd的交互发生在哪里? 从客户端发出请求到保存在etcd中对象的端到端路 ...

  5. 散列(Hash)表入门

    一.概述 以 Key-Value 的形式进行数据存取的映射(map)结构 简单理解:用最基本的向量(数组)作为底层物理存储结构,通过适当的散列函数在词条的关键码与向量单元的秩(下标)之间建立映射关系 ...

  6. JAVA笔试准备

    建立时间:2019.4.19 修改时间: 腾讯:选择题(30个,一小时内),简答(2道)和编程题(2道) 涉及内容:(也有可能全是算法)C++,JAVA,数据结构,网络,Linux,计算题 1. 磁盘 ...

  7. PHPMyWind5.4存储XSS(CVE-2017-12984)

    0x0 环境 操作机:xp   192.168.110.128 目标:win2003    192.168.110.133 目标cms:PHPMyWind5.4 0x11 漏洞介绍 允许恶意访问者在客 ...

  8. Python进阶量化交易场外篇3——最大回撤评价策略风险

    新年伊始,很荣幸笔者的<教你用 Python 进阶量化交易>专栏在慕课专栏板块上线了,欢迎大家订阅!为了能够提供给大家更轻松的学习过程,笔者在专栏内容之外会陆续推出一些手记来辅助同学们学习 ...

  9. PHP密码的六种加密方式

    1. MD5加密 string md5 ( string $str [, bool $raw_output = false ] ) 参数 str  --  原始字符串. raw_output  --  ...

  10. PAT甲题题解-1042. Shuffling Machine (20)-模拟

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789205.html特别不喜欢那些随便转载别人的原创文章又不给 ...