题面

传送门

分析

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

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

当我们处理到第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. Python---进阶---常用模块os、jso

    一.写一个6位随机验证码程序(使用 random模块),要求验证码中至少包含一个数字.一个小写字母.一个大写字母 import randomimport string #help(string) co ...

  2. SQL Server死锁问题:事务(进程 ID x)与另一个进程被死锁在 锁 | 通信缓冲区资源上并且已被选作死锁牺牲品。请重新运行该事务。

    ### The error occurred while setting parameters### SQL: update ERP_SCjh_zzc_pl set IF_TONGBU=1 where ...

  3. java.util.Properties的使用及读取资源文件

    1.工具类Utils package com.oy.utils; import java.io.BufferedInputStream; import java.io.Closeable; impor ...

  4. mysql SELECT语句 语法

    mysql SELECT语句 语法,苏州大理石方箱 作用:用于从表中选取数据.结果被存储在一个结果表中(称为结果集). 语法:SELECT 列名称 FROM 表名称 以及 SELECT * FROM ...

  5. go语言实战 摘抄

    append 函数append会智能地处理底层数组的容量增长.在切片的容量小于1000个元素时,总是会成倍地增加容量.一旦元素个数超过1000,容量的增长因子就会设为1.25, 也就是每次增加25%的 ...

  6. HDU-6709 Fishing Master

    Description Heard that eom is a fishing MASTER, you want to acknowledge him as your mentor. As every ...

  7. 【PowerOJ1753&网络流24题】分配问题(KM)

    题意: 思路:费用流可做 最好的算法是KM板子 #include<bits/stdc++.h> using namespace std; typedef long long ll; typ ...

  8. [design pattern](1) Strategy

    引言 最近,在学习设计模式相关的知识.本博客主要想讲一讲策略模式,这也是我学习的第一个模式.写下这篇博客,主要想记录下个人的一点理解,也是想通过写博客的方式来加深对与Strategy的一点理解.以下的 ...

  9. 全球DC主机交流

    全球DC主机交流https://www.globaldc.cn/ 全球DC主机交流论坛是一个综合性的国内服务器.国外服务器.高防清洗.硬件服务器交流论坛,主要为网友提供IP地址鉴定主机商,全球独立服务 ...

  10. commons-collections包中的常用的工具类

    commons-collections包中的常用的工具类 <dependency> <groupId>commons-collections</groupId> & ...