CodeForces 1000F One Occurrence
You are given an array $a$ consisting of $n$ integers, and $q$ queries to it. $i$-th query is denoted by two integers $l_i$ and $r_i$. For each query, you have to find any integer that occurs exactly once in the subarray of $a$ from index $l_i$ to index $r_i$ (a subarray is a contiguous subsegment of an array). For example, if a=[1,1,2,3,2,4]a=[1,1,2,3,2,4], then for query (li=2,ri=6)(li=2,ri=6) the subarray we are interested in is [1,2,3,2,4][1,2,3,2,4], and possible answers are 11, 33 and 44; for query (li=1,ri=2)(li=1,ri=2) the subarray we are interested in is [1,1][1,1], and there is no such element that occurs exactly once.
Can you answer all of the queries?
Input
The first line contains one integer $n$ ($1≤n≤5⋅10^5$,$1≤n≤5⋅10^5$).
The second line contains $n$ integers $a_1,a_2,…,a_n$ ($1≤a_i≤5⋅10^5$,$1≤a_i≤5⋅10^5$).
The third line contains one integer $q$ ($1≤q≤5⋅10^5$,$1≤q≤5⋅10^5$).
Then $q$ lines follow, $i$-th line containing two integers $l_i$ and $r_i$ representing $i$-th query ($1≤l_i≤r_i≤n$,$1≤l_i≤r_i≤n$).
Output
Answer the queries as follows:
If there is no integer such that it occurs in the subarray from index $l_i$ to index $r_i$exactly once, print $0$. Otherwise print any such integer.
Example
Input
6
1 1 2 3 2 4
2
2 6
1 2
Output
4
0
感想
再也不敢熬夜了,通过熬夜换回了半夜两三个小时,后面几天我要用几十个小时去偿还,利息太高了……一道可以算做过的题,把我卡了那么多天……
解题思路
先扔一个链接(HH的项链)//看不懂当年自己的博客什么鬼……这次借着做这题的机会,把当年的博客改得更好懂一点
同样的思路,把询问离线,按左端点升序排序。搞一个nxt数组,nxt[i]记录下一个和a[i]相同的数字的位置,没有就赋值为inf(无穷大)。搞一棵线段树(树状数组啥的也行,只要能快速单点更新、查询区间最大值就好),记录区间内(nxt[i]最大)的i,代码里变量名是mxi,开始建树时不急着在树上存值,先把mxi全部赋值成0。
然后,所有第一次出现的数字a[i]在线段树下标i的位置留下痕迹(用自己的nxt[i]去更新线段树,让线段树存下区间内所有留下痕迹的i中nxt[i]最大的i)。
每次查询之前,先把询问的左端点以左的那些第一次出现的数字处理一下。如果他们的nxt[i]不是inf,就把他们的nxt[i]拿去在线段树上留下痕迹,然后清空他们自己留下的痕迹,查询时que函数会返回一个区间内nxt[i]最大的那个i,代码里变量名为temp,如果nxt[temp]都没超过询问区间的右端点,那么区间里没有只出现一次的数了,答案为0,否则答案就可以是a[temp],下一个相同的出现在区间外(inf也算区间外)。
最后,把答案按照输入顺序排个序,输出,没了。
和HH的项链那系列莫队板子题一样,莫队也可以(这题莫队好像要卡常?),主席树也能在线处理询问(我还不会)。
源代码
#include<stdio.h>
#include<algorithm> const int inf=0x7fffffff; int n,m;
int a[]={},mx=-,nxt[]={};
int p[]={};//类似一个桶,装第i号颜色第一次出现的位置 struct Segtree{
int l,r;
int mxi;//区间最大的nxt[i]对应的下标i
}t[];
inline int lson(int x){return x<<;}
inline int rson(int x){return x<<|;}
void maketree(int x,int l,int r)
{
if(l==r)
{
t[x]={l,r,};
return;
}
t[x].l=l;
t[x].r=r;
t[x].mxi=;
int mid=(l+r)>>;
maketree(lson(x),l,mid);
maketree(rson(x),mid+,r);
}
void update(int x,int pos)
{
if(pos>t[x].r||pos<t[x].l) return;
if(t[x].l==t[x].r)
{
t[x].mxi=pos;
return;
}
update(lson(x),pos);
update(rson(x),pos);
if(nxt[t[lson(x)].mxi]>nxt[t[rson(x)].mxi])
t[x].mxi=t[lson(x)].mxi;
else
t[x].mxi=t[rson(x)].mxi;
}
int que(int x,int l,int r)
{
if(l>t[x].r||r<t[x].l) return ;
if(l<=t[x].l&&t[x].r<=r)
return t[x].mxi;
int templ=que(lson(x),l,r),tempr=que(rson(x),l,r);
if(nxt[templ]>nxt[tempr]) return templ;
else return tempr;
} struct Ask{
int l,r,id,ans;
}ask[];
bool cmp1(const Ask & a,const Ask & b)
{
return a.l==b.l?a.r<b.r:a.l<b.l;
}
bool cmp2(const Ask & a,const Ask & b)
{
return a.id<b.id;
} int main()
{
//freopen("test.in","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",a+i),mx=std::max(a[i],mx);
for(int i=n;i>;i--)//预处理出nxt和p
{
if(p[a[i]]) nxt[i]=p[a[i]];
else nxt[i]=inf;
p[a[i]]=i;
}
maketree(,,n);
for(int i=;i<=mx;i++)
{
if(p[i]) update(,p[i]);//线段树单点更新
} scanf("%d",&m);
for(int i=,l,r;i<=m;i++)
{
scanf("%d%d",&l,&r);
ask[i]={l,r,i,};
}
std::sort(ask+,ask++m,cmp1); for(int i=,ll=;i<=m;i++)
{
while(ll<ask[i].l)
{
if(nxt[ll]!=0x7fffffff)
{
update(,nxt[ll]);
nxt[ll]=;
update(,ll);
}
ll++;
}
int temp=que(,ask[i].l,ask[i].r);//返回nxt最大的那个的下标
if(nxt[temp]<=ask[i].r) ask[i].ans=;
else ask[i].ans=a[temp];
} std::sort(ask+,ask++m,cmp2);
for(int i=;i<=m;i++)
printf("%d\n",ask[i].ans); return ;
}
CodeForces 1000F One Occurrence的更多相关文章
- codeforces 1000F One Occurrence(线段树、想法)
codeforces 1000F One Occurrence 题意 多次询问lr之间只出现过一次的数是多少. 题解 将查询按照左端点排序,对于所有值维护它在当前位置后面第二次出现是什么时候,那么查询 ...
- Codeforces 1000F One Occurrence 主席树|| 离线+线段树
One Occurrence 为什么我半年前这么菜呀, 这种场只A三题... 我们在主席树 || 线段树上维护每个数的右边和它一样的数在哪里, 然后就变成了区间求最大值. 注意加进去的时候要把它右边一 ...
- 【Codeforces 1000F】One Occurrence
题意:给一个序列,每次查询某个区间内一个只出现一次的数. 思路:线段树. 首先我们看只出现一次的本质是什么. 如果一个数\(x\)在\((l,r)\)中只出现了一次,那么它在其中第一次出现位置为\ ...
- F - One Occurrence CodeForces - 1000F (线段树+离线处理)
You are given an array aa consisting of nn integers, and qq queries to it. ii-th query is denoted by ...
- Codeforces 528D Fuzzy Search(FFT)
题目 Source http://codeforces.com/problemset/problem/528/D Description Leonid works for a small and pr ...
- Codeforces 714C. Sonya and Queries Tire树
C. Sonya and Queries time limit per test:1 second memory limit per test: 256 megabytes input:standar ...
- CodeForces - 427A (警察和罪犯 思维题)
Police Recruits Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)
Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)(A.思维题,B.思维题)
A. Vicious Keyboard time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
随机推荐
- bzoj 1045: [HAOI2008] 糖果传递【瞎搞】
感觉我的智商可能不够写题解,就直接截了hzwer的blog 地址http://hzwer.com/2656.html #include<iostream> #include<cstd ...
- bzoj 1774: [Usaco2009 Dec]Toll 过路费【排序+Floyd】
非常迷的一道题啊 我觉得挺对的版本只得了30 总之就是Floyd·改,开两个数组,一个是d[i][j]就是普通的只有边权的最短路,a[i][j]是题目要求的那种 具体改的地方是把枚举中转点的地方把中转 ...
- 源码阅读之HashMap(JDK8)
概述 HashMap根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而具有很快的访问速度,但遍历顺序却是不确定的. HashMap最多只允许一条记录的键为null,允许多条记录 ...
- OAuth2.0最简向导
无论你是否有技术背景,你都能看懂授权协议框架OAuth2.0 翻译来自:川崎高彦对自己投资人讲解自己的SaaS安全产品. Got it! https://medium.com/@darutk/the- ...
- [算法] 常见排序算法总结(C语言版)
常见排序算法总结 本文对比较常用且比较高效的排序算法进行了总结和解析,并贴出了比较精简的实现代码,包括选择排序.插入排序.归并排序.希尔排序.快速排序等.算法性能比较如下图所示: 1 冒泡排序 基本原 ...
- [Usaco2018 Open]Disruption
Description Farmer John自豪于他所经营的交通发达的的农场.这个农场是由N块牧场(2≤N≤50,000)组成的,N-1条双向道路将它们连接起来,每一条道路的都为一单位长度.Farm ...
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- Linux重启和关机命令
Linux重启命令: 方式1:shutdown –r now 方式2:reboot Linux关机命令: shutdown –h now
- PHP 小tip .(@)符号和 php if 赋值
tip 1: 下面介绍一下它的用法. 例如: 复制代码代码如下: function db_connect()//连接数据库 { @$db =mysql_connect('localhost','roo ...
- 374 Guess Number Higher or Lower 猜数字大小
我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字.每次你猜错了,我会告诉你这个数字是大了还是小了.你调用一个预先定义好的接口 guess(int n ...