LightOJ 1188 Fast Queries(简单莫队)
| Time Limit: 3 second(s) | Memory Limit: 64 MB |
Given an array of N integers indexed from 1 to N, and q queries, each in the form i j, you have to find the number of distinct integers from index i to j (inclusive).
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
The first line of a case is a blank line. The next line contains two integers N (1 ≤ N ≤ 105), q (1 ≤ q ≤ 50000). The next line contains N space separated integers forming the array. There integers range in [0, 105].
Each of the next q lines will contain a query which is in the form i j (1 ≤ i ≤ j ≤ N).
Output
For each test case, print the case number in a single line. Then for each query you have to print a line containing number of distinct integers from index i to j.
Sample Input |
Output for Sample Input |
|
1 8 5 1 1 1 2 3 5 1 2 1 8 2 3 3 6 4 5 4 8 |
Case 1: 4 1 4 2 4 |
Note
Dataset is huge. Use faster I/O methods.
题目链接:LighOJ 1188
莫队大法好,看到n的范围是1e5也就直接上莫队了,用vis记录数字出现次数,显然对于区间的增减进行更新vis,若缩小区间导致vis[val]为0则说明少了一个数,反之为1则就是增加一个数,700MS还可以,另外一种不同的做法是用树状数组更新统计。
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
const int M=50010;
struct info
{
int l,r;
int id,b;
bool operator<(const info &t)const
{
if(b==t.b)
return r<t.r;
return b<t.b;
}
};
info Q[M];
int arr[N];
int vis[N];
int ans[M];
int main(void)
{
int tcase,i,j;
int n,m,l,r;
scanf("%d",&tcase);
for (int q=1; q<=tcase; ++q)
{
scanf("%d%d",&n,&m);
for (i=1; i<=n; ++i)
scanf("%d",&arr[i]);
int unit=(int)sqrt(n);
for (i=1; i<=m; ++i)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].id=i;
Q[i].b=Q[i].l/unit;
}
sort(Q+1,Q+1+m);
CLR(vis,0);
int L=1,R=0;
int temp=0;
for (i=1; i<=m; ++i)
{
while (L>Q[i].l)
{
--L;
++vis[arr[L]];
if(vis[arr[L]]==1)
++temp;
}
while (L<Q[i].l)
{
--vis[arr[L]];
if(vis[arr[L]]==0)
--temp;
++L;
}
while (R>Q[i].r)
{
--vis[arr[R]];
if(vis[arr[R]]==0)
--temp;
--R;
}
while (R<Q[i].r)
{
++R;
++vis[arr[R]];
if(vis[arr[R]]==1)
++temp;
}
ans[Q[i].id]=temp;
}
printf("Case %d:\n",q);
for (i=1; i<=m; ++i)
printf("%d\n",ans[i]);
}
return 0;
}
LightOJ 1188 Fast Queries(简单莫队)的更多相关文章
- Strange Queries(莫队)
题目 You are given an array with n integers a1, a2, ..., an, and q queries to answer. Each query consi ...
- CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)
You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...
- CF 375D. Tree and Queries【莫队 | dsu on tree】
题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...
- BZOJ 1878 hh的项链(简单莫队)
Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一 段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此他的项链变得 ...
- 【 Gym - 101138D 】Strange Queries (莫队算法)
BUPT2017 wintertraining(15) #4B Gym - 101138D 题意 a数组大小为n.(1 ≤ n ≤ 50 000) (1 ≤ q ≤ 50 000)(1 ≤ ai ≤ ...
- BZOJ 3339 && luogu4137 Rmq Problem / mex(莫队)
P4137 Rmq Problem / mex 题目描述 有一个长度为n的数组{a1,a2,-,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. 输入输出格式 输入格式: 第一行n,m. ...
- 洛谷 P3901 数列找不同(莫队)
题目链接:https://www.luogu.com.cn/problem/P3901 这道题简单莫队模板题,然后$add$和$del$分别处理$vis[]$从$0-->1$和从$1--> ...
- Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)
vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...
- Gym101138D Strange Queries/BZOJ5016 SNOI2017 一个简单的询问 莫队、前缀和、容斥
传送门--Gym 传送门--BZOJ THUWC2019D1T1撞题可还行 以前有些人做过还问过我,但是我没有珍惜,直到进入考场才追悔莫及-- 设\(que_{i,j}\)表示询问\((1,i,1,j ...
随机推荐
- Emacs 24.3 配置JDEE(http://blog.csdn.net/csfreebird/article/details/19033939)
最近要重回Java编程,所以打算在最新版本的Emacs 24.3上配置JDEE,听说会有些问题,特此记录安装过程. Emacs 24.3内置了CEDET, 版本是2.0, 这是一个让人困惑的事情,因为 ...
- SpringJDBC的简单应用
此处写上应用JdbcTemplate的dao操作数据库的一些代码(含基本的增删改查,注:重点是查询出多条语句的写法): package org.sakaiproject.zhaorui.dao.imp ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)
Problem A: The 3n + 1 problem Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 14 Solved: 6[Submit][St ...
- C#学习笔记(四)——变量的更多内容
一.类型转换 1.转换的类型 2.隐式转换 bool 和string 没有隐式转换,具有隐式转换的都列在下面的表格 . 记住一个规律,就是由精度低的类型转到精度高的类型是很容易的. 3.显式转换 (1 ...
- SQL Server SA 密码丢失无法连接数据库怎么办?
如果Windows账户无法连接并且SA密码也丢失了,那么如何可以连接到数据库呢? 答案是: 在单用户模式下启动SQL Server然后用本地管理员权限连接.登陆之后就可以修改SA密码了. 步骤: 1. ...
- ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component解决
第一:先确定一下开发流程是否正确 1.写好servlet组件类 2.写好web.xml文件--向服务器介绍组件 3.发布--就是拷贝 注意:要拷贝包结构,不要只拷贝组件类文件 另外,拷贝的是.clas ...
- Android SeekBar自定义使用图片和颜色显示
案例使用的图片如下: 1.在res/drawable目录下新增一个xml风格文件,seekbar_define_style.xml ? 1 2 3 ...
- 转 BHO API HOOK Wininet基于IE编程的一些资料
BHO原理:推荐vc base中的文章: 如何使用BHO定制你的Internet Explorer浏览器 API HOOK的基本原理:推荐C++ builder 研究中的文章: API Hook基 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- HazelCast 的内存管理原理
As it is said in the recent article "Google: Taming the Long Latency Tail - When More Machines ...
