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 题意: 给一个序列由 ...
随机推荐
- Docker数据持久化及实战(Nginx+Spring Boot项目+MySQL)
Docker数据持久化: Volume: (1)创建mysql数据库的container docker run -d --name mysql01 -e MYSQL_ROOT_PASSWORD= my ...
- MySQL-快速入门(7)索引
1.什么是索引 索引是对数据库表中一列或者多列的值进行排序的一种结构.索引是在存储引擎中实现的,每种存储引擎中的索引不一定完全相同. MySQL中索引的存储类型有两种:btree和hash.MyISA ...
- [2019沈阳网络赛D题]Dawn-K's water(点分治)
题目链接 题意为求出树上任意点对的距离对3取余的和. 比赛上听到题意就知道是点分治了,但是越写越不对劲,交之前就觉得会T,果不其然T了.修修改改结果队友写了发dp直接就过了Orz. 赛后想了想维护的东 ...
- [BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT)
[BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT) 题面 小C有一个集合S,里面的元素都是小于质数M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数 ...
- 批处理遍历文件夹执行git pull
echo off & color 0A for /d %%f in (D:\www\*) do ( D: cd %%f chdir git pull ) pause 遍历D:\www\这个文件 ...
- php手动实现ip2long和long2ip
php手动实现ip2long和long2ip /** * 测试 */ public function testipAction() { $ip = '10.58.101.175'; echo ip2l ...
- 在JSP中<%= >,<%! %>,<% %>所代表的含义
<%! %>:是jsp中的声明标签,通常声明全局变量,常量,方法等. <% %>:<% java代码 %>,其中可以包含局部变量,java语句等. <%= % ...
- HTML回顾之表格
HTML表格 由什么组成? 表格由<table>标签来定义.每个表格有若干行(<tr>标签来定义),每行被分割成若干单元格(<td>标签来定义). td值表格数据, ...
- 为啥要使用Hessian
1.为啥要使用Hessian? 有需求就有市场,挨踢界也是一样,想要实现远程调用,Hessian就应运而生. 场景:有一个后台系统,基本上所有的用户管理都在这个系统里操作,其中有一个方法是添加用户的方 ...
- String.IsNullOrEmpty官方示例
// This example demonstrates the String.IsNullOrEmpty() method using System; class Sample { public s ...