题目链接:https://www.nowcoder.com/acm/contest/139/J

题目:

题意:给你n个数,q次查询,对于每次查询得l,r,求1~l和r~n元素得种类。

莫队思路:1.将元素copy一份到最右边然后对于每次查询得l,r,我们就可以转换成求r,l+n这一个连续区间得元素种类,就将其转换成了一个莫队模板题了(比赛时还不会莫队就随便找了个板子);

     2.将移动的两个指针l设为0,r设为n+1,然后进行莫队即可。

做法一代码实现如下:

 #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 init()
{
for(int i = ;i <= n;i++) {
scanf("%d",&cur[i]);
cur[i+n] = cur[i];
}
for(int i = ;i <= m;i++)
{
scanf("%d%d",&que[i].l,&que[i].r);
int t = que[i].r;
que[i].r = que[i].l + n;
que[i].l = t;
que[i].id = i;
}
limit = (int)(sqrt( * n)+0.5);
memset(then,,sizeof(then));
memset(ans, , sizeof(ans));
sort(que+,que++m,cmp);
}
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%d", &n, &m)) {
init();
solve();
}
return ;
}

做法二代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef unsigned long long ull; #define bug printf("*********\n");
#define FIN freopen("in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f; inline int read() {//读入挂
int ret = , c, f = ;
for(c = getchar(); !(isdigit(c) || c == '-'); c = getchar());
if(c == '-') f = -, c = getchar();
for(; isdigit(c); c = getchar()) ret = ret * + c - '';
if(f < ) ret = -ret;
return ret;
} int n, q, sum, block;
int a[maxn], cnt[maxn], pos[maxn]; struct node {
int l, r, ans, id;
}ask[maxn]; bool cmp(const node& x, const node& y) {
return pos[x.l] == pos[y.l] ? x.r < y.r : x.l < y.l;
} void add(int x) {
cnt[x]++;
if(cnt[x] == ) sum++;
} void del(int x) {
if(cnt[x] == ) sum--;
cnt[x]--;
} int main() {
//FIN;
while(~scanf("%d%d", &n, &q)) {
for(int i = ; i <= n; i++) {
cnt[i] = ;
}
sum = ;
block = sqrt( * n);
for(int i = ; i <= n; i++) {
a[i] = read();
pos[i] = (i - ) / block;
}
for(int i = ; i <= q; i++) {
ask[i].l = read();
ask[i].r = read();
ask[i].id = i;
}
sort(ask + , ask + q + , cmp);
for(int i = , l = , r = n + ; i <= q; i++) {
while(r < ask[i].r) del(a[r++]);
while(r > ask[i].r) add(a[--r]);
while(l < ask[i].l) add(a[++l]);
while(l > ask[i].l) del(a[l--]);
ask[ask[i].id].ans = sum;
}
for(int i = ; i <= q; i++)
printf("%d\n", ask[i].ans);
}
return ;
}

Different Integers(牛客多校第一场+莫队做法)的更多相关文章

  1. Different Integers 牛客多校第一场只会签到题

    Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, r ...

  2. 2019牛客多校第一场 I Points Division(动态规划+线段树)

    2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...

  3. 牛客多校第一场 B Inergratiion

    牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...

  4. 2019年牛客多校第一场B题Integration 数学

    2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...

  5. 2019牛客多校第一场E ABBA(DP)题解

    链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 ABBA 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语 ...

  6. 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 ...

  7. 牛客多校第一场 A Equivalent Prefixes 单调栈(笛卡尔树)

    Equivalent Prefixes 单调栈(笛卡尔树) 题意: 给出两个数组u,v,每个数组都有n个不同的元素,RMQ(u,l,r)表示u数组中[l,r]区间里面的最小值标号是多少,求一个最大的m ...

  8. 2019牛客多校第一场A-Equivalent Prefixes

    Equivalent Prefixes 传送门 解题思路 先用单调栈求出两个序列中每一个数左边第一个小于自己的数的下标, 存入a[], b[].然后按照1~n的顺序循环,比较 a[i]和b[i]是否相 ...

  9. 2019年牛客多校第一场 I题Points Division 线段树+DP

    题目链接 传送门 题意 给你\(n\)个点,每个点的坐标为\((x_i,y_i)\),有两个权值\(a_i,b_i\). 现在要你将它分成\(\mathbb{A},\mathbb{B}\)两部分,使得 ...

随机推荐

  1. YaoLingJump开发者日志(九)V1.1.1版本完成

    跳跃吧瑶玲下载连接 百度网盘下载 介绍   Android版本升级到8.0,发现原来的图标不显示了:   百度了一下解决方案,用了该博客提供的第二种方法,修改roundIcon的内容.   可爱的图标 ...

  2. PHPcmsv9 还原数据库 操作步骤

    相比dedecms,相同之处:模版好制作,都是开源.不同之处:pc貌似有更好的 负载能力. 言归正传,这两天在捣鼓phpcmsv9程序,但是本地调试好了之后,无论是通过打包方式,还是 转移数据的方式. ...

  3. 从大量的IP访问记录中找到访问次数最多的IP

    1.内存不受限 一个IP有32bit(4Byte),1GB=10亿,那么在4GB内存的情况下,可以存10亿个IP.用HashMap,边存入IP边维护一个最大次数,这样遍历一遍就可以求出,时间复杂度为O ...

  4. 面试中常用排序算法的python实现和性能分析

    这篇是关于排序的,把常见的排序算法和面试中经常提到的一些问题整理了一下.这里面大概有3个需要提到的问题: 虽然专业是数学,但是自己还是比较讨厌繁琐的公式,所以基本上文章所有的逻辑,我都尽可能的用大白话 ...

  5. 【Maven】Snapshot和Release版本的区别

    Snapshot版本代表不稳定.尚处于开发中的版本,快照版本. Release版本则代表稳定的版本,发行版本. 什么时候用Snapshot版本? 依赖库中的jar正处于开发的阶段,会被经常被更新,这种 ...

  6. BZOJ 口胡记录

    最近实在是懒的不想打代码...好像口胡也算一种训练,那就口胡把. BZOJ 2243 染色(树链剖分) 首先树链剖分,然后记录下每个区间的左右端点颜色和当前区间的颜色段.再对每个节点维护一个tag标记 ...

  7. 楼房 洛谷1382 && codevs2995

    P1382 楼房 题目描述 地平线(x轴)上有n个矩(lou)形(fang),用三个整数h[i],l[i],r[i]来表示第i个矩形:矩形左下角为(l[i],0),右上角为(r[i],h[i]).地平 ...

  8. python-输出颜色显示

    显示颜色格式:\033[显示方式;字体色;背景色m...主题内容hello world...\033[0m \033 从这里开始标颜色................................. ...

  9. java 读写文件乱码问题

    这样写,会出现乱码.原因是文件时gbk格式的, BufferedReader br = new BufferedReader(new FileReader(indir)); BufferedWrite ...

  10. UVA.357 Let Me Count The Ways (DP 完全背包)

    UVA.357 Let Me Count The Ways (DP 完全背包) 题意分析 与UVA.UVA.674 Coin Change是一模一样的题.需要注意的是,此题的数据量较大,dp数组需要使 ...