HDU 5172
超内存了,呃。。。不知道如何优化了。
首先要判断区间的和是否和1~n的和相等。
再个,记录下每个数字前一次出现的位置,求这些位置的最大值,如果小于左端点,则表示有这样的一个序列。
呃~~~第二个条件当时曾有想过,但认为要在O(1)时间内得出不可能,后来才知道,还有ST算法啊。。。。不让熟练啊。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL __int64 using namespace std;
const int MAX=1000002;
int num[MAX],pre[MAX];
double sum[MAX];
int dp[MAX][21]; void ST(int n){
int i, j, k, m;
k = (int) (log((double)n) / log(2.0));
for(i = 1; i <= n; i++) {
dp[i][0] = num[i];
}
for(j = 1; j <= k; j++) {
for(i = 1; i + (1 << j) - 1 <= n; i++) {
m = i + (1 << (j - 1));
dp[i][j] = max(dp[i][j-1], dp[m][j-1]);
}
}
}
int rmq(int i, int j) {
int k = (int)(log(double(j-i+1)) / log(2.0)), t1;
t1 =max(dp[i][k], dp[j - (1<<k) + 1][k]);
return t1;
} int main(){
int n,m,tmp,l,r,i; double tsum;
while(scanf("%d%d",&n,&m)!=EOF){
sum[0]=0;
memset(pre,0,sizeof(pre));
for(i=1;i<=n;i++){
scanf("%d",&tmp);
sum[i]=sum[i-1]+tmp;
num[i]=pre[tmp];
pre[tmp]=i;
}
ST(n);
while(m--){
scanf("%d%d",&l,&r);
tsum=(r-l+2)*(r-l+1)*1.0/2.0;
if(tsum!=sum[r]-sum[l-1]){
puts("NO");
continue;
}
if(rmq(l,r)<l){
puts("YES");
}
else puts("NO");
}
}
return 0;
}
HDU 5172的更多相关文章
- HDU 5172 GTY's gay friends 线段树+前缀和+全排列
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5172 bc(中文):http://bestcoder.hdu.edu.cn/contest ...
- HDU 5172 GTY's gay friends (线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5172 题意: 给你一个n个数的数组,m次询问,询问在[L, R] 这个区间里面有没有 [1, R-L+ ...
- 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's gay friends 题意:给n个数和m次查询:(1<n,m<1000,000);之后输入n个数值(1 <= ai <= n):问下面m次查询[L,R]中是否存在 ...
- HDU 5172 GTY's gay friends 线段树
GTY's gay friends Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- GTY's gay friends HDU - 5172 线段树
GTY has nn gay friends. To manage them conveniently, every morning he ordered all his gay friends to ...
- hdu 5172(线段树||HASH)
GTY's gay friends Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 5172 GTY's gay friends(线段树最值)
题意: GTY有n个朋友,站成一排,每个人有一个特征值ai. 有m个询问.每次询问给两个数L,R.问你[L,R](即aL...aR)是否是1..(R-L+1)的一个全排列. 是输出YES,否则输出NO ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- Working with SQL Server LocalDB
https://docs.asp.net/en/latest/tutorials/first-mvc-app/working-with-sql.html The ApplicationDbContex ...
- [.Net] Excel导入导出各种方式分析
1.引言 1.1解决哪些问题 现在很多公司用的导出基本上采用的通过gridView导出excel,此种导出存在以下几种问题 1.数据量大的时候有时导出有时会让浏览器卡死,因为导出的excel不是真 ...
- 46.Qt 使用OpenGL绘制立方体
main.cpp #include <QApplication> #include <iostream> #include "vowelcube.h" in ...
- 在linux上加速git clone
在linux上加速git clone 进入终端命令行模式,sudo vim /etc/hosts 编辑hosts文件,添加以下ip-域名,保存退出 151.101.44.249 github.glob ...
- BZOJ 3522 DFS+DP
思路: f[]表示选1个点的 g[]表示选2个点的 dp一下 ans+=(ll)g[k]*deep[k]; g[k]+=(ll)f[k]*deep[k]; f[k]+=deep[k]; 听说有O(n) ...
- Super超级ERP系统---(10)订单打包
订单拣货完成后,需要把订单装箱打包,并打印客户地址信息.订单打包的操作流程先是扫描订单号,然后扫描商品条码. 1.订单打包 打印包装箱面单 2.订单发货 订单打包完成后就等待发货,快递公司来拉货的时 ...
- Unity3d gameObject
using UnityEngine; using System.Collections; public class test : MonoBehaviour { //print只能在MonoBehav ...
- Docker-compose Setup for Self-hosting Development & Deployment Tools
Last week I wrote about my self-hosted Sentry install in 3 Docker containers. This week I want to br ...
- Python框架、库和软件资源大全(整理篇)
有少量修改,请访问原始链接.PythonWIn的exe安装包;http://www.lfd.uci.edu/~gohlke/pythonlibs/ 原文链接:codecloud.net/python- ...
- 基于Linux/C++简单线程池的实现
我们知道Java语言对于多线程的支持十分丰富,JDK本身提供了很多性能优良的库,包括ThreadPoolExecutor和ScheduleThreadPoolExecutor等.C++11中的STL也 ...