2019CCPC网络赛——array(权值线段树)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=6703
题目大意:
给出一个n(n<1e5)个元素的数组A,A中所有元素都是不重复的[1,n]。
有两种操作:
1.将pos位置的元素+1e7
2.查询不属于[1,r]中的最小的>=k的值。
强制在线,上次计算结果和输入值xor得到区间。
比赛的时候感觉这道题有点线段树的感觉,和前段时间多校训练一个题很像,想了40多分钟才理想清思路。
解法:
可以看出,执行1操作的时候加的数非常大,可以得出每次输出的答案在为1到n+1之间。可以将1-n的每个数在数组A中的位置记录下,存在线段树中,维护线段树区间最大值。
当执行2操作时,只需要查询区间[k,n]中大于r的值即可,找出最小的数字,先搜索左子树,若无答案搜索右子树,若两侧均无解则答案为n+1。
当执行1操作时,由于pos+1e7,因此之后的数组中不存在该数,将第pos位的数字在线段树中的值转换为inf即可。
贴上自己比赛时打了40多分钟的代码
#include<bits/stdc++.h> using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int,int> pii;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define rept(i,x,y) for(int i=x;i<=y;i++)
#define per(i,x,y) for(int i=x;i>=y;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define de(x) cout<< #x<<" = "<<x<<endl
#define dd(x) cout<< #x<<" = "<<x<<" "
#define mes(a,b) memset(a,b,sizeof a)
const int inf= 0x3f3f3f3f;
int arrcy[],p[];
class Tree
{
public:
int l,r,val;
}tree[];
void build(int id,int l,int r);
void change(int id,int x);
void test();
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int t;
cin>>t;
while(t--)
test();
return ;
}
int find(int id,int r,int k);
void test()
{
int n,q;
int ans=;
cin>>n>>q;
rept(i,,n)
{
cin>>arrcy[i];
p[arrcy[i]]=i;
}
build(,,n);
rep(i,,q)
{
int way;
cin>>way;
if(way==)
{
int pos;
cin>>pos;
change(,arrcy[ans^pos]);
}
else
{
int r,k;
cin>>r>>k;
r^=ans;
k^=ans;
// cout<<"r="<<r<<" "<<"k="<<k<<endl;
ans=find(,r,k);
if(ans==-)
ans=max(n+,k);
cout<<ans<<endl;
}
}
}
void build(int id,int l,int r)
{
tree[id].l=l;
tree[id].r=r;
if(l==r)
{
tree[id].val=p[l];
return ;
}
int mid=(l+r)/;
build(id*,l,mid);
build(id*+,mid+,r);
tree[id].val=max(tree[id*].val,tree[id*+].val);
} int find(int id,int r,int k)
{
if(tree[id].l==tree[id].r)
{
if(tree[id].val>r)
return tree[id].l;
else return -;
}
int mid=(tree[id].l+tree[id].r)/,ans=-;
if(mid>=k&&tree[id*].val>r) ans=find(id*,r,k);
if(ans!=-) return ans;
else if(tree[id*+].r>=k&&tree[id*+].val>r) ans=find(id*+,r,k);
return ans;
} void change(int id,int x)//将x转换为inf
{
if(tree[id].l==tree[id].r)
{
tree[id].val=inf;
return ;
}
if(x<=(tree[id].l+tree[id].r)/) change(id*,x);
else change(id*+,x);
tree[id].val=max(tree[id*].val,tree[id*+].val);
}
2019CCPC网络赛——array(权值线段树)的更多相关文章
- hdu 6703 array(权值线段树)
Problem Description You are given an array a1,a2,...,an(∀i∈[1,n],1≤ai≤n). Initially, each element of ...
- Petya and Array (权值线段树+逆序对)
Petya and Array http://codeforces.com/problemset/problem/1042/D time limit per test 2 seconds memory ...
- 2019年CCPC网络赛 HDU 6703 array【权值线段树】
题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...
- ccpc网赛 hdu6703 array(权值线段树
http://acm.hdu.edu.cn/showproblem.php?pid=6703 大意:给一个n个元素的数组,其中所有元素都是不重复的[1,n]. 两种操作: 将pos位置元素+1e7 查 ...
- 2019牛客训练赛第七场 C Governing sand 权值线段树+贪心
Governing sand 题意 森林里有m种树木,每种树木有一定高度,并且砍掉他要消耗一定的代价,问消耗最少多少代价可以使得森林中最高的树木大于所有树的一半 分析 复杂度分析:n 1e5种树木,并 ...
- HDU 6464 免费送气球 【权值线段树】(广东工业大学第十四届程序设计竞赛)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6464 免费送气球 Time Limit: 2000/1000 MS (Java/Others) M ...
- HDU 6464 权值线段树 && HDU 6468 思维题
免费送气球 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- D. Restore Permutation(权值线段树)
D. Restore Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 2019牛客多校第七场E Find the median 权值线段树+离散化
Find the median 题目链接: https://ac.nowcoder.com/acm/contest/887/E 题目描述 Let median of some array be the ...
- 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings
谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...
随机推荐
- ROS与树莓派的结合
从零开始学树莓派和ROS 今天写下自己的第一篇博客,记录一下自己的学习历程和学习过程中碰到的各种小问题,供同道者参阅和自己以后回顾用 ,水平不高,我就放开手写吧,反正也不会有人看. 我现在在做毕业设计 ...
- P3144 [USACO16OPEN]关闭农场——离线,并查集
https://www.luogu.org/problem/P3144 每次关闭一个农场,农场之间有边相连,问每次关闭后开着的农场是否是一个连通块: 数据小,离线搞: 我们先记录删的顺序,然后倒着来, ...
- Ubuntu14.04系统显示器不自动休眠修改
-----设置Ubuntu14.04不自动锁屏,常亮 右上角的菜单打开system setting ----- brightness&lock按钮 1. 2. 参考: https://blog ...
- mysql IFNULL函数和COALESCE函数使用技巧
IFNULL() 函数 IFNULL() 函数用于判断第一个表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值. IFNULL() 函数 ...
- Java并发编程示例代码-----ReentrantLock
public class ReenterLock implements Runnable{ public static ReentrantLock lock=new ReentrantLock(); ...
- Arts打卡第7周
Algorithm.主要是为了编程训练和学习. 每周至少做一个 leetcode 的算法题(先从Easy开始,然后再Medium,最后才Hard). 进行编程训练,如果不训练你看再多的算法书,你依然不 ...
- [软工]Github的使用
注册 修改个人信息 fork项目 使用github客户端 commit项目 发送PR 注意事项 不要使用上述项目进行试验 建议Github用户名有规律,好记忆
- html中的lang标记有什么用
html中的lang标记有什么用 一.总结 一句话总结: 为文档或元素设定主语言(即lang) 比如google浏览器有个自动翻译的功能,而自动翻译要看这个文档的语言 1.其它标签中设置lang属性? ...
- 到达型01背包---P1504 积木城堡
P1504 积木城堡 题解 到达型01背包 对于每一组城堡,它可以到达一些高度 但是我们要求的是所有背包可以到达的公共高度的最大值 f[ i ] 表示对于一组城堡,能否到达高度 j ,然后我们跑 n ...
- VUE钩子函数created与mounted区别
created:在模板渲染成html前调用,即通常初始化某些属性值,然后再渲染成视图. mounted:在模板渲染成html后调用,通常是初始化页面完成后,再对html的dom节点进行一些需要的操作.