Educational Codeforces Round 7(C)线段树
来源:互联网 发布:大纲写作软件 编辑:程序博客网 时间:2024/05/23 01:51
题意:给你N个数字,m次查询,每次问在[LI,RI]区间有没有不等于的XI这个数字,如果有输出任意一个数字的下标
题解:这里可是考虑使用线段树,我们更新最值(MAX,MIN),如果这个区间的最值不等于XI,那么必定是有解的,否则肯定是无解的,那么我们直接愉快的上线段树就好了,更新最值。
#include<iostream>#include<cstdio>using namespace std;#define lson L,mid,rt<<1#define rson mid+1,R,rt<<1|1#define N 300005struct point{int x,org;bool operator<(const point &x1)const{return this->x<x1.x;}point (int _x,int _org):x(_x),org(_org){}point (){}};struct segment{int lc,rc,rt;point mx,mi;int mid(){return (lc+rc)>>1;}}tree[N<<2];point ans;void pushup(int rt){tree[rt].mx=max(tree[rt<<1].mx,tree[rt<<1|1].mx);tree[rt].mi=min(tree[rt<<1].mi,tree[rt<<1|1].mi);}void build(int L,int R,int rt){tree[rt].lc=L;tree[rt].rc=R;if(L==R){scanf("%d",&tree[rt].mi.x);tree[rt].mi.org=L;tree[rt].mx=tree[rt].mi;return ;}int mid=tree[rt].mid();build(lson);build(rson);pushup(rt);}void query(int L,int R,int rt, int num){ if(tree[rt].lc==L&&tree[rt].rc==R) {if(num!=tree[rt].mi.x){ans=tree[rt].mi;}if(num!=tree[rt].mx.x){ans=tree[rt].mx;}return ;}int mid=tree[rt].mid();if(R<=mid)query(L,R,rt<<1,num);else if(L>mid)query(L,R,rt<<1|1,num);else{query(lson,num);query(rson,num);}}int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifint n,m,xi,ri,li;scanf("%d%d",&n,&m);build(1,n,1);while(m--){scanf("%d%d%d",&li,&ri,&xi);ans=point(-1,-1);query(li,ri,1,xi);printf("%d\n",ans.org);}return 0;}
0 0
- Educational Codeforces Round 7(C)线段树
- Educational Codeforces Round 6(E)DFS序,线段树
- Educational Codeforces Round 12(C)贪心
- CF Educational Codeforces Round 15(C)
- Educational Codeforces Round 7
- Educational Codeforces Round 7
- Educational Codeforces Round 7
- Educational Codeforces Round 7
- Educational Codeforces Round 7
- CF Educational Codeforces Round 6 E题 dfs+线段树
- Educational Codeforces Round 21 C
- Educational Codeforces Round 5 C
- Educational Codeforces Round 26 C
- Educational Codeforces Round 20 G. Periodic RMQ Problem(线段树+主席树)
- Educational Codeforces Round 6 E. New Year Tree(DFS序+线段树)
- Educational Codeforces Round 6 E. New Year Tree(dfs序线段树)
- Educational Codeforces Round 6 E. New Year Tree(DFS序+线段树)
- Educational Codeforces Round 20 G. Periodic RMQ Problem(线段树动态开点)
- 从头认识Spring-2.1 自动装配(2)-byType(2)
- UITableView细节.txt
- Educational Codeforces Round 7(B)模拟
- 深度学习与计算机视觉系列(10)_细说卷积神经网络
- URL和URI的区别
- Educational Codeforces Round 7(C)线段树
- BNUOJ 51277 魔方复原(模拟、置换)
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍
- nyoj891找点
- AppRTCDemo 运行测试
- HDU 5250 三阶魔方(模拟、置换)
- 应用内调用系统的照相机拍照的方法
- 如何处理大量并发用户访问网页,并且如何跟踪每个用户
- exit()与_exit()的区别