题意:给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. BUPT复试专题—最值问题(2013计院)

    题目描述 给出N个数,求出这N个数中最大值和次大值.注意这里的次大值必须严格小于最大值.输入保证N个数中至少存在两个不同的数. 输入格式 第一行为测试数据的组数T(T≤20).请注意,任意两组测试数据 ...

  2. linux操作系统下查看某rpm包是32bit 还是x64bit的命令

    [root@hosta ~]# rpm -qa --queryformat "%{NAME}-%{VERSION}-%{RELEASE} (%{ARCH})\n" | grep l ...

  3. ng-options bug解决方案(示例)

    情况: 无法获取 ng-model 的值 解决方案: 绑定到对象的属性值上 1.页面 <ion-view hide-nav-bar="true"> <ion-co ...

  4. Expression Tree 学习笔记(一)

    大家可能都知道Expression Tree是.NET 3.5引入的新增功能.不少朋友们已经听说过这一特性,但还没来得及了解.看看博客园里的老赵等诸多牛人,将Expression Tree玩得眼花缭乱 ...

  5. crm操作约会实体

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Crm.Sdk.Messages;     using Microsoft ...

  6. javascript 总结(持续更新)

    1.jQuery对象转DOM对象. jQuery对象转DOM对象有两种方法,[index]和get(index). var $cr = $("#cr"); //jQuery对象 v ...

  7. hdu 4858 项目管理(vector模拟)

    # include <stdio.h> # include <algorithm> # include <string.h> # include <vecto ...

  8. mac下配置eclipse的maven环境

    转自:http://www.cnblogs.com/yqskj/archive/2013/03/30/2990292.html 1.下载maven的bin包,解压,配置到环境变量里面去 1). 首先到 ...

  9. SKStoreReviewController之程序内评价

    在iOS 10.3出现之前,App实现评价方式一般有两种: (一)deep link调用.在app 链接地址后面拼上action=write-review这种方式可以实现程序内评价: (二)App跳转 ...

  10. td 中设置超出宽度显示省略号失效

    td测试内容超出显示省略号时,结果没有显示省略号,而是一直往后显示,且超出了td大小,强行挤大了table. 原因是因为td默认不换行. 解决方法: 1.强制td换行. IE加上word-break: ...