D-query SPOJ - DQUERY(模板莫队)
题意:
给定一个序列,询问m次,每次求出区间 [ L,R ] 有多少个不同数字。 套模板就好了。。。但我不大明白。。。。我的写法为什么不行。。。唉。。。
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
LL n, m, pos[maxn], s[maxn], c[maxn], ans; struct node
{
LL l, r, id, res;
}Node[maxn]; bool cmp(node a, node b)
{
return pos[a.l] == pos[b.l] ? (a.r < b.r) : (a.l < b.l);
} bool cmp_id(node a, node b)
{
return a.id < b.id;
} //void update(int k, int add)
//{
// if(add == 1) s[c[k]]++;
// else s[c[k]]--;
// if(s[c[k]] == 1)
// ans++;
// else if(s[c[k]] == 0)
// ans--;
//} void add(int x){
s[c[x]]++;
if(s[c[x]]==) ans++;
} void del(int x){
s[c[x]]--;
if(s[c[x]]==) ans--;
} int main()
{
ans = ;
scanf("%lld", &n);
for(int i=; i<=n; i++)
scanf("%lld", &c[i]);
int block = sqrt(n);
for(int i=; i<=n; i++)
pos[i] = (i-)/block + ;
scanf("%lld", &m);
for(int i=; i<=m; i++)
{
scanf("%lld%lld", &Node[i].l, &Node[i].r);
Node[i].id = i;
}
sort(Node+, Node++m, cmp);
for(int i=, l=, r=; i<=m; i++)
{
for(; r < Node[i].r; r++)
add(r+);
for(; r > Node[i].r; r--)
del(r);
for(; l < Node[i].l; l++)
del(l);
for(; l > Node[i].l; l--)
add(l-);
Node[i].res = ans;
}
sort(Node+, Node++m, cmp_id);
for(int i=; i<=m; i++)
printf("%I64d\n", Node[i].res); return ;
}
D-query SPOJ - DQUERY(模板莫队)的更多相关文章
- SPOJ D-query(莫队算法模板)
题目链接:http://www.spoj.com/problems/DQUERY/ 题目大意:给定一个数组,每次询问一个区间内的不同元素的个数 解题思路:直接套莫队的裸题 #include<cs ...
- (原创)D-query SPOJ - DQUERY(莫队)统计不同数的数量
A - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a ...
- D-query SPOJ - DQUERY(莫队)统计不同数的数量
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...
- D-query SPOJ - DQUERY (莫队算法裸题)
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...
- luogu P4887 模板 莫队二次离线 莫队 离线
LINK:模板莫队二次离线 很早以前学的知识点 不过 很久了忘了. 考虑暴力 :每次莫队更新的时候 尝试更新一个点到一个区间的答案 可以枚举二进制下位数为k的数字 看一下区间内的这种数字有多少个. 不 ...
- spoj COT2(树上莫队)
模板.树上莫队的分块就是按dfn分,然后区间之间转移时注意一下就好.有个图方便理解http://blog.csdn.net/thy_asdf/article/details/47377709: #in ...
- BZOJ 2038 2009国家集训队 小Z的袜子【模板·莫队】
[题解] 1,先说说莫队算法. 莫队算法是用来离线处理区间问题的算法.非常易于理解和使用,且运用十分广泛. 假设我们现在已知区间[L,R]的答案,如果我们能以较低的时间复杂度扩展得到区间$[L-1,R ...
- 洛谷 P2709 小B的询问(莫队)
题目链接:https://www.luogu.com.cn/problem/P2709 这道题是模板莫队,然后$i$在$[l,r]$区间内的个数就是$vis[ ]$数组 $add()$和$del()$ ...
- SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)
DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...
随机推荐
- netty+proto使用简要记录
1. maven环境配置protobuf 2.生成.proto文件 3.将.proto转为java文件 打开电脑的cmd,进入.proto所在文件位置,输入命令:protoc.exe --java_o ...
- Windows下Redis集群搭建
1.第一步先安装Redis 参照<Windows下Redis安装及使用.docx> 在Redis目录E:/Redis下新建Logs文件夹,并且创建3个端口下的配置文件,记得修改里面的接口 ...
- EventBus的基本使用步骤
为什么要使用EventBus 当我们进行项目开发的时候,往往是需要应用程序的各组件间进行通信,比如在子线程中进行请求数据,当数据请求完毕后通过Handler或者是广播通知UI, 通常两个Activit ...
- v-if、v-show 指令
HTML部分: <div id="app"> <button type="button" @click="flag=!flag&qu ...
- Netty源码分析第5章(ByteBuf)---->第2节: ByteBuf的分类
Netty源码分析第五章: ByteBuf 第二节: ByteBuf的分类 上一小节简单介绍了AbstractByteBuf这个抽象类, 这一小节对其子类的分类做一个简单的介绍 ByteBuf根据不同 ...
- DockerCon2017前瞻 - Docker企业版体验
DockerCon 2017将于四月17号在美国Austin召开.在去年DockerCon上,Docker公司一系列的发布吹响了进军企业市场的号角.今天,容器技术已经愈发成熟,被越来越多的企业所关注和 ...
- CF 1095C Powers Of Two(二进制拆分)
A positive integer xx is called a power of two if it can be represented as x=2y, where y is a non-ne ...
- PSP Daily软件beta版本——基于spec评论
题目要求: 每个小组评论其他小组beta发布作品的软件功能说明书. 试用(并截图)所有其他小组的beta作品,与软件功能说明书对比,评论beta作品对软件功能说明书的实现. 根据软件功能说明书,测试所 ...
- 第十一次PSP
- Linux系统(X32)安装Oracle11g完整安装图文教程另附基本操作
一.修改操作系统核心参数 在Root用户下执行以下步骤: )修改用户的SHELL的限制,修改/etc/security/limits.conf文件 输入命令:vi /etc/security/limi ...