题面

传送门

分析

首先我们观察到区间范围较大,而区间个数较少,考虑离散化,将所有询问按照右端点进行排序

离散化之后研究区间颜色个数变化的规律

当我们处理到第a[i]个段时,设a[i]上一次出现的地方为last[a[i]],则last[a[i]]之前的颜色出现次数不受影响.

首先遍历右端点范围r,用线段树维护每一位到r的区间中有多少种颜色,记为c

则处理到第a[i]个段时,将(last[a[i]],i]区间+1

如r=6时

a={3,1,5,2,4,1}

处理第6位之前c={5,4,3,2,1,0}

last[a[6]]=2,然后将区间(2,6]+1

c={5,5,4,3,2,1}

此时,在询问中找右端点为r的询问[l,r],答案即为c[l]

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 50005
#define maxm 200005
using namespace std;
int n,m;
struct node{//标准的线段树
int l;
int r;
int mark;
int v;
int len(){
return r-l+1;
}
}tree[maxn<<2];
void push_up(int pos){
tree[pos].v=tree[pos<<1].v+tree[pos<<1|1].v;
}
void build(int l,int r,int pos){
tree[pos].l=l;
tree[pos].r=r;
tree[pos].mark=0;
tree[pos].v=0;
if(l==r){
tree[pos].v=0;
return;
}
int mid=(l+r)>>1;
build(l,mid,pos<<1);
build(mid+1,r,pos<<1|1);
push_up(pos);
}
void push_down(int pos){
if(tree[pos].mark){
tree[pos<<1].mark+=tree[pos].mark;
tree[pos<<1|1].mark+=tree[pos].mark;
tree[pos<<1].v+=tree[pos].mark*tree[pos<<1].len();
tree[pos<<1|1].v+=tree[pos].mark*tree[pos<<1|1].len();
tree[pos].mark=0;
}
}
void update(int L,int R,int v,int pos){
if(L<=tree[pos].l&&R>=tree[pos].r){
tree[pos].v+=v*tree[pos].len();
tree[pos].mark+=v;
return;
}
push_down(pos);
int mid=(tree[pos].l+tree[pos].r)>>1;
if(L<=mid) update(L,R,v,pos<<1);
if(R>mid) update(L,R,v,pos<<1|1);
push_up(pos);
}
int query(int L,int R,int pos){
if(L<=tree[pos].l&&R>=tree[pos].r){
return tree[pos].v;
}
push_down(pos);
int mid=(tree[pos].l+tree[pos].r)>>1;
int ans=0;
if(L<=mid) ans+=query(L,R,pos<<1);
if(R>mid) ans+=query(L,R,pos<<1|1);
return ans;
}
int bin_search(int l,int r,int x,int *a){//二分查找,离散化用
int mid;
while(l<=r){
mid=(l+r)>>1;
if(a[mid]==x) return mid;
else if(a[mid]>x) r=mid-1;
else l=mid+1;
}
return -1;
}
int a[maxn],b[maxn];
int last[maxn];
struct ask{
int l;
int r;
int i;
}q[maxm];
int ans[maxm];
int cmp(ask x,ask y){
return x.r==y.r?x.l<y.l:x.r<y.r;//按右端点排序
}
int main(){
int l,r;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+1,b+1+n);
int n2=unique(b+1,b+1+n)-b-1;
for(int i=1;i<=n;i++){
a[i]=bin_search(1,n2,a[i],b);
}
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%d %d",&q[i].l,&q[i].r);
q[i].i=i;
}
sort(q+1,q+1+m,cmp);
int cnt=1;
build(1,n,1);
for(int i=1;i<=n;i++){//遍历右端点i
update(last[a[i]]+1,i,1,1);
last[a[i]]=i;
while(q[cnt].r==i){
ans[q[cnt].i]=query(q[cnt].l,q[cnt].l,1);//要按排序前的顺序输出
cnt++;
}
if(cnt>m) break;
}
for(int i=1;i<=m;i++) printf("%d ",ans[i]);
}

BZOJ 1878(离散化+线段树)的更多相关文章

  1. 南阳理工 题目9:posters(离散化+线段树)

    posters 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 The citizens of Bytetown, AB, could not stand that ...

  2. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

  3. 【POJ】2528 Mayor's posters ——离散化+线段树

    Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K   Description The citizens of Bytetown, A ...

  4. hpu校赛--雪人的高度(离散化线段树)

    1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec  内存限制: 128 MB 提交: 81  解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...

  5. BZOJ.4184.shallot(线段树分治 线性基)

    BZOJ 裸的线段树分治+线性基,就是跑的巨慢_(:з」∠)_ . 不知道他们都写的什么=-= //41652kb 11920ms #include <map> #include < ...

  6. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  7. 【bzoj4636】蒟蒻的数列 离散化+线段树

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...

  8. 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers

    题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...

  9. Mayor's posters (离散化线段树+对lazy的理解)

    题目 题意: n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围 li,ri(1<=li<=ri<=10000000) .求出最后还能看见多少张海报. 思路: 由于 ...

随机推荐

  1. bzoj5090 [Lydsy1711月赛]组题 分数规划

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5090 题解 令 \(s_i\) 表示 \(a_i\) 的前缀和. 那么平均难度系数就是 \[ ...

  2. 获取sender进程所产生的trace文件

    直接开启数据库实例级别的sql_trace是不现实的,会对所有连接到oracle的session都产生sql_trace,这样会产生大量的trace文件(垃圾文件),因此为跟踪特定进程的sql信息,可 ...

  3. 自学semantic UI个人博客首页模板

    以下是代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <m ...

  4. vue根据路由判断所在的内容

    1.所要实现的效果 2.利用路由来判断激活那个tab,更加准确

  5. 关于本地使用antd的upload组件上传文件,ngnix报错405的问题

    使用阿里的ui框架antd的upload,会自动请求ngnix上面的一个路径,也就是action所在的位置,一直报错405 not allowed,后来经讨论,统一将action写成一个路径,后端对这 ...

  6. angularjs表单注册--两次密码验证

    html <div class="container" ng-controller="RegisterCtrl"> <form name=&q ...

  7. PHP入门培训教程 PHP 数据类型

    PHP 支持八种原始类型(type),下面兄弟连PHP培训 小编来给大家列出:. 四种标量类型: string(字符串) integer(整型) float(浮点型,也作 double ) boole ...

  8. 动态淀粉质(划掉)题单&简要题解

    简介 动态点分治的思想:还不太清楚诶怎么办. 大概是通过降低树高来降低每次修改和询问的复杂度吧,还可以把树上一个连通块的信息统计到一个点(重心)上.具体实现方式和普通的静态点分治没有太大的区别,只是把 ...

  9. 走进JavaWeb技术世界14:Mybatis入门

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...

  10. 源码编译安装Apache/2.4.37-------踩了无数坑,重装了十几次服务器才会的,不容易啊!

    1.先进入/usr/local/中创建三个文件夹 apr apr-util apache cd /usr/local目录 mkdir apr mkdir apr-util mkdir apache 2 ...