SPOJ3267 D-query 离线+树状数组 在线主席树
分析:这个题,离线的话就是水题,如果强制在线,其实和离线一个思路,然后硬上主席树就行了
离线的代码
#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 离线+树状数组 在线主席树的更多相关文章
- hdu 3333 离线线段树 + 思维/树状数组 /在线主席树
#include<iostream> #include<cstdio> #include<string> #include<cmath> #includ ...
- BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树
[题目分析] BZOJ这个题目抄的挺霸气. 主席树是第一时间想到的,但是修改又很麻烦. 看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树. 学到了新的姿势,2333o(* ̄▽ ̄*)ブ [代 ...
- ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解
题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...
- [COGS257]动态排名系统 树状数组套主席树
257. 动态排名系统 时间限制:5 s 内存限制:512 MB [问题描述]给定一个长度为N的已知序列A[i](1<=i<=N),要求维护这个序列,能够支持以下两种操作:1.查询A[ ...
- BZOJ 2141 排队(树状数组套主席树)
解法很多的题,可以块套树状数组,可以线段树套平衡树.我用的是树状数组套主席树. 题意:给出一段数列,m次操作,每次操作是交换两个位置的数,求每次操作后的逆序对数.(n,m<=2e4). 对于没有 ...
- 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】
题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...
- 【Luogu】P2617Dynamic Ranking(树状数组套主席树)
题目链接 树状数组套主席树有点难懂qwq 不好理解 树状数组套主席树的直观理解应该是:树状数组的每一个节点是一棵主席树. 普通区间修改我们是创建1个线段树,树状数组套主席树的时候我们就创建log个线段 ...
- [BZOJ3932] [CQOI2015]任务查询系统(主席树 || 树状数组 套 主席树 + 差分 + 离散化)
传送门 看到这个题有个很暴力的想法, 可以每一个时间点都建一颗主席树,主席树上叶子节点 i 表示优先级为 i 的任务有多少个. 当 x 到 y 有个优先级为 k 的任务时,循环 x 到 y 的每个点, ...
- LUOGU P2617 Dynamic Rankings(树状数组套主席树)
传送门 解题思路 动态区间第\(k\)大,树状数组套主席树模板.树状数组的每个位置的意思的是每棵主席树的根,维护的是一个前缀和.然后询问的时候\(log\)个点一起做前缀和,一起移动.时空复杂度\(O ...
随机推荐
- jQuery 扩展 【ajax实例】
先前写工具类都是自定义类,直接prototype,jQuery扩展这一块儿,一直也没写过,刚好今天有空,写个试试. 已经有很多人对jQuery,jQuery.fn,jQuery.fn.extend详细 ...
- Java Web开发之Servlet、JSP基础
有好多年不搞Java Web开发了,这几天正好国庆放假,放松之余也有兴趣回头看看Java Web开发技术的基础. 我们都知道,Servlet是Java Web开发的重要基础,但是由于Servlet开发 ...
- display:block; 块级元素。<a>,<span>标签设置宽度和高度
display:block;是让对象成为块级元素(比如a,span等) 转化后 可以对a或者span标签进行width和height设置,否则设置不了 display有很多对象,具体可以参考http: ...
- DataGridView 绑定数据后修改列类型
dataGridView1.DataSource = dt; dataGridView1.Columns.RemoveAt(); //删除要被替换的列 DataGridViewLinkColumn l ...
- 今日吐槽20151208.net服务器控件
正文 今天有个小任务是给页面添加一个搜索条件,复选框多选.因为页面都是服务器控件,我也只能用服务器控件配合了.然后给页面加了一个 CheckBoxList 控件.后台通过数据表加载数据. fore ...
- PHPCMS二次开发教程(转)
转自:http://www.cnblogs.com/semcoding/p/3347600.html PHPCMS V9 结构设计 根目录 |–api 结构文件目录 |–caches 缓存文件目录 ...
- VM 启动时报错:Failed to lock the file
http://www.cnblogs.com/kristain/articles/2491966.html Reason: Failed to lock the fileGoogle 了一下, 在網路 ...
- button的相关属性
设置自定义按钮的文字大小 [submit.titleLabel setFont:[UIFont boldSystemFontOfSize:16]]; 设置按钮选中状态的颜色 [btn setTintC ...
- Almeza MultiSet Pro(批量安装程序) V8.7.6中文特别版
Almeza MultiSet Pro(批量安装程序)是一款非常实用的工具.它能够帮你批量地安装常用的软件.这将解决每次重装系统后能够快速方便地重装常用软件.使用这款软件不需要编写程序,还可以在安装过 ...
- [转载]MongoDB开发学习(1)开天辟地,经典入门
原文地址:http://www.cnblogs.com/xumingxiang/archive/2012/04/08/2437468.html 如果你从来没有接触MongoDB或对MongoDB有一点 ...