HDU 5172 GTY's gay friends (线段树)
来源:互联网 发布:淘宝男装卖家推荐尺码 编辑:程序博客网 时间:2024/04/19 13:11
题目地址:HDU 5172
比赛的时候用一个维护了区间和,区间积,区间最值的线段树水过去了。。赛后数据改回10^6后,就TLE了。。
正解是区间和用前缀和维护就可以。然后维护一个该位上的数上一个出现额位置,那么每次查询,如果每个数的上一个出现的位置都小于l的话,那么就说明没有重复的,如果区间和符合全排列的和,那么就说明肯定是一个全排列了。
代码如下:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;#define root 1, n, 1#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1int a[1000002], pre[1000002], pos[1000002];int Max[4000000];LL sum[1000002];void PushUp(int rt){ Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);}void build(int l, int r, int rt){ if(l==r){ Max[rt]=pre[l]; return ; } int mid=l+r>>1; build(lson); build(rson); PushUp(rt);}int Query(int ll, int rr, int l, int r, int rt){ if(ll<=l&&rr>=r){ return Max[rt]; } int mid=l+r>>1, ans=-1; if(ll<=mid) ans=max(ans,Query(ll,rr,lson)); if(rr>mid) ans=max(ans,Query(ll,rr,rson)); return ans;}int main(){ int n, m, i, j, x, l, r, ans; while(scanf("%d%d",&n,&m)!=EOF){ memset(pos,-1,sizeof(pos)); sum[0]=0; for(i=1;i<=n;i++){ scanf("%d",&x); pre[i]=pos[x]; pos[x]=i; sum[i]=sum[i-1]+x; } build(root); while(m--){ scanf("%d%d",&l,&r); if(sum[r]-sum[l-1]!=(LL)(r-l+1)*(r-l+2)/2){ puts("NO"); continue ; } ans=Query(l,r,root); if(ans<l) puts("YES"); else puts("NO"); } } return 0;}
1 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 (线段树||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(线段树)
- HDOJ 题目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 HASH随机算法
- iOS快速开发框架Bee-Framework应用和解析(三) --- Message, Model, Signal
- OpenCV:使用Kalman滤波器跟踪一个旋转的点
- 图像处理中的一阶偏导和二阶偏导
- GitHub 优秀的 Android 开源项目
- [sfs2x]SmartFoxServer 2X 的安装
- HDU 5172 GTY's gay friends (线段树)
- linux下Jsoncpp的安装与使用
- 【收藏】经典shell十三问
- ecmall数据库表详解
- 循环某个文件夹下子文件中的文件,删除
- OpenCV:利用级联的haar分类器寻找检测目标
- 【转】WinFrom自定义控件–Panel扩展
- 记录笔记本安装Archlinux的几个折腾之处
- 自定义ListView盘点-CalendarListview(30)