题意: 询问区间不同种类颜色数


[2016-11-15]

离线好厉害

对于每一个区间询问,一个数只考虑一次,那么考虑他最后出现的一次

将询问按r排序

从1到n扫描,用树状数组维护一个位置应不应该考虑(记不记入答案),让每种颜色最后一个出现位置贡献

last[x]是x上一个出现的位置,每到一个a[i],last位置-1,i位置+1,并更新last

然后对于所有r==i的询问计算答案

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=5e4+,M=2e5+,INF=1e6+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,a[N],last[INF];
struct data{
int l,r,id,ans;
bool operator <(const data &rhs)const{return r<rhs.r;}
}q[M];
inline bool cmp(data a,data b){return a.id<b.id;}
int c[N];
inline int lowbit(int x){return x&-x;}
inline void add(int p,int v){
for(;p<=n;p+=lowbit(p)) c[p]+=v;
}
inline int sum(int p){
int res=;
for(;p>;p-=lowbit(p)) res+=c[p];
return res;
}
int main(){
n=read();
for(int i=;i<=n;i++) a[i]=read(); m=read();
for(int i=;i<=m;i++) q[i].l=read(),q[i].r=read(),q[i].id=i;
sort(q+,q++m);
int p=;
for(int i=;i<=n;i++){
if(last[a[i]]) add(last[a[i]],-);
add(i,); //printf("%d %d %d %d\n",i,a[i],last[a[i]],p);
last[a[i]]=i;
while(q[p].r==i) q[p].ans=sum(q[p].r)-sum(q[p].l-),p++;
}
sort(q+,q++m,cmp);
for(int i=;i<=m;i++) printf("%d\n",q[i].ans);
}

[2017-01-14]

用主席树做起来直观多了,特别在做了BZOJ3514之后这就是水题啊

last[i]表示i位置的数上一个出现位置,查询区间中last[i]<l的个数,序列建主席树,last权值线段树上就是[0...l-1]的权值和啊

注意权值从0开始注意权值从0开始注意权值从0开始

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lc(x) t[x].l
#define rc(x) t[x].r
const int N=2e5+,MX=1e6+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,Q,x,ql,qr,last[N],pos[MX];
struct node{
int l,r,size;
}t[N*];
int sz,root[N];
void ins(int &x,int l,int r,int p){
t[++sz]=t[x];x=sz;
t[x].size++;
if(l==r) return;
int mid=(l+r)>>;
if(p<=mid) ins(t[x].l,l,mid,p);
else ins(t[x].r,mid+,r,p);
}
int que(int x,int y,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return t[y].size-t[x].size;
else{
int mid=(l+r)>>,ans=;
if(ql<=mid) ans+=que(lc(x),lc(y),l,mid,ql,qr);
if(mid<qr) ans+=que(rc(x),rc(y),mid+,r,ql,qr);
return ans;
}
} int main(){
//freopen("in.txt","r",stdin);
n=read();
for(int i=;i<=n;i++){
x=read();
last[i]=pos[x];
pos[x]=i;
}
for(int i=;i<=n;i++) root[i]=root[i-],ins(root[i],,n,last[i]);//,printf("last %d %d\n",i,last[i]);
Q=read();
while(Q--){
ql=read();qr=read();
printf("%d\n",que(root[ql-],root[qr],,n,,ql-));
}
}

BZOJ1878: [SDOI2009]HH的项链[树状数组+离线 | 主席树]的更多相关文章

  1. BZOJ 3196 Tyvj 1730 二逼平衡树 ——树状数组套主席树

    [题目分析] 听说是树套树.(雾) 怒写树状数组套主席树,然后就Rank1了.23333 单点修改,区间查询+k大数查询=树状数组套主席树. [代码] #include <cstdio> ...

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

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

  3. BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树

    BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排 ...

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

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

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

    P2617 Dynamic Rankings 单点修改,区间查询第k大 当然是无脑树套树了~ 树状数组套主席树就好辣 #include<iostream> #include<cstd ...

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

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

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

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

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

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

  9. Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)

    E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...

随机推荐

  1. python模块-logging的智商上限

    logging,故名肆意就是正在进行日志,我艹,这个文化底蕴! logging是python内置的日志模块,便于日常程序的日志写入和输出 logging共分为5个日志等级,分别是: debug , i ...

  2. npm_一个有意思的npm包

    $ npm install yosay const yosay = require('yosay'); console.log(yosay('Hello, and welcome to my fant ...

  3. python端口扫描

    简易版: #author:Blood_Zero #coding:utf-8 import socket import sys PortList=[21,22,23,25,80,135] # host= ...

  4. Print Numbers by Recursion

    Print numbers from 1 to the largest number with N digits by recursion. Notice It's pretty easy to do ...

  5. gcc编译选项【转】

    转自:https://blog.csdn.net/rheostat/article/details/19811407 常用选项 -E:只进行预处理,不编译-S:只编译,不汇编-c:只编译.汇编,不链接 ...

  6. Linux下MySQL/MariaDB Galera集群搭建过程【转】

    MariaDB介绍 MariaDB是开源社区维护的一个MySQL分支,由MySQL的创始人Michael Widenius主导开发,采用GPL授权许可证. MariaDB的目的是完全兼容MySQL,包 ...

  7. 如何扎实自己的Java基础?

    问:如何扎实自己的Java基础? 答:玩好JDK JDK其实就是Java SE Development Kit的缩写,要玩好这东西可不简单.JDK主要包含了三部分,第一部分就是Java运行时环境,这其 ...

  8. 【h5标签转小程序标签】小程序使用wxParse解析html教程

    一.先下载所需文件,下载地址:https://pan.baidu.com/s/1umZO9uI24zUTRd7VqaWbAg  ,下载完毕后会得到一个wxParse文件夹,后面会用到: 二.先拷贝cs ...

  9. DedeCMS栏目页调用当前栏目名和上级栏目名

    在构建网页的时候,如果不想逐个写栏目列表页的标题,即列表页标题形式为:{field:seotitle/}_{dede:global.cfg_webname/},其中{field:seotitle/}为 ...

  10. mysql数据库主从同步复制原理

    MySQL的Replication(英文为复制)是一个多MySQL数据库做主从同步的方案,特点是异步复制,广泛用在各种对MySQL有更高性能.更高可靠性要求的场合.与之对应的是另一个同步技术是MySQ ...