Practice Link

J. Different Integers

题意:

给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdots, a_n\)中有多少个不同的数。

思路:

先分别离线求出\(a_1, \cdots a_i\)以及\(a_j, \cdots, a_n\)中有多少个不同的数。

再考虑有多少个数既在\([1, i]\)中也在\([j, n]\)中,再离线做一次。

考虑一个数第一次出现的时候,那么这个数下一次出现的位置以及之后的所有询问区间都要减去一个贡献。

代码:

#include <bits/stdc++.h>
using namespace std; #define N 100010
int n, q, a[N], b[N], c[N], nx[N], ans[N];
struct node {
int l, r, id;
node() {}
void scan(int id) {
this->id = id;
scanf("%d%d", &l, &r);
if (l >= r) {
l = 1;
r = 2;
}
}
}qrr[N]; struct BIT {
int a[N];
void init() {
memset(a, 0, sizeof a);
}
void update(int x, int v) {
for (; x > 0; x -= x & -x) {
a[x] += v;
}
}
int query(int x) {
int res = 0;
for (; x < N; x += x & -x) {
res += a[x];
}
return res;
}
int query(int l, int r) {
return query(l) - query(r + 1);
}
}bit; int main() {
while (scanf("%d%d", &n, &q) != EOF) {
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
}
for (int i = 1; i <= q; ++i) {
qrr[i].scan(i);
}
if (n == 1) {
for (int i = 1; i <= q; ++i) {
printf("1\n");
}
continue;
}
sort(qrr + 1, qrr + 1 + q, [&](node x, node y) {
return x.l < y.l;
});
memset(b, 0, sizeof b);
for (int i = 1, j = 1, k = 0; i <= q; ++i) {
while (j <= n && j <= qrr[i].l) {
if (b[a[j]] == 0) {
b[a[j]] = 1;
++k;
}
++j;
}
ans[qrr[i].id] = k;
}
sort(qrr + 1, qrr + 1 + q, [&](node x, node y){
return x.r > y.r;
});
memset(b, 0, sizeof b);
for (int i = 1, j = n, k = 0; i <= q; ++i) {
while (j >= 1 && j >= qrr[i].r) {
if (b[a[j]] == 0) {
b[a[j]] = 1;
++k;
}
--j;
}
ans[qrr[i].id] += k;
}
memset(b, 0, sizeof b);
for (int i = 1; i <= n; ++i) {
nx[i] = n + 1;
}
for (int i = n; i >= 1; --i) {
c[i] = nx[a[i]];
if (nx[a[i]] == n + 1) {
nx[a[i]] = i;
}
}
bit.init();
sort(qrr + 1, qrr + 1 + q, [&](node x, node y){
return x.l < y.l;
});
for (int i = 1, j = 1; i <= q; ++i) {
while (j <= n && j <= qrr[i].l) {
if (b[a[j]] == 0) {
bit.update(c[j], -1);
b[a[j]] = 1;
}
++j;
}
ans[qrr[i].id] += bit.query(qrr[i].r, n);
}
for (int i = 1; i <= q; ++i) {
printf("%d\n", ans[i]);
}
}
return 0;
}

2018 Nowcoder Multi-University Training Contest 1的更多相关文章

  1. HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/500 ...

  2. 2018 Multi-University Training Contest 2

    题目链接:2018 Multi-University Training Contest 2 6318 Swaps and Inversions 题意:sum=x*逆序个数+交换次数*y,使sum最小 ...

  3. 2018 Multi-University Training Contest 1

    比赛链接:2018 Multi-University Training Contest 1 6301 Distinct Values 题意:输出一个长度为n的序列,要求满足m个区间的数都不相同,并且字 ...

  4. hdu 6301 Distinct Values (2018 Multi-University Training Contest 1 1004)

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

  5. 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...

  6. 2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 200 ...

  7. 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...

  8. 2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000 ...

  9. 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...

  10. 2018 Multi-University Training Contest 1 Distinct Values 【贪心 + set】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6301 Distinct Values Time Limit: 4000/2000 MS (Java/Ot ...

随机推荐

  1. SAS学习笔记12 SAS数据清洗和加工

    set语句纵向合并 我们把a1和b1进行合并,并区分是来自哪个数据集,会用到in=选项 in=a是产生临时变量a,由于它是a1的选项,所以a的值=1(来自a1)或者=0(不来自a1) in=b是产生临 ...

  2. 数据库开启最小补充日志hang住

    一.场景说明: 客户环境需要部署OGG,同事在数据库中执行添加最小补充日志,会话Hang住 二.环境测试 本次测试环境进行模拟,添加最小补充日志的操作,怎么会被Hang住呢? 2.1 模拟会话hang ...

  3. (三)ActiveMQ之发布- 订阅消息模式实现

    一.概念 发布者/订阅者模型支持向一个特定的消息主题发布消息.0或多个订阅者可能对接收来自特定消息主题的消息感兴趣.在这种模型下,发布者和订阅者彼此不知道对方.这种模式好比是匿名公告板.这种模式被概括 ...

  4. 【原创】大叔经验分享(89)docker启动openjdk执行jmap报错

    docker启动openjdk后,可以查看进程 # docker exec -it XXX jps 10 XXX.jar 可见启动的java进程id一直为10,然后可以执行jvm命令,比如 # doc ...

  5. map自定义键值类型

    map自定义键值类型 改变Map的默认比较方式 https://www.cnblogs.com/zjfdlut/archive/2011/08/12/2135698.html 大家知道,STL中的ma ...

  6. Go 代码风格和规范

    Go 语言写起来类似于C语言,因此熟悉C语言及其派生语言(C++.C#.Objective-C 等)的人都会迅速熟悉这门语言 编码风格 标识符命名规范 在习惯上,Go语言程序员推荐使用驼峰式命名,当名 ...

  7. python常见函数运用【一】

    1.Python hasattr() 函数 描述hasattr() 函数用于判断对象是否包含对应的属性. 语法 hasattr 语法: hasattr(object, name)参数object -- ...

  8. PHP函数问题

    有时候,运行nginx和PHP CGI(PHP FPM)web服务的Linux服务器,突然系统负载上升,用top命令查看,很多phpcgi进程的CPU利用率接近100%后来通过跟踪发现,这种情况与PH ...

  9. SAP官方发布的ABAP编程规范

    最近有朋友在公众号后台给我留言,"Jerry啊,你最近写的都是一些SAP研究院里面用到的新技术,能不能写点SAP传统的开发技术比如ABAP相关的东西"? 其实Jerry在刚开始写这 ...

  10. 一组简单好看的css3渐变按钮

    主要代码如下: body { background:#fff } /* Mixins */ /* bg shortcodes */ .bg-gradient1 span,.bg-gradient1:b ...