2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]
题目链接:https://www.nowcoder.com/acm/contest/139/J
题目描述
输入描述:
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.
输入
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.
题意:
给出序列a[1:n],给出q个查询,每个查询包含(l,r),要求回答a[1:l]和a[r:n]合起来有多少不同的整数。
题解:
莫队算法。
统计a[1:n]有多少不同的整数,记为sum,所有 r - l ≤ 1 的查询答案为sum,
cnt[k]表示整数k出现了几次,sum的变动根据cnt[k]的0→1以及1→0变化判断。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+;
const int MAXQ=1e5+; struct Query
{
int block;
int id;
int l,r;
bool operator <(const Query &oth) const
{
return (this->block == oth.block) ? (this->r < oth.r) : (this->block < oth.block);
}
}query[MAXQ]; int n,q;
int sum;
int num[MAXN],cnt[MAXN];
int ans[MAXQ]; int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
memset(cnt,,sizeof(cnt));
sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&num[i]);
if(cnt[num[i]]==) sum++;
cnt[num[i]]++;
} int len=sqrt(n);
for(int i=;i<=q;i++)
{
scanf("%d%d",&query[i].l,&query[i].r);
if(query[i].r-query[i].l<=) ans[i]=sum;
query[i].block=query[i].l/len;
query[i].id=i;
}
sort(query+,query+q+); int pl=,pr=;
for(int i=;i<=q;i++)
{
if(query[i].r-query[i].l<=) continue; if(pr<query[i].r)
{
for(int j=pr;j<query[i].r;j++)
{
cnt[num[j]]--;
if(cnt[num[j]]==) sum--;
}
}
if(pr>query[i].r)
{
for(int j=pr-;j>=query[i].r;j--)
{
if(cnt[num[j]]==) sum++;
cnt[num[j]]++;
}
}
if(pl<query[i].l)
{
for(int j=pl+;j<=query[i].l;j++)
{
if(cnt[num[j]]==) sum++;
cnt[num[j]]++;
}
}
if(pl>query[i].l)
{
for(int j=pl;j>query[i].l;j--)
{
cnt[num[j]]--;
if(cnt[num[j]]==) sum--;
}
} ans[query[i].id]=sum; pl=query[i].l;
pr=query[i].r;
} for(int i=;i<=q;i++)
{
printf("%d\n",ans[i]);
}
}
}
2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]的更多相关文章
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...
- 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)
Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...
- 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)
链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...
- 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)
链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...
- 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)
题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...
- 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)
题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...
- 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]
题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]
题目链接:https://www.nowcoder.com/acm/contest/143/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
随机推荐
- Spring JDBC入门
Spring将替我们完成所有使用JDBC API进行开发的单调乏味的.底层细节处理工作. 操作JDBC时Spring可以帮我们做这些事情: 定义数据库连接参数,打开数据库连接,处理异常,关闭数据库连接 ...
- PHP 源码加密模块 php-beast
PHP Beast是一个源码加密模块,使用这个模块可以把PHP源码加密并在此模块下运行. 为什么要用PHP-Beast? 有时候我们的代码会放到代理商上, 所以很有可能代码被盗取, 或者我们写了一个商 ...
- CreateProcess函数详解
在windows程序设计五中有详解 CreateProcess 说明: WIN32API函数CreateProcess用来创建一个新的进程和它的主线程,这个新进程运行指定的可执行文件. 函数原型: B ...
- pycharm 激活
方法1: (1)更新**hosts**文件 [hosts文件百度云下载地址](https://pan.baidu.com/s/1o9ZujxS) **hosts**文件在windows中的地址为: C ...
- Redis 未授权访问漏洞(附Python脚本)
0x01 环境搭建 #下载并安装 cd /tmp wget http://download.redis.io/releases/redis-2.8.17.tar.gz tar xzf redis-.t ...
- mybatis 之 resultType="Integer"
public class EcPromoteRuleAdditionalNew extends BaseBO { private String[] promoteRuleIds; public Str ...
- jinja2主要语法
jinja2主要语法 1.变量 {{name}} 2.控制语句 {% if %} {{name}} {% else %} {{name2}} {% endif%} 3.宏 {% macro check ...
- 开启mysql远程连接访问权限的几种方法
1.改表法. 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 " ...
- iText7生成pdf
1 官网 http://developers.itextpdf.com/itext-java 2 form中加入表格 http://developers.itextpdf.com/content/be ...
- 批处理--批量打开程序&批量关闭程序
批量打开程序 start D:\work\RunSvr01\IceFire88.01.exe start D:\work\RunSvr02\IceFire88.02.exe start D:\work ...