题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5172

题意:

  给你一个n个数的数组,m次询问,询问在[L, R] 这个区间里面有没有 [1, R-L+1] 的数。

题解:

  判断有没有 1 ~ R-L+1 其实就是判断这一段区间和是不是等于 (R-L+1)*(R-L+1+1)/ 2 。

  当然还有就是每一个数只能出现1次。

  这个问题我们应该怎么解决呢。

  我们可以记录第i个数x 的前一个x出现的位置。如果x的前一个x也出现在[L, R]里面,那么这一段区间就不是1~R-L+1 的全排列。

  所以我们可以O(n) 处理位置为i 的数x 的前一个数的位置 pre[i] 。

  用线段树维护这个pre[i] 的区间最大值。在[L, R] 这个区间的pre[i] 的最大值如果在[L, R] , 就NO。

  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define rep(a, b) for(int a = 0;a<b;a++)
#define rep1(a, b) for(int a = 1;a<=b;a++)
#define pb push_back
#define mp make_pair
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
int n, m, x, l, r;
int pre[maxn], now[maxn];
LL sum[maxn];
int maxv[*maxn];
int query(int ql, int qr, int o, int L, int R)
{
int M = (L+R)>>;
int ans = -;
if(ql <= L && R <= qr) return maxv[o];
if(ql <= M) ans = max(ans, query(ql, qr, o*, L, M));
if(M < qr) ans = max(ans, query(ql, qr, o*+, M+, R));
return ans;
}
void update(int o, int L, int R)
{
if(L==R){
maxv[o] = pre[L];
return;
}
int M = (L+R)>>;
update(o*, L, M);
update(o*+, M+, R);
maxv[o] = max(maxv[o*], maxv[o*+]);
}
void solve()
{
ms(now, );sum[] = ;
for(int i = ;i<=n;i++){
scanf("%d", &x);
sum[i] = sum[i-] + 1LL*x;
pre[i] = now[x];
now[x] = i;
}
update(,,n);
while(m--){
scanf("%d%d",&l, &r);
LL len = 1LL*(r-l+);
if(sum[r]-sum[l-] != len*(len+)/){
printf("NO\n");continue;
}
int Max = query(l, r, , , n);
if(Max<l){
printf("YES\n");
}
else{
printf("NO\n");
}
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// IOS
while(~scanf("%d%d", &n, &m))
solve();
return ;
}

HDU 5172 GTY's gay friends (线段树)的更多相关文章

  1. HDU 5172 GTY's gay friends 线段树

    GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. HDU 5172 GTY's gay friends 线段树+前缀和+全排列

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5172 bc(中文):http://bestcoder.hdu.edu.cn/contest ...

  3. BestCoder Round #29 1003 (hdu 5172) GTY's gay friends [线段树 判不同 预处理 好题]

    传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  4. hdu 5172 GTY's gay friends(线段树最值)

    题意: GTY有n个朋友,站成一排,每个人有一个特征值ai. 有m个询问.每次询问给两个数L,R.问你[L,R](即aL...aR)是否是1..(R-L+1)的一个全排列. 是输出YES,否则输出NO ...

  5. hdu 5172 GTY's gay friends

    GTY's gay friends 题意:给n个数和m次查询:(1<n,m<1000,000);之后输入n个数值(1 <= ai <= n):问下面m次查询[L,R]中是否存在 ...

  6. GTY's gay friends 线段树判断区间是否有相同数字

    http://acm.hdu.edu.cn/showproblem.php?pid=5172 判断一个区间是否为全排列是: 1.区间总和 = (1 + R - L + 1) * (R - L + 1) ...

  7. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  9. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

随机推荐

  1. [转帖]Windows下cwRsyncServer双机连续同步部署

    Windows下cwRsyncServer双机连续同步部署 https://www.cnblogs.com/nulige/p/7607503.html 找时间做一下测试 应该能更好的实现 自动部署的功 ...

  2. 右键windows terminal here无法进入当前目录

    很久没写水笔了,简单记一水 使用windows terminal的基本上都自己改过注册表,添加到右键windows terminal here吧,用着很方便,哪里不会点哪里. 我起初删除掉starti ...

  3. bind call apply 的区别和使用

    bind call apply 的区别和使用:https://www.jianshu.com/p/015f9f15d6b3 在讲这个之前要理解一些概念,这些概念很重要,有人说过学会了javascrip ...

  4. 剑指offer-从尾到头打印链表-链表-python

    题目描述 输入一个链表,按链表从尾到头的顺序返回一个ArrayList. 把链表依次放入list里面,反向打印 # -*- coding:utf-8 -*- # class ListNode: # d ...

  5. 如何增强Linux和Unix服务器系统安全性

    众所周知,网络安全是一个非常重要的课题,而 Linux 和 unix 又是一种服务器上运行最广告的操作系统,下面本文将就加强一些适当的配置来防止一些安全问题的发生,以增强Linux/Unix服务器系统 ...

  6. SpringBoot_03mybatisPlus

    注意: mybatisPlus默认加载resources下的mapper文件夹下的xml文件 默认将数据库表的字段用驼峰标识转换成实体类的属性 官方网站: https://mp.baomidou.co ...

  7. Cross-Origin-Resource-Sharing-Solutions

    from:https://github.com/hijiangtao/hijiangtao.github.io/blob/master/_posts/2017-06-13-Cross-Origin-R ...

  8. 前端开发-css基础入门

    CSS  cascading(层叠) style(样式) sheet(表) css注释 /* 注释内容 */ 快捷键:ctrl ? 引入方式 <!-- 1.行间样式 --> <div ...

  9. 来到园子的第two天!!!!!

    经过这几天的打字练习  从练习的第一天到今天(应该是第三天吧) 从67到133字/分钟,我练的是英文文章测试,总的来说挺不容易 ,感觉后几天提升就不好提了呀 不说废话了  简单说一下今天的收获吧 都是 ...

  10. 长沙理工大学第十二届ACM大赛-重现赛 B 日历中的数字 (实现)

    链接:https://ac.nowcoder.com/acm/contest/1/B来源:牛客网 全屏查看题目   时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他 ...