GTY's gay friends 线段树判断区间是否有相同数字
http://acm.hdu.edu.cn/showproblem.php?pid=5172
判断一个区间是否为全排列是:
1、区间总和 = (1 + R - L + 1) * (R - L + 1) / 2;
2、区间没有重复数字
记录数组a[i]表示第i个数上一次在那个位置出现。
那么最需要在[L, R]中a[i]的最大值 >= L的,就是有重复数字了。
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
#define root 1, n, 1
#define lson L, mid, cur << 1
#define rson mid + 1, R, cur << 1 | 1
int n, m;
const int maxn = + ;
LL sum[maxn];
int mx[maxn << ];
int pre[maxn];
int a[maxn];
void pushUp(int cur) {
mx[cur] = max(mx[cur << ], mx[cur << | ]);
}
void build(int L, int R, int cur) {
if (L == R) {
mx[cur] = a[L];
return;
}
int mid = (L + R) >> ;
build(lson);
build(rson);
pushUp(cur);
}
int ask(int be, int en, int L, int R, int cur) {
if (L >= be && R <= en) {
return mx[cur];
}
int mid = (L + R) >> ;
int ans = ;
if (be <= mid) ans = ask(be, en, lson);
if (en > mid) ans = max(ans, ask(be, en, rson));
return ans;
}
void work() {
for (int i = ; i <= n; ++i) pre[i] = ;
for (int i = ; i <= n; ++i) {
int val;
scanf("%d", &val);
a[i] = pre[val];
pre[val] = i;
sum[i] = sum[i - ] + val;
}
build(root);
for (int i = ; i <= m; ++i) {
int L, R;
scanf("%d%d", &L, &R);
LL t = (R - L + ) * 1LL * (R - L + ) / ;
if (t != sum[R] - sum[L - ] || ask(L, R, root) >= L) {
printf("NO\n");
continue;
}
printf("YES\n");
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
while (scanf("%d%d", &n, &m) > ) work();
return ;
}
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 ...
- 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 线段树+前缀和+全排列
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5172 bc(中文):http://bestcoder.hdu.edu.cn/contest ...
- POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )
POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...
- 线段树离散化+区间更新——cf1179C好题
绝对是很好的题 把问题转化成当第i个询问的答案是数值x时是否可行 要判断值x是否可行,只要再将问题转化成a数组里>=x的值数量是否严格大于b数组里的>=x的值 那么线段树叶子结点维护对于值 ...
- Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并
D. Developing Game Pavel is going to make a game of his dream. However, he knows that he can't mak ...
- hiho一下20周 线段树的区间修改
线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题改了改,又出给了 ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
随机推荐
- HDOJ1059(多重部分和问题)
#include<cstdio> #include<cstring> using namespace std; +; ]; int dp[SIZE]; bool check() ...
- Python:内置函数makestrans()、translate()
转于:https://blog.csdn.net/u014351782/article/details/46740297 博主:夜-feng 一.makestrans() 格式: str.maketr ...
- 【转】 Pro Android学习笔记(七七):服务(2):Local Service
目录(?)[-] Local service代码 调用Local ServiceLocal Service client代码 AndroidManifestxml定义Serviceacitivty的l ...
- Uboot启动参数说明
bootcmd=cp.b 0xc4200000 0x7fc0 0x200000 ; bootm // 倒计时到 0 以后,自动执行的指令 bootdelay=2 baudrate=38400 // 串 ...
- python 基础 字符串格式化
print "hello %s %s" % ('wd','pc') c风格 print "hello {1} {0}".format("wd" ...
- linux日常管理-防火墙selinux
关闭防火墙 SELINUX=disabled 可以是三种状态 # enforcing - SELinux security policy is enforced.打开# permissive - SE ...
- shell入门-tr替换字符和split切割大文件
命令:tr 说明:替换字符 格式tr ‘原字符’ ‘新字符’ 可以是范围字符,指定字符 命令:split 选项:-b 50m 1.txt 根据大小分割 单位是b不用单位,单位是兆加m -l 100 ...
- ubuntu下root用户默认密码及修改方法
[ubuntu下root用户默认密码及修改方法] 很多朋友用ubuntu,一般都是装完ubuntu系统,马上就修改root密码了,那么root用户的默认密码是多少,当忘记root用户密码时如何找回呢, ...
- 11、scala类型参数
一.类型参数1 1.介绍 类型参数是什么?类型参数其实就类似于Java中的泛型.先说说Java中的泛型是什么,比如我们有List a = new ArrayList(),接着a.add(1),没问题, ...
- C# 中out 参数 和 ref参数的区别
C#中共有4种参数类型,分别是 传值(by value), 传址 (by reference), 输出参数 (by output), 数组参数 (by array) by value => 传值 ...