2018 Nowcoder Multi-University Training Contest 1
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的更多相关文章
- 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 ...
- 2018 Multi-University Training Contest 2
题目链接:2018 Multi-University Training Contest 2 6318 Swaps and Inversions 题意:sum=x*逆序个数+交换次数*y,使sum最小 ...
- 2018 Multi-University Training Contest 1
比赛链接:2018 Multi-University Training Contest 1 6301 Distinct Values 题意:输出一个长度为n的序列,要求满足m个区间的数都不相同,并且字 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Python+Appium启动手机APP或者浏览器
一.设备信息配置 脚本如下: from appium import webdriver class my_app(): def __init__(self): desired_caps = {} # ...
- 前端 aes 加密
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- react中jsx文件是如何转换成js对象的
通过在线babel转换器,转换出jsx是如何变成js对象的 jsx文件 加入了正常的标签以及嵌套标签以及方法属性 function hello() { click=()=>{ console.l ...
- GDB数据库SQL操作平台
GDB数据库SQL操作平台 开发本软件的初衷:由于计算数据库要素层属性的时候,涉及到要计算多个字段,或者要根据代码计算名称,得一个一个的筛选并计算,过程比较繁琐,于是就想能不能通过像处理SQLServ ...
- C# 中类的成员有哪些?
类(class)是C#类型中最基础的类型.类是一个数据结构,将状态(字段)和行为(方法和其他函数成员)组合在一个单元中.类提供了用于动态创建类实例的定义,也就是对象(object).类支持继承(inh ...
- matlab cell
cell元包是matlab中提供的一种数据类型,功能强大. 关于cell的创建: 1.跟一般创建举证一样,直接使用C = {A B D E}这种形式,不过这里把"[]"改成了}&q ...
- 每次开机都要按F1的解决办法
买了个新的硬盘来装电脑,装操作系统时到微软官网下载了WIN10放在U盘里制作成系统安装盘,具体操作自己百度.装好了之后发现每次开机都要按一下F1,百度了很多都没用, 一次偶然的机会,我拆开了电脑主机硬 ...
- C# 循环中 直接索引 VS 缓存索引 性能测试
using System; namespace TestCSharp { class MainClass { public class t1 { public b1 b = new b1(); } p ...
- 软件打包 Inno
官网 http://www.jrsoftware.org/ 新建 点击工具栏第一项"新建".输入产品的名称.版本号.公司网址等信息 添加应用程序文件 应用程序图标 应用程序文档 许 ...
- RabbitMQ中初始化ConnectionFactory常用设置属性
初始化ConnectionFactory 代码 ConnectionFactory factory = new ConnectionFactory(); factory.setHost(ip); fa ...