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]是否相 ...
随机推荐
- js bom和dom
一, 前言 到目前为止,我们已经学过了JavaScript的一些简单的语法.但是这些简单的语法,并没有和浏览器有任何交互. 也就是我们还不能制作一些我们经常看到的网页的一些交互,我们需要继续学习BOM ...
- 华为机试 求int型数据在内存中存储时1的个数
题目描述 输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数. 输入描述: 输入一个整数(int类型) 输出描述: 这个数转换成2进制后,输出1的个数 输入 5 输出 2 普通运算方 ...
- FZU:1759-Problem 1759 Super A^B mod C (欧拉降幂)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 欧拉降幂是用来干啥的?例如一个问题AB mod c,当B特别大的时候int或者longlong装不下的时 ...
- react ant-design自定义图标
ant-design给我们提供的图标不够怎么办呢?答案是我们可以自定义图标. 自定义图标也挺简单的,现在图标推荐用svg格式,那么我们就需要制作svg图片. 下面让我们看看如果制作svg图片吧. 1. ...
- Deep Learning 之 最优化方法
Deep Learning 之 最优化方法 2017年05月21日 22:18:40 阅读数:5910 写在前面本文主要是对Deep Learning一书最优化方法的总结,具体详细的算法,另起博文展开 ...
- 2,理解JVM
一.内存管理: 1,内存结构: 栈和堆区别,栈是连续内存区,一般是2M单位,堆是不连续的链表.受限于虚拟内存,new时分配 PC寄存器.java栈.堆.方法区.本地方法区.运行常量池 java ...
- LaTeX工具——mathpix安利
官网: https://mathpix.com/ 效果看下图: 图片打不开点这里 识别效果还行,感觉很适合jbc/zcy这种不喜欢打LaTex公式的神仙.
- 30分钟快速搭建Web CRUD的管理平台--django神奇魔法
加上你的准备的时间,估计30分钟完全够用了,因为最近在做爬虫管理平台,想着快速开发,没想到python web平台下有这么非常方便的框架,简洁而优雅.将自己的一些坑总结出来,方便给大家的使用. 准备环 ...
- 【个人训练】(POJ1276)Cash Machine
最近的很多题解应该都是dp相关的了,emmm因为dp对我而言思考难度比较大,那么为了理顺自己的思路当然只能通过写blog整理了.愿我能成功搞定dp这个大关!(至少中等难度的dp要能够解决啊o(TヘTo ...
- [模板]BZOJ4756线段树合并
题面 Solution: 板子不解释 #include <iostream> #include <algorithm> #include <cstdio> #inc ...