Different Integers 牛客多校第一场只会签到题
Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l2, r2), ..., count(lq, rq) where count(i, j) is the number of different integers among a1, a2, ..., ai, aj, aj + 1, ..., an.
输入描述:
The input consists of several test cases and is terminated by end-of-file.The first line of each test cases contains two integers n and q.The second line contains n integers a1, a2, ..., an.The i-th of the following q lines contains two integers li and ri.
输出描述:
For each test case, print q integers which denote the result.
示例1
输入
复制
3 2
1 2 1
1 2
1 3
4 1
1 2 3 4
1 3
输出
复制
2
1
3
备注:
* 1 ≤ n, q ≤ 105* 1 ≤ ai ≤ n* 1 ≤ li, ri ≤ n* The number of test cases does not exceed 10.
这是唯一一题签到题 ,现场找的板子,修改一下过的
最简单粗暴的方法,把数组扩张一倍
修改一下查询区间。板子题的变形吧
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <stack>
#include <queue>
using namespace std;
#define ll long long
const int maxn1 = ;
int cur[maxn1];
int then[maxn1];
int ans[maxn1];
int limit, n, m;
struct node {
int l, r, id;
} que[maxn1];
bool cmp(node x, node y) {
if(x.l / limit == y.l / limit) return x.r < y.r;
return x.l / limit < y.l / limit;
}
void solve() {
int L, R, ans1;
L = R = ;
ans1 = ;
for(int i = ; i <= m; i++) {
while(que[i].l > L) {
then[cur[L]]--;
if(then[cur[L]] == )
ans1--;
L++;
}
while(que[i].r < R) {
then[cur[R]]--;
if(then[cur[R]] == )
ans1--;
R--;
}
while(que[i].l < L) {
L--;
then[cur[L]]++;
if(then[cur[L]] == )
ans1++;
}
while(que[i].r > R) {
R++;
then[cur[R]]++;
if(then[cur[R]] == )
ans1++;
}
ans[que[i].id] = ans1;
}
for(int i = ; i <= m; i++)
printf("%d\n", ans[i]);
}
int main() {
while(scanf("%d", &n) != EOF) {
scanf("%d", &m);
memset(cur, , sizeof(cur));
memset(then, , sizeof(then));
memset(ans, , sizeof(ans));
for(int i = ; i <= n; i++)
scanf("%d", &cur[i]);
for (int i = n + ; i <= * n ; i++)
cur[i] = cur[i - n];
for(int i = ; i <= m; i++) {
scanf("%d%d", &que[i].l, &que[i].r);
int temp1 = que[i].l, temp2 = que[i].r;
que[i].l = temp2, que[i].r = temp1 + n;
que[i].id = i;
}
limit = (int)(sqrt( * n) + 0.5);
memset(then, , sizeof(then));
sort(que + , que + + m, cmp);
solve();
}
return ;
}
Different Integers 牛客多校第一场只会签到题的更多相关文章
- 线段树优化dp——牛客多校第一场I(好题)
和两天做了两道数据结构优化dp的题,套路还是差不多的 题解链接! https://www.cnblogs.com/kls123/p/11221471.html 一些补充 其实这道题的dp[i]维护的不 ...
- 2019牛客多校第一场 I Points Division(动态规划+线段树)
2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...
- 牛客多校第一场 B Inergratiion
牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...
- 2019年牛客多校第一场B题Integration 数学
2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...
- 2019牛客多校第一场E ABBA(DP)题解
链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 ABBA 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语 ...
- Different Integers(牛客多校第一场+莫队做法)
题目链接:https://www.nowcoder.com/acm/contest/139/J 题目: 题意:给你n个数,q次查询,对于每次查询得l,r,求1~l和r~n元素得种类. 莫队思路:1.将 ...
- 2019牛客多校第一场 A.Equivalent Prefixes
题目描述 Two arrays u and v each with m distinct elements are called equivalent if and only if RMQ(u,l,r ...
- 牛客多校第一场 A Equivalent Prefixes 单调栈(笛卡尔树)
Equivalent Prefixes 单调栈(笛卡尔树) 题意: 给出两个数组u,v,每个数组都有n个不同的元素,RMQ(u,l,r)表示u数组中[l,r]区间里面的最小值标号是多少,求一个最大的m ...
- 2019牛客多校第一场A-Equivalent Prefixes
Equivalent Prefixes 传送门 解题思路 先用单调栈求出两个序列中每一个数左边第一个小于自己的数的下标, 存入a[], b[].然后按照1~n的顺序循环,比较 a[i]和b[i]是否相 ...
随机推荐
- C语言实现简易扫雷
首先,写代码之前要将整体思路写出来: 扫雷游戏:1.需要两个二维数组,一个用来展示,一个用来放雷; 2.整体骨架在代码中都有注释说明; 3.游戏难度比较简单,适合初学者观看,如果有大佬看明白,可以指点 ...
- 嵌入式框架Zorb Framework搭建二:环形缓冲区的实现
我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...
- (数据科学学习手札14)Mean-Shift聚类法简单介绍及Python实现
不管之前介绍的K-means还是K-medoids聚类,都得事先确定聚类簇的个数,而且肘部法则也并不是万能的,总会遇到难以抉择的情况,而本篇将要介绍的Mean-Shift聚类法就可以自动确定k的个数, ...
- 【SAPUI5】ODataを構成するもの
はじめに SAPUI5でアプリケーションを作るにあたり.ODataは避けては通れないトピックです.結構広いテーマなので.5-7回くらいに分けて書きたいと思います.1回目はODataの概要について説明し ...
- fastDFS 上传 java源码
要想搭建fastDFS网上有相近的文章: 分布式文件系统 - FastDFS 在 CentOS 下配置安装部署 分布式文件系统 - FastDFS 配置 Nginx 模块及上传测试 首先下载fastd ...
- RHCE7认证学习笔记17——KickStart安装系统
一.自动化安装系统工具 1.Cobbler 另一个自动化安装工具: 2.Kickstart 二.使用kickstart自动化安装系统 服务器安装的软件: 1.dhcp服务 [root@lin ...
- windows下网络命令----Tracert命令详解
现在网络四通八达,网线光纤基站卫星,只要运营商能收费的地方,就有网络,覆盖了全世界所有的区域.彻底改变了以前通讯基本靠吼的情况.那么宽广的网络世界,超过100米就得需要中继放大信号的网线,即使现在的光 ...
- docker基础-虚拟化与容器介绍
正如所有关心docker技术的人所知道的那样,docker是以容器虚拟化为技术为基础的软件,因此在学习docker具体的内容之前,有必要讨论一下虚拟化和容器技术. 虚拟化技术: 在了解虚拟化技术时,各 ...
- JavaSE复习(六)函数式接口
函数式接口 有且仅有一个抽象方法的接口 @FunctionalInterface注解 一旦使用该注解来定义接口,编译器将会强制检查该接口是否确实有且仅有一个抽象方法,否则将会报错.需要注 意的是,即使 ...
- Android Service 服务(二)—— BroadcastReceiver
(转自:http://blog.csdn.net/ithomer/article/details/7365147) 一. BroadcastReceiver简介 BroadcastReceiver,用 ...