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. 利用WebHook实现PHP自动部署Git代码

    平时项目代码都托管在Coding,然后每次提交了代码之后都要SSH到服务器上去git pull一次,很是繁琐,在看了OverTrue的<使用PHP脚本远程部署git项目>后就尝试在自己服务 ...

  2. [胡泽聪 趣题选讲]大包子环绕宝藏-[状压dp]

    Description 你有一个长方形的地图,每一个格子要么是一个障碍物,要么是一个有一定价值的宝藏,要么是一个炸弹,或者是一块空地.你的初始位置已经给出.你每次可以走到上.下.左.右这四个相邻的格子 ...

  3. VS新建一个模板工程

    新建一个模板工程的好处:    1.就不用每次都走一边新建向导了,新建工程一步到位. 2.可以往项目中每次都的输入的代码,如一些声明注释-- 效果图: 具体步骤: 1.自己先新建一个属于自己的工程. ...

  4. 解决:Linux SSH Secure Shell(ssh) 超时断开的解决方法

    转载:http://www.cnblogs.com/jifeng/archive/2011/06/25/2090118.html 修改/etc/ssh/sshd_config文件,找到 ClientA ...

  5. wkhtmltopdf 参数介绍

    wkhtmltopdf [OPTIONS]... <input file> [More input files] <output file> 常规选项   --allow &l ...

  6. 利用Junit实现eclipse单元测试

    在eclipse中使用Junit进行单元测试 今天学会了用Junit在eclipse中进行单元测试,代码的测试工作,在整个软件开发中占有总要的地位,无论是代码开发阶段,还是代码维护阶段.另外边开发边测 ...

  7. 微信小程序中跳转另一个小程序

    wx.navigateToMiniProgram({ appId: 'xxxxxxxxxxxxxxxxxx', // 要跳转的小程序的appid path: 'page/index/index', / ...

  8. 关于hive的优化

    首先hive本质就是mapreduce,那么优化就从mapreduce开始入手. 然而mapreduce的执行快慢又和map和reduce的个数有关,所以我们先从这里下手,调整并发度. 关于map的优 ...

  9. [T-ARA][Ma boo]

    歌词来源:http://music.163.com/#/song?id=22704447 作曲 : 金道勋/Rhymer [作曲 : 金道勋/Rhymer] 作词 : 金道勋 [作词 : 金道勋] 사 ...

  10. BugPhobia开发篇章:Beta阶段第III次Scrum Meeting

    0x01 :Scrum Meeting基本摘要 Beta阶段第三次Scrum Meeting 敏捷开发起始时间 2015/12/15 00:00 A.M. 敏捷开发终止时间 2015/12/15 23 ...