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]是否相 ...
随机推荐
- python网络编程的坑(持续更新)
初学python,踩了许多坑...每天都学一点吧..(大佬绕过) 1.session的用法: session是python requests库中的一个重要功能.session可以存储用户的数据并且存储 ...
- (原创)用Verilog实现一个参数化的呼吸灯(Verilog,CPLD/FPGA)
1.Abstract 观察到一个有趣的现象,每当把Apple笔记本合上的时候,那个白色的呼吸灯就会反复地由暗渐明,然后又由明渐暗,乍一看就像Apple笔记本在打盹休息一样,十分可爱!于是突发奇 ...
- HBase java API 的使用范例(增,删,查,扫描)
编辑pom.xml <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase ...
- 521. [NOIP2010] 引水入城 cogs
521. [NOIP2010] 引水入城 ★★★ 输入文件:flow.in 输出文件:flow.out 简单对比时间限制:1 s 内存限制:128 MB 在一个遥远的国度,一侧是风景秀 ...
- 『Golang』在Golang中使用json
由于要开发一个小型的web应用,而web应用大部分都会使用json作为数据传输的格式,所以有了这篇文章. 包引用 import ( "encoding/json" "gi ...
- 最火的.NET开源项目[转]
综合类 微软企业库 微软官方出品,是为了协助开发商解决企业级应用开发过程中所面临的一系列共性的问题, 如安全(Security).日志(Logging).数据访问(Data Access).配置管理( ...
- C#异步了解一下
如何让你的代码在“同一时间”干着两件件事呢?比如说,在初始化加载配置的同时,UI界面能够响应用户的各种点击事件.而不置于卡死,特别是出现如下面这种情况的时候,对于用户来说是很崩溃的.
- Linux-Shell脚本编程-学习-1-Linux基本命令
在学习Linux-Shell脚本编程之前,我们需要学习一定的Linux基本命令,不然在后面学习Shell脚本编程的的时候,我们就呵呵了. 我学习所用的系统是Ubuntu 16.04版本 也没有什么规则 ...
- mysql修改外部访问权限
mysql>use mysql; mysql>update user set host =’%’ where user=’root’ mysql>select host,user f ...
- flask中static_folder与static_url_path的区别与联系
# -*- coding:utf-8 -*- from flask import Flask, url_for app1 = Flask(__name__, static_folder='mystat ...