分析:这个题,离线的话就是水题,如果强制在线,其实和离线一个思路,然后硬上主席树就行了

离线的代码

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1e6+;
const int maxn= 3e4+;
const int INF=0x3f3f3f3f;
typedef unsigned long long ULL;
typedef long long LL;
int n,a[maxn],pre[maxn],mp[N],res[maxn*],q;
struct Que{
int l,r,id;
bool operator<(const Que &rhs)const{
return r<rhs.r;
}
}o[maxn*]; int c[maxn];
void add(int x,int t){
for(int i=x;i<=n;i+=i&(-i))
c[i]+=t;
}
int get(int x){
int ans=;
for(int i=x;i>;i-=i&(-i))
ans+=c[i];
return ans;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;++i){
scanf("%d",&a[i]);
pre[i]=mp[a[i]];
mp[a[i]]=i;
}
scanf("%d",&q);
for(int i=;i<=q;++i)
scanf("%d%d",&o[i].l,&o[i].r),o[i].id=i;
sort(o+,o++q);
int cnt=;
for(int i=;i<=q;++i){
for(;cnt<=o[i].r;++cnt){
if(pre[cnt])add(pre[cnt],-);
add(cnt,);
}
res[o[i].id]=get(o[i].r)-get(o[i].l-);
}
for(int i=;i<=q;++i)printf("%d\n",res[i]);
return ;
}

在线,其实感觉比离线好写呢。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1e6+;
const int maxn= 3e4+;
const int INF=0x3f3f3f3f;
typedef unsigned long long ULL;
typedef long long LL;
int n,mp[N],q;
struct Node{
int l,r,v;
}o[*maxn];
int root[maxn],sz;
void update(int &rt,int l,int r,int pos,int t){
o[++sz]=o[rt],rt=sz;
o[rt].v+=t;
if(l==r)return;
int m=(l+r)>>;
if(pos<=m)update(o[rt].l,l,m,pos,t);
else update(o[rt].r,m+,r,pos,t);
}
int query(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y)
return o[rt].v;
int m=(l+r)>>;
int ans=;
if(x<=m)ans+=query(o[rt].l,l,m,x,y);
if(y>m)ans+=query(o[rt].r,m+,r,x,y);
return ans;
}
int main(){
scanf("%d",&n);
root[]=sz=;
for(int i=;i<=n;++i){
int x;
scanf("%d",&x);
if(mp[x]){
int tmp;
update(tmp=root[i-],,n,mp[x],-);
update(root[i]=tmp,,n,i,);
}
else update(root[i]=root[i-],,n,i,);
mp[x]=i;
}
scanf("%d",&q);
for(int i=;i<=q;++i){
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",query(root[r],,n,l,r));
}
return ;
}

SPOJ3267 D-query 离线+树状数组 在线主席树的更多相关文章

  1. hdu 3333 离线线段树 + 思维/树状数组 /在线主席树

    #include<iostream> #include<cstdio> #include<string> #include<cmath> #includ ...

  2. BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树

    [题目分析] BZOJ这个题目抄的挺霸气. 主席树是第一时间想到的,但是修改又很麻烦. 看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树. 学到了新的姿势,2333o(* ̄▽ ̄*)ブ [代 ...

  3. ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解

    题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...

  4. [COGS257]动态排名系统 树状数组套主席树

    257. 动态排名系统 时间限制:5 s   内存限制:512 MB [问题描述]给定一个长度为N的已知序列A[i](1<=i<=N),要求维护这个序列,能够支持以下两种操作:1.查询A[ ...

  5. BZOJ 2141 排队(树状数组套主席树)

    解法很多的题,可以块套树状数组,可以线段树套平衡树.我用的是树状数组套主席树. 题意:给出一段数列,m次操作,每次操作是交换两个位置的数,求每次操作后的逆序对数.(n,m<=2e4). 对于没有 ...

  6. 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】

    题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...

  7. 【Luogu】P2617Dynamic Ranking(树状数组套主席树)

    题目链接 树状数组套主席树有点难懂qwq 不好理解 树状数组套主席树的直观理解应该是:树状数组的每一个节点是一棵主席树. 普通区间修改我们是创建1个线段树,树状数组套主席树的时候我们就创建log个线段 ...

  8. [BZOJ3932] [CQOI2015]任务查询系统(主席树 || 树状数组 套 主席树 + 差分 + 离散化)

    传送门 看到这个题有个很暴力的想法, 可以每一个时间点都建一颗主席树,主席树上叶子节点 i 表示优先级为 i 的任务有多少个. 当 x 到 y 有个优先级为 k 的任务时,循环 x 到 y 的每个点, ...

  9. LUOGU P2617 Dynamic Rankings(树状数组套主席树)

    传送门 解题思路 动态区间第\(k\)大,树状数组套主席树模板.树状数组的每个位置的意思的是每棵主席树的根,维护的是一个前缀和.然后询问的时候\(log\)个点一起做前缀和,一起移动.时空复杂度\(O ...

随机推荐

  1. vim插件:latex-suite 使用方法

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4030057.html 零.操作快捷键:对于<++>的块,按下ctrl+j即可快速 ...

  2. 基于Hadoop生态圈的数据仓库实践 —— ETL

    使用Hive转换.装载数据 1. Hive简介 (1)Hive是什么         Hive是一个数据仓库软件,使用SQL读.写.管理分布式存储上的大数据集.它建立在Hadoop之上,具有以下功能和 ...

  3. http通信协议详解

    转载自:http://blog.csdn.net/gueter/article/details/1524447 引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒 ...

  4. Magento Api 记录

    magento api 首次接触 (-) /** * magento Api 身份验证 调用示例 * Example of simple product POST using Admin accoun ...

  5. js日期范围初始化,得到前一个月的日期

    今天做时间范围的初始化设定,开始时间是当前时间的前一个月,终于找到完美的解决方案了. Date.prototype.format = function(format) {     var o = { ...

  6. Win8.1设置窗口背景颜色为护眼色

    更改注册表 window+R --->输入regedit(点击确定后进入注册表编辑器) 需要修改以下两个地方,重启电脑生效: [HKEY_CURRENT_USER\Control Panel\C ...

  7. 解决npm安装模块时 npm err! registry error parsing json

    最近还真是点背,从yeoman生成一个react项目或是github上克隆一个项目,在npm install的时候, 一直报npm err! registry error parsing json(就 ...

  8. Pair project(刘昊岩11061156 黄明源11061186)

    Pair project members:刘昊岩11061156,黄明源11061186 两周时间,工程下午刚刚结束,现做一些总结. 在现有工程基础上修改schedule 包下方法,主要思想是,也就是 ...

  9. 微软Hololens学院教程-Hologram 211-Gestures(手势)【微软教程已经更新,本文是老版本】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

  10. C#<热血传奇>服务端源代码再次给力更新

    前段时间一直在忙公司项目,最近抽点空稍微把部分代码重新整理一下(代码太久没碰很多地方都忘记了 囧~~~) 此次还是更新服务端,并修正上一版里面存在的很多指针 异常BUG...  (代码比较烂,还请各位 ...