HDU 5172 GTY's gay friends (线段树)
题目链接: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 (线段树)的更多相关文章
- HDU 5172 GTY's gay friends 线段树
GTY's gay friends Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5172 GTY's gay friends 线段树+前缀和+全排列
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5172 bc(中文):http://bestcoder.hdu.edu.cn/contest ...
- 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 ...
- hdu 5172 GTY's gay friends(线段树最值)
题意: GTY有n个朋友,站成一排,每个人有一个特征值ai. 有m个询问.每次询问给两个数L,R.问你[L,R](即aL...aR)是否是1..(R-L+1)的一个全排列. 是输出YES,否则输出NO ...
- hdu 5172 GTY's gay friends
GTY's gay friends 题意:给n个数和m次查询:(1<n,m<1000,000);之后输入n个数值(1 <= ai <= n):问下面m次查询[L,R]中是否存在 ...
- GTY's gay friends 线段树判断区间是否有相同数字
http://acm.hdu.edu.cn/showproblem.php?pid=5172 判断一个区间是否为全排列是: 1.区间总和 = (1 + R - L + 1) * (R - L + 1) ...
- HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
Multiply game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
随机推荐
- Spark启动流程(Standalone)- master源码
Master源码 package org.apache.spark.deploy.master //伴生类 private[deploy] class Master( override val rpc ...
- [LeetCode] 210. 课程表 II
题目链接:https://leetcode-cn.com/problems/course-schedule-ii/ 题目描述: 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前 ...
- 理解 JavaScript 闭包
这是本系列的第 4 篇文章. 作为 JS 初学者,第一次接触闭包的概念是因为写出了类似下面的代码: for (var i = 0; i < helpText.length; i++) { var ...
- django后台集成富文本编辑器Tinymce的使用
富文本编辑器Tinymce是使用步骤: 1.首先去python的模块包的网站下载一个django-tinymce的包 2.下载上图的安装包,然后解压,进入文件夹,执行: (pychrm直接运行命令pi ...
- 【算法入门】深度优先搜索(DFS)
深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...
- Laravel 学习笔记之文件上传
自定义添加磁盘——upload 位置:config/filesystems.php 'disks' => [ 'local' => [ 'driver' => 'local', 'r ...
- POI读取文件的最佳实践
POI是 Apache 旗下一款读写微软家文档声名显赫的类库.应该很多人在做报表的导出,或者创建 word 文档以及读取之类的都是用过 POI.POI 也的确对于这些操作带来很大的便利性.我最近做的一 ...
- css厂商前缀
在vue中写css,不要加厂商前缀,vue-cli会在打包时自动生成
- 混合加密算法(RSA和DES)
一.混合加密的理由 a.前面提及了RSA加解密算法和DES加解密算法这两种加解密算法,由于随着计算机系统能力的不断发展,DES的安全性比它刚出现时会弱得多,追溯历史破解DES的案例层出不穷,一台实际的 ...
- 面试经典&&竞赛——二叉树
To record her trees for future generations, she wrote down two strings for each tree: a preorder tra ...