题意:给n个数(n<=200000),每个数的绝对值不超过(10^6),有m个查询(m<=200000),每次查询区间[a,b]中连续的没有相同数的的最大长度。

析:由于n太大,无法暴力,也承受不了O(n*n)的复杂度,只能是O(nlogn),首先是用f[i] 表示每个数 i 为左端点,向右可以最多到达的点为f[i],

那么这个dp很好转移,f[i] = max(f[i+1], last[a[i]]),其中last数组是用来记录上次序列数中a[i]的出现的位置。

那么对于给定的区间[l, r] g[i] = min(r, f[i]) - i + 1,而答案为 ans = max{ g[i] l <= i<= r}。这样复杂度也太大,我们从f上下手、

f 数组 一定是非降序的,所以一定存在一个位置pos,

当 i < pos 时,g[i] = f[i] - i + 1,这个我们可以用rmq 来维护最大值。

当 i >= pos 时,g[i] = r - i + 1,这个我们可以直接求出。

对于pos我们可以用二分快速求出,利用 f 的单调性。总时间复杂度就是 O(nlogn)。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 2e5 + 10;
const int mod = 1e6 + 10;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn];
int f[maxn], last[mod*3]; struct Rmq{
int dp[maxn][30];
void init(int *a){
for(int i = 0; i < n; ++i) dp[i][0] = a[i] - i + 1;
for(int j = 1; (1<<j) <= n; ++j)
for(int i = 0; i + (1<<j) <= n; ++i)
dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
} int query(int l, int r){
int k = log(r-l+1.0) / log(2.0);
return max(dp[l][k], dp[r-(1<<k)+1][k]);
}
}; Rmq rmq; int main(){ while(scanf("%d %d", &n, &m) == 2){
for(int i = 0; i < n; ++i){
scanf("%d", a+i);
a[i] += mod;
last[a[i]] = n;
}
f[n-1] = n-1; last[a[n-1]] = n-1;
for(int i = n-2; i >= 0; --i){
f[i] = min(f[i+1], last[a[i]]-1);
last[a[i]] = i;
}
rmq.init(f);
while(m--){
int l, r;
scanf("%d %d", &l, &r);
int pos = lower_bound(f+l, f+n, r) - f;
int ans = r - pos + 1;
--pos;
if(pos > l) ans = max(ans, rmq.query(l, pos));
printf("%d\n", ans);
}
}
return 0;
}

POJ 3419 Difference Is Beautiful (DP + 二分 + rmq)的更多相关文章

  1. POJ 3419 Difference Is Beautiful(RMQ+二分 或者 模拟)

    Difference Is Beautiful Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%lld & %l ...

  2. POJ 3419 Difference Is Beautiful(RMQ变形)

    题意:N个数,M个询问,每个询问为一个区间,求区间最长连续子序列,要求每个数都不同(perfect sequence,简称PS). 题解:很容易求出以每个数为结尾的ps,也就是求区间的最大值.有一个不 ...

  3. POJ 3419 Difference Is Beautiful

    先处理出每一个i位置向左最远能到达的位置L[i].每一次询问,要找到L,R区间中的p位置,p位置左边的L[i]都是小于L的,p位置开始,到R位置,L[i]都大于等于L,对于前者,最大值为p-L,后者求 ...

  4. 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 题目大意: 给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离 ...

  5. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

  6. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  7. poj3208 Apocalypse Someday 数位dp+二分 求第K(K <= 5*107)个有连续3个6的数。

    /** 题目:poj3208 Apocalypse Someday 链接:http://poj.org/problem?id=3208 题意:求第K(K <= 5*107)个有连续3个6的数. ...

  8. HDU 3433 (DP + 二分) A Task Process

    题意: 有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少 思路: DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到. dp[i ...

  9. POJ-2533最长上升子序列(DP+二分)(优化版)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41944   Acc ...

随机推荐

  1. [Testing] Config jest to test Javascript Application -- Part 3

    Run Jest Watch Mode by default locally with is-ci-cli In CI, we don’t want to start the tests in wat ...

  2. cocos2d-x wp8 中文显示问题

    cocos2d-x项目在wp8下中文显示不了.有两个原因: 1. 本身字符串的utf8编码, 有问题. 2. 显示中文所须要的字体,缺失. 要解决第二个问题非常easy,在fonts下加入一个中文字体 ...

  3. 消息列队 php 基于redis 实现

    说明 消息列队 基于PHP 实现. 之前 用python 的 flower 实现了 列队. 今天这里我们用的是 PHP 来实现: 在实际的业务环境中 PHP 用的多些: PHP 实现列队 最重要的是用 ...

  4. lua 异常 错误处理 pcall

    lua 错误处理 匿名函数 if pcall(function () local s=object.data[1]['type'] end) then return object.data[1]['t ...

  5. HTML--比较实用的小例子

    常用的前端实例: 1略 2.在网页商城中的图片当我们把鼠标放上去之后,图片会显示一个有颜色的外边框,图片某一部分的字体的颜色并发生改变 鼠标放上去之前 鼠标放上去之后: 实现的代码: <!DOC ...

  6. leetcode题解||Container With Most Water问题

    problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...

  7. 【转载】究竟啥才是互联网架构“高并发”

    一.什么是高并发 高并发(High Concurrency)是互联网分布式系统架构设计中必须考虑的因素之一,它通常是指,通过设计保证系统能够同时并行处理很多请求. 高并发相关常用的一些指标有响应时间( ...

  8. 如何动态地给vSphere虚拟机模板注入信息

    在做vSphere自动化安装过程中,遇到这样一个需求:将vCenter Server做成模板,在给用户自动化装好vSphere后, 下载vCenter Server模板并启动虚拟机,然后将vCente ...

  9. iGrimaceVX3.0和1.44在线源手机直接安装教程

    [第一步] 先安装好三个组件设备必须是苹果越狱好后 确定6点几跟7点几的版本号才干够 首先打开cydia 选开发人员 以下 点软件源 点右上角编辑  1加入源 apt.25pp.com和IG包下载源a ...

  10. cocos2dx学习进度

    将cocos2dx实战上面的例子都自己过一遍,手动敲一边里面的代码,瓦片地图,地图滚动,碰撞,容器类,现在搞到了fileUtils相关的了,哦,官方叫做数据持久化,一不小心就6点了,时间过得太快了,看 ...