Problem Description

You are given an array a1,a2,...,an(∀i∈[1,n],1≤ai≤n). Initially, each element of the array is **unique**.

Moreover, there are m instructions.

Each instruction is in one of the following two formats:

1. (1,pos),indicating to change the value of apos to apos+10,000,000;
2. (2,r,k),indicating to ask the minimum value which is **not equal** to any ai ( 1≤i≤r ) and **not less ** than k.

Please print all results of the instructions in format 2.

Input

The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.

In each test case, there are two integers n(1≤n≤100,000),m(1≤m≤100,000) in the first line, denoting the size of array a and the number of instructions.

In the second line, there are n distinct integers a1,a2,...,an (∀i∈[1,n],1≤ai≤n),denoting the array.
For the following m lines, each line is of format (1,t1) or (2,t2,t3).
The parameters of each instruction are generated by such way :

For instructions in format 1 , we defined pos=t1⊕LastAns . (It is promised that 1≤pos≤n)

For instructions in format 2 , we defined r=t2⊕LastAns,k=t3⊕LastAns. (It is promised that 1≤r≤n,1≤k≤n )

(Note that ⊕ means the bitwise XOR operator. )

Before the first instruction of each test case, LastAns is equal to 0 .After each instruction in format 2, LastAns will be changed to the result of that instruction.

(∑n≤510,000,∑m≤510,000 )

Output

For each instruction in format 2, output the answer in one line.

思路

有两种nlogn的解法 、

其一:用权值线段树维护出现的下标 然后维护一下区间最大值 对于1操作我们可以直接把下标变大 对于操作二我们可以区间查询到底 但是要加上一个判断 就是当前子树的

最大下标是否大于输入的位置。

其二:我们可以同样考虑用权值线段树维护当前的最左位置 这样建立可持久化线段树以后我们就可以解决不修改的查询操作 对于操作一 我们可以考虑用一个set维护 那么对于某一查询要么就是

直接查出来的值要么就是set里面的值 我们选一个最小的即可。

解法一:

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int N = 1e5+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
typedef long long ll;
const ll mod = 1e9+7;
int a[N],po[N];
struct tree{
int l,r,v;
}t[N<<2];
void build(int p,int l,int r){
t[p].l=l; t[p].r=r;
if(l==r){
t[p].v=po[l];
//cout<<t[p].l<<" "<<t[p].r<<" "<<t[p].v<<endl;
return ;
}
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
t[p].v=max(t[p<<1].v,t[p<<1|1].v);
}
void update(int p,int x,int v){
if(t[p].l==t[p].r&&t[p].l==x){
t[p].v=v;
po[t[p].l]=v;
return ;
}
int mid=(t[p].l+t[p].r)>>1;
if(x<=mid) update(p<<1,x,v);
else update(p<<1|1,x,v);
t[p].v=max(t[p<<1].v,t[p<<1|1].v);
}
int query(int p,int l,int r,int pos){
// cout<<t[p].l<<" "<<t[p].r<<endl;
if(t[p].l==t[p].r){
return t[p].l;
}
int mid=(t[p].l+t[p].r)>>1;
int res;
if(t[p<<1].v>pos){
if(l<=mid) res=query(p<<1,l,r,pos);
if(po[res]>pos) return res;
if(r>mid) res=query(p<<1|1,l,r,pos);
}else{
if(r>mid) res=query(p<<1|1,l,r,pos);
}
return res;
}
int main(){
// ios::sync_with_stdio(false);
// cin.tie(0); cout.tie(0);
int t; scanf("%d",&t);
while(t--){
memset(po,0,sizeof(po));
int n,m; scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",a+i),po[a[i]]=i;
po[n+1]=n+1;
a[n+1]=n+1;
build(1,1,n+1);
int lastans=0,ans;
for(int i=1;i<=m;i++){
int op,r,k;
scanf("%d%d",&op,&r);
if(op==1){
r=r^lastans;
//printf("%d\n",a[r]);
update(1,a[r],n+1);
}else{
scanf("%d",&k);
r=r^lastans; k=k^lastans;
//cout<<r<<" "<<k<<" "<<lastans<<endl;
ans=query(1,k,n+1,r);
lastans=ans;
printf("%d\n",ans);
// cout<<ans<<"\n";
}
}
}
}

解法二:

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int N = 1e5+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
typedef long long ll;
const ll mod = 1e7+9;
int a[N],rt[N];
struct tree{
int l,r,v;
int ls,rs;
}t[N<<5];
set<int> s;
int nico=0;
int cnt=0;
void build(int &p,int l,int r){
p=++nico;
t[p].l=l; t[p].r=r;
if(l==r){
t[p].v=l;
return ;
}
int mid=(l+r)>>1;
build(t[p].ls,l,mid);
build(t[p].rs,mid+1,r);
t[p].v=min(t[t[p].ls].v,t[t[p].rs].v);
}
void update(int &p,int last,int x,int v){
p=++cnt;
t[p]=t[last];
if(t[p].l==t[p].r&&t[p].l==x){
t[p].v=v;
return ;
}
int mid=(t[p].l+t[p].r)>>1;
if(x<=mid) update(t[p].ls,t[last].ls,x,v);
else update(t[p].rs,t[last].rs,x,v);
t[p].v=min(t[t[p].ls].v,t[t[p].rs].v);
}
int query(int p,int l,int r){
if(l<=t[p].l&&t[p].r<=r){
return t[p].v;
}
int mid=(t[p].l+t[p].r)>>1;
int ans=inf;
if(l<=mid) ans=min(ans,query(t[p].ls,l,r));
if(mid<r) ans=min(ans,query(t[p].rs,l,r));
return ans;
}
int main(){
// ios::sync_with_stdio(false);
// cin.tie(0); cout.tie(0);
int t; scanf("%d",&t); while(t--){
s.clear();
int n,m; scanf("%d%d",&n,&m);
build(rt[0],1,n+1);
cnt=nico;
for(int i=1;i<=n;i++){
scanf("%d",a+i);
update(rt[i],rt[i-1],a[i],inf);
}
int lastans=0; int nowans;
for(int i=1;i<=m;i++){
int op,t1,t2;
scanf("%d",&op);
if(op==1){
scanf("%d",&t1);
t1=t1^lastans;
s.insert(a[t1]);
}else{
scanf("%d%d",&t1,&t2);
t1=t1^lastans; t2=t2^lastans;
auto v=s.lower_bound(t2);
if(v!=s.end()){
nowans=min(*v,query(rt[t1],t2,n+1));
}else{
nowans=query(rt[t1],t2,n+1);
}
lastans=nowans;
printf("%d\n",nowans);
}
}
}
}

hdu 6703 array(权值线段树)的更多相关文章

  1. R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数

    R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...

  2. HDU 6609 离散化+权值线段树

    题意 有一个长度为\(n\)的数组W; 对于每一个\(i\)(\(1<=i<=n\)),你可以选择中任意一些元素W[k] (\(1<=k<i\)),将他们的值改变为0,使得\( ...

  3. Petya and Array (权值线段树+逆序对)

    Petya and Array http://codeforces.com/problemset/problem/1042/D time limit per test 2 seconds memory ...

  4. 2019年CCPC网络赛 HDU 6703 array【权值线段树】

    题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...

  5. ccpc网赛 hdu6703 array(权值线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=6703 大意:给一个n个元素的数组,其中所有元素都是不重复的[1,n]. 两种操作: 将pos位置元素+1e7 查 ...

  6. HDU 6464 免费送气球 【权值线段树】(广东工业大学第十四届程序设计竞赛)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6464 免费送气球 Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. HDU - 2665 Kth number 主席树/可持久化权值线段树

    题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...

  8. HDU - 5592 ZYB's Premutation (权值线段树)

    题意:给出序列前k项中的逆序对数,构造出这个序列. 分析:使用权值线段树来确定序列元素. 逆序对的数量肯定是递增的,从最后一个元素开始逆向统计,则\(a[i] - a[i-1]\)即位置i之前比位置i ...

  9. hdu 5592 ZYB's Premutation (权值线段树)

    最近在线段树的世界里遨游,什么都能用线段树做,这不又一道权值线段树了么. ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Mem ...

随机推荐

  1. PostgreSQL WAL日志详解

    wal日志即write ahead log预写式日志,简称wal日志.wal日志可以说是PostgreSQL中十分重要的部分,相当于oracle中的redo日志. 当数据库中数据发生变更时:chang ...

  2. 【C++】《C++ Primer 》第六章

    第六章 函数 一.函数基础 函数定义:包括返回类型.函数名字和0个或者多个形参(parameter)组成的列表和函数体. 调用运算符:调用运算符的形式是一对圆括号 (),作用于一个表达式,该表达式是函 ...

  3. 天梯赛练习 L3-007 天梯地图 (30分) Dijkstra

    题目分析: 本题的题意比较清晰,就是有一个起点和一个终点,给出m条路径,可能是单向的可能是双向的,同时一条路有两个权重,分别是通过这条路需要的时间和这条路的路径长度,题目需要求出两条路径,一条是在最快 ...

  4. python学习笔记 | macOS Big Sur动态壁纸食用指南

    目录 前言 爬虫篇 壁纸使用篇 后记 前言 北京时间23日凌晨1点,苹果WWDC2020大会开幕.在发布会上,苹果正式发布了新版macOS,并将其命名为"Big Sur". 相比于 ...

  5. MyBatis 查询的时候属性名和字段名不一致的问题

    目录 问题 解决方案:resultMap 问题 当我们数据库中的字段和实体类中的字段不一致的时候,查询会出问题 数据库字段是 pwd id name pwd 1 张三 123456 2 李四 1234 ...

  6. freopen函数总结

    函数原型: freopen(const char * __restrict__ _Filename,const char * __restrict__ _Mode,FILE * __restrict_ ...

  7. python之格式化字符串速记整理

      一.格式化字符串的方式: 1.字符串表达式: 语法格式:'%s' % var 或 '%s %d' % (var1, var2) 说明:%s.%d等为格式类型说明符 例子: >>> ...

  8. Kubernetes CoreDNS 状态是 CrashLoopBackOff 报错

    查看状态的时候,遇见coredns出现crashlookbackoff,首先我们来进行排错,不管是什么原因,查看coredns的详细信息,以及logs [root@k8s-master coredns ...

  9. 视图V_160M和表T_160M的维护

    今天发现一个视图,通过SM30居然无法维护,这个视图就是V_160M,表为T_160M,是采购相关的系统消息, 不过别着急,有办法维护的,呵呵,看下面: 试一试OMCQ这个事物代码吧! 分享出来,给需 ...

  10. Java中的Date类型无法赋值给数据库的datetime类型

    因为Java中new Date()的结果是"Thu Aug 27 19:03:54 CST 2020",而mysql中的datetime不接受这样的日期格式,插入数据会报错. 解决 ...