hdu 5172 GTY's gay friends (区间最值)
来源:互联网 发布:linux 切换节点 编辑:程序博客网 时间:2024/04/20 17:44
GTY 有n个朋友站成一排,每个朋友有一个值a[i](1<=a[i]<=n)有m个询问,问区间[L,R]内的数值是否是1到R-L+1的一个全排列。
先判断区间[L,R]内的和是否等于1~R-L+1全排列的和,用前缀和实现。.若相等,由于数值可能会有相等的,所以还需要对区间内元素进行判重。
如何进行判重呢?用pre[i]记录数a[i]上一次出现的位置,然后对于区间[L,R]内的每一个数判断其pre是否都小于L。这里用线段树查询区间内pre的最大值,看其是否小于L即可。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define maxn 1000005#define lson i<<1,l,m#define rson i<<1|1,m+1,rint Max[maxn<<2],a[maxn],sum[maxn],last[maxn],pre[maxn];void PushUp(int i){ Max[i]=max(Max[i<<1],Max[i<<1|1]);}void build(int i,int l,int r){ if(l==r) { Max[i]=pre[l]; return; } int m=(l+r)>>1; build(lson); build(rson); PushUp(i);}int query(int L,int R,int i,int l,int r){ if(L<=l&&r<=R) return Max[i]; int m=(l+r)>>1; int ans=0; if(L<=m) ans=max(ans,query(L,R,lson)); if(R>m) ans=max(ans,query(L,R,rson)); return ans;}int main(){ int n,m,i; while(~scanf("%d%d",&n,&m)) { sum[0]=0; memset(last,0,sizeof(last)); for(i=1;i<=n;++i) { scanf("%d",&a[i]); pre[i]=last[a[i]]; last[a[i]]=i; sum[i]=sum[i-1]+a[i]; } build(1,1,n); while(m--) { int L,R; scanf("%d%d",&L,&R); if(sum[R]-sum[L-1]==(R-L+2)*(R-L+1)/2&&query(L,R,1,1,n)<L) puts("YES"); else puts("NO"); } } return 0;}
0 0
- hdu 5172 GTY's gay friends (区间最值)
- Hdu 5172 GTY's gay friends
- hdu 5172 GTY's gay friends
- HDU 5172 GTY's gay friends
- HDU 5172 GTY's gay friends
- HDU 5172 GTY's gay friends(线段树)
- HDU 5172 GTY's gay friends (预处理+线段树)
- HDU 5172 GTY's gay friends(线段树)
- HDU 5172 GTY's gay friends(HASH 随机算法)
- HDU 5172 GTY's gay friends 线段树(排列,区间不同的数)
- hdu 5172 GTY's gay friends 线段树
- HDU 5172 - GTY's gay friends (线段树)
- hdu 5172 GTY's gay friends (线段树||hash)
- hdu 5172 GTY's gay friends Hash随机算法
- HDU 5172 GTY's gay friends HASH随机算法
- HDU 5172 GTY's gay friends (线段树)
- HDU-5172-GTY's gay friends-线段树单点更新
- GTY's gay friends
- SQLServer 网络协议(一)
- sgu209:Areas(计算几何)
- 动态规划经典问题
- android布局属性详解
- 信号量函数semget中的key_t
- hdu 5172 GTY's gay friends (区间最值)
- C#编程中yield return
- AndroidManifest.xml file missing!
- hdu 5059 Help him(模拟)
- poj_2151
- sublime text 主题插件-Nexus
- MIPS平台移植apache 2.2.7
- Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置
- nyoj 75 日期计算