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. C#记录日志到本地文件工具类

    using System; using System.Diagnostics; using System.IO; using System.Threading; using System.Web; n ...

  2. 【转载】在使用JDBC连接MySql时报错:You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support

    在使用JDBC连接MySql时报错:You must configure either the server or JDBC driver (via the serverTimezone config ...

  3. ligerui tab 部分记载

    打开一个Tab $(".strength_box").click(function () { var id = $(this).attr("data"); va ...

  4. Django 之一些request封装的常用功能

    一些常用的request对象属性 介绍 HTTP 应用的信息是通过 请求报文 和 响应报文 传递的,关于更多的相关知识,可以阅读<HTTP权威指南>获得. 其中 请求报文 由客户端发送,其 ...

  5. HighChart 不同颜色(柱状图)

    var chart = new Highcharts.Chart({ chart: { plotBackgroundColor: null, plotBorderWidth: null, backgr ...

  6. apk签名文件生成

    一.生成签名文件 命令: keytool -genkey -v -keystore 签名文件名称.keystore -alias 签名文件别名 -keyalg RSA -keysize 2048 -v ...

  7. 08_Azkaban案例实践1_Command单一job示例

    1.Azkaban实战 Azkaba内置的任务类型支持command.java 2.Command类型单一job示例 1.创建job描述文件:vi command.job #command.job t ...

  8. ubuntu python3虚拟环境

    mkvirtualenv flow_chart -p /usr/bin/python3.6 #  命令    环境名    -p   python所在路径 pip install -r request ...

  9. 通用分页model封装pageList

    package selfimpr.page; import java.util.List; /** * 分页模型 * @param <T> 数据泛型 * @author selfimpr ...

  10. springboot 学习小结

    springboot 默认自动扫描和配置根包下面的类.如果启动配置不在根包目录下,得把对应的类进行配置扫描生成对应的bean. 主要的扫描注解有: @SpringBootApplication //s ...