题意: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的更多相关文章

  1. 以bank account 数据为例,认识elasticsearch query 和 filter

    Elasticsearch 查询语言(Query DSL)认识(一) 一.基本认识 查询子句的行为取决于 query context filter context 也就是执行的是查询(query)还是 ...

  2. 相关query挖掘

    1.何为相关query 我通常也把相关query称为相似query,搜索日志中一个用户在短时间内的一系列搜索词被称为相关query.相关就是两个query间有一定的关系,反映了用户在当时的需求.本文就 ...

  3. 第三篇 Entity Framework Plus 之 Query Cache

    离上一篇博客,快一周,工作太忙,只能利用休息日来写一些跟大家分享,Entity Framework Plus 组件系列文章,之前已经写过两篇 第一篇 Entity Framework Plus 之 A ...

  4. 第二篇 Entity Framework Plus 之 Query Future

    从性能的角度出发,能够减少 增,删,改,查,跟数据库打交道次数,肯定是对性能会有所提升的(这里单纯是数据库部分). 今天主要怎样减少Entity Framework查询跟数据库打交道的次数,来提高查询 ...

  5. FunDA(1)- Query Result Row:强类型Query结果行

    FunDA的特点之一是以数据流方式提供逐行数据操作支持.这项功能解决了FRM如Slick数据操作以SQL批次模式为主所产生的问题.为了实现安全高效的数据行操作,我们必须把FRM产生的Query结果集转 ...

  6. 细谈Slick(6)- Projection:ProvenShape,强类型的Query结果类型

    在Slick官方文档中描述:连接后台数据库后,需要通过定义Projection,即def * 来进行具体库表列column的选择和排序.通过Projection我们可以选择库表中部分列.也可以增加一些 ...

  7. elasticsearch__5__java操作之FilterBuilders构建过滤器Query

    FilterBuilders构建过滤器Query 代码如下: package com.elasticsearch; import org.elasticsearch.action.ActionList ...

  8. [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 ...

  9. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  10. [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 ...

随机推荐

  1. Request.QueryString()和Request()和Request.Form();

    一. querystring的用法及原理 当页面上的 FORM以 GET方式向页面发送请求数据 (如数据含有不安全字符,则浏览器先将其转换成 16进制的字符再传送,如空格被转成 %20)时, WEB ...

  2. centos7 使用cgroup进行资源限制

    centos7中进行资源限制,使用的仍然是cgroup,只是配置接口使用的systemd. 下文将介绍如何使用systemd进行资源限制. Step1 编写unit文件 命令为my-demo.serv ...

  3. Hbase 简介+环境安装配置教程

    HBase介绍 HBase是参考google的bigtable的一个开源产品,建立在hdfs之上的一个提供高可靠性.高性能.列存储.可伸缩.实时读写的数据库系统.是一种介于nosql和RDBMs之间的 ...

  4. SQL 行列转置

    ),),[Score] int) Insert Class union all union all union all union all union all union all union all ...

  5. 编译NDK的source code一定要用release mode!

    编译NDK的source code一定要用release mode! 编译NDK的source code一定要用release mode! 编译NDK的source code一定要用release m ...

  6. 写了一个RenderInBackground的脚本

    某主管希望write节点有多线程渲染的功能,而nuke中的render in background功能恰恰可以多个渲染任务同时执行,于是我考虑使用这个方法来实现. 调 试过程中发现renderinba ...

  7. 【巷子】---redux---【react】

    一.flux的缺陷 因为dispatcher和Store可以有多个互相管理起来特别麻烦 二.什么是redux 其实redux就是Flux的一种进阶实现.它是一个应用数据流框架,主要作用应用状态的管理 ...

  8. jquery 中prop和 attr

    prop就是给html中元素固有的属性赋值 而attr是给元素定义新的属性值.

  9. Centos7 安装sz,rz命令

    yum install lrzsz 我记得以前某个我敬佩的人说过压缩分很多种,有空,补充这篇笔记.加油~

  10. STL基础--算法(已排序数据的算法,数值算法)

    已排序数据的算法 Binary search, merge, set operations 每个已排序数据算法都有一个同名的更一般的形式 vector vec = {8,9,9,9,45,87,90} ...