SPOJ3276 D-query
题意:n个数 a1...an,q组询问,每组询问给定 l,r,输出 [ l, r ] 有多少不同的数 ( n ≤30000, q ≤200000, ai ≤ 106 )
离线 + 树状数组维护
#include<bits/stdc++.h> using namespace std; const int MAXN = ;
int n, a[MAXN], start[MAXN], nex[MAXN], q, t[MAXN], an[MAXN], k[MAXN];
pair<int, int> Q[MAXN]; template <typename tn> void read (tn & a) {
tn x = , f = ;
char c = getchar();
while (c < '' || c > ''){ if (c == '-') f = -; c = getchar(); }
while (c >= '' && c <= ''){ x = x * + c - ''; c = getchar(); }
a = f == ? x : -x;
} void add (int k, int d) {
while (k <= n) {
t[k] += d;
k += k & -k;
}
} int sum (int k) {
int s = ;
while (k > ) {
s += t[k];
k -= k & -k;
}
return s;
} bool cmp (const int &i, const int & j) {
return Q[i] < Q[j];
} int main() {
read(n);
for (int i = ; i <= n; ++i) {
read(a[i]);
}
for (int i = n; i >= ; --i) {
if (start[a[i]] != ) add(start[a[i]], -);
add(i, );
nex[i] = start[a[i]];
start[a[i]] = i;
}
read(q);
for (int i = ; i <= q; ++i) {
read(Q[i].first);
read(Q[i].second);
}
for (int i = ; i <= q; ++i) k[i] = i;
sort(k + , k + + q, cmp);
int lx = ;
for (int i = ; i <= q; ++i) {
while (Q[k[i]].first > lx) {
if (sum(lx) - sum(lx - ) > ) {
if (nex[lx] != ) add(nex[lx], );
add(lx, -);
}
++lx;
}
an[k[i]] = sum(Q[k[i]].second) - sum(Q[k[i]].first - );
}
for (int i = ; i <= q; ++i) printf("%d ", an[i]); printf("\n");
return ;
}
SPOJ3276 D-query的更多相关文章
- 以bank account 数据为例,认识elasticsearch query 和 filter
Elasticsearch 查询语言(Query DSL)认识(一) 一.基本认识 查询子句的行为取决于 query context filter context 也就是执行的是查询(query)还是 ...
- 相关query挖掘
1.何为相关query 我通常也把相关query称为相似query,搜索日志中一个用户在短时间内的一系列搜索词被称为相关query.相关就是两个query间有一定的关系,反映了用户在当时的需求.本文就 ...
- 第三篇 Entity Framework Plus 之 Query Cache
离上一篇博客,快一周,工作太忙,只能利用休息日来写一些跟大家分享,Entity Framework Plus 组件系列文章,之前已经写过两篇 第一篇 Entity Framework Plus 之 A ...
- 第二篇 Entity Framework Plus 之 Query Future
从性能的角度出发,能够减少 增,删,改,查,跟数据库打交道次数,肯定是对性能会有所提升的(这里单纯是数据库部分). 今天主要怎样减少Entity Framework查询跟数据库打交道的次数,来提高查询 ...
- FunDA(1)- Query Result Row:强类型Query结果行
FunDA的特点之一是以数据流方式提供逐行数据操作支持.这项功能解决了FRM如Slick数据操作以SQL批次模式为主所产生的问题.为了实现安全高效的数据行操作,我们必须把FRM产生的Query结果集转 ...
- 细谈Slick(6)- Projection:ProvenShape,强类型的Query结果类型
在Slick官方文档中描述:连接后台数据库后,需要通过定义Projection,即def * 来进行具体库表列column的选择和排序.通过Projection我们可以选择库表中部分列.也可以增加一些 ...
- elasticsearch__5__java操作之FilterBuilders构建过滤器Query
FilterBuilders构建过滤器Query 代码如下: package com.elasticsearch; import org.elasticsearch.action.ActionList ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
随机推荐
- 不同路径 II
一个机器人位于一个 m x n 网格的左上角 (起始点标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角. 现在考虑网格中有障碍物.那么从左上角到右下角将会有多 ...
- 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll
阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll 1..BeautifulSoup库的使用 Beautiful ...
- ubantu 重启mysql
如何启动/停止/重启MySQL一. 启动方式 1.使用 service 启动:service mysql start 2.使用 mysqld 脚本启动:/etc/inint.d/mysql start ...
- java线程调度原则
线程的调度采用占先原则,优先级越高的线程越优先执行.每个JAVA线程优先级有设置在常数1-10的范围,默认值是5.但优先级高并不代表能独自占用执行时间片,而是优先级越高得到越多的执行时间片,反之,优先 ...
- 【java】之类加载机制
类加载: 类加载机制是指.class文件加载到jvm,并形成Class对象的机制,之后的应用就可对Class对象进行实例化并调用,类加载机制可在运行时动态加载外部的类,远程网络下载过来的class文件 ...
- awk如何向shell传值
今天写脚本,遇到awk脚本向shell传参的情况,上网谷歌一下,发现都有些麻烦,通过管道,通过eval,感觉都很复杂.于是想到用read来试一下. 首先构造一个测试文件test.txt,里面的内容是1 ...
- 搜索引擎(lucene及周边) 涉及的一些算法总结
一)分词 1)正向/逆向最大匹配算法 典型:IKAnalyzer采用的是正向迭代最细粒度切分算法 IKAnalyzer源码简单分析: http://www.cnblogs.com/huangfox/p ...
- 【Maven】从Maven中导出项目依赖的Jar包
从SVN上下载源代码 svn export https://10.200.1.201/xxxx/PLATFORM code/ --force --username xxx --password xxx ...
- QHBoxLayout移除控件
def clear_layout(widget, layout): buttons = widget.findChildren(QtGui.QPushButton) while layout.item ...
- [UE4]对象
类型和类 1.类型,Type,代表了数据含义,程序员可以对数据进行哪些操作.如果是整数,就可以进行加减乘除:如果是字符串,可以进行打印.连接.但不能对字符串进行加减乘除. 2.类,class,自定义类 ...