BZOJ 1878(离散化+线段树)
题面
分析
首先我们观察到区间范围较大,而区间个数较少,考虑离散化,将所有询问按照右端点进行排序
离散化之后研究区间颜色个数变化的规律
当我们处理到第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(离散化+线段树)的更多相关文章
- 南阳理工 题目9:posters(离散化+线段树)
posters 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 The citizens of Bytetown, AB, could not stand that ...
- SGU 180 Inversions(离散化 + 线段树求逆序对)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...
- 【POJ】2528 Mayor's posters ——离散化+线段树
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of Bytetown, A ...
- hpu校赛--雪人的高度(离散化线段树)
1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec 内存限制: 128 MB 提交: 81 解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...
- BZOJ.4184.shallot(线段树分治 线性基)
BZOJ 裸的线段树分治+线性基,就是跑的巨慢_(:з」∠)_ . 不知道他们都写的什么=-= //41652kb 11920ms #include <map> #include < ...
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...
- 【bzoj4636】蒟蒻的数列 离散化+线段树
原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...
- 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers
题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...
- Mayor's posters (离散化线段树+对lazy的理解)
题目 题意: n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围 li,ri(1<=li<=ri<=10000000) .求出最后还能看见多少张海报. 思路: 由于 ...
随机推荐
- IO流二
1 数据流(了解) 1.1 概述 为了方便的操作java语言的基本数据类型和String类型的数据,可以使用数据流. 数据流的分类: DataInputStream DataOutputStream ...
- AGC014做题记录
貌似是比较水的一场 可是我依然8会做 C 发现除了第一步以外的走法都不会受到锁的影响并且一定选四个方向距离最近的径直走过去 那么第一步能走到的联通块取个min就好了 (我竟然第一发特别认真的写了一个D ...
- MySQL集群安装与配置
MySQL集群安装与配置 文章目录 [隐藏] 一.mysql集群安装 二.节点配置 三.首次启动节点 四.测试服务是否正常 五.安全关闭和重启 MySQL Cluster 是 MySQL 适合于分 ...
- proxy配置
关于config.js里面proxy的配置: proxy: { '/api': { target: 'http://192.168.***.**:8500', cha ...
- 【SaltStack官方版】—— Events&Reactor系统—EVENT SYSTEM
Events&Reactor系统 EVENT SYSTEM The Salt Event System is used to fire off events enabling third pa ...
- 【leetcode】1161. Maximum Level Sum of a Binary Tree
题目如下: Given the root of a binary tree, the level of its root is 1, the level of its children is 2, a ...
- 用TweenMax.js动画让数字动起来
html: <div class="val2">0</div> js: let val2 = document.getElementsByClassName ...
- CodeForces 1198D 1199F Rectangle Painting 1
Time limit 1000 ms Memory limit 262144 kB 解题思路 一堆循环嵌套的那种dp,不好想.但是可以搜啊,很暴力的.记忆化一下就好. 我们定义搜索函数\(\text{ ...
- HDU 6581 Vacation
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission( ...
- Java数据结构之单链表
这篇文章主要讲解了通过java实现单链表的操作,一般我们开始学习链表的时候,都是使用C语言,C语言中我们可以通过结构体来定义节点,但是在Java中,我们没有结构体,我们使用的是通过类来定义我们所需要的 ...