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 ...
随机推荐
- iphone越狱安装python2.7
cydia 添加源地址:http://apt.so/whitefur 选择python 进行安装 打开ssh连接后输入python 显示python2.7.3 安装成功
- C/S,B/S的应用区别
C/S,B/S的应用区别 C/S即大家熟知的客服机和服务器结构通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销 B/S结构即浏览器和服务 ...
- Windows Azure Storage (24) 启用Azure Blob日志
<Windows Azure Platform 系列文章目录> 之前有一个业务需求,客户想知道Azure Storage是否有日志功能,可以检查某个Azure Blob文件在某个时间点被删 ...
- Django--django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE
Django序列化时遇到错误: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESP ...
- 胖子哥的大数据之路(10)- 基于Hive构建数据仓库实例
一.引言 基于Hive+Hadoop模式构建数据仓库,是大数据时代的一个不错的选择,本文以郑商所每日交易行情数据为案例,探讨数据Hive数据导入的操作实例. 二.源数据-每日行情数据 三.建表脚本 C ...
- 测试教程网.unittest教程.4. 实例: 读取测试数据并测试弱密码
From: http://www.testclass.net/pyunit/test_example_2/ 背景 接上一节的弱密码例子,我们的用例尽管运行的不错,但还是有点问题. 假如我们需要增加一些 ...
- Importing multi-valued field into Solr from mySQL using Solr Data Import Handler
http://stackoverflow.com/questions/20233837/importing-multi-valued-field-into-solr-from-mysql-using- ...
- Elasticsearch 5.2.x 使用 Head 插件连接不上集群
如果访问elasticsearch出现跨域的问题,如下: 修改elasticsearch.yml文件 vim $ES_HOME$/config/elasticsearch.yml # 增加如下字段 h ...
- dfs常见的配置文件中的value与description(重要)
不多说,直接上干货! name value description dfs.namenode.logging.level info The logging level for dfs namenode ...
- C#对Mongodb数组对象操作
Mongo对数据的存储非常随意,需要修改对象中的数组对象时,就会变得比较复杂. 类中的类对象可以直接通过“.”例如:Department.User.name 类中的对象User是数组时可以用Depar ...