POJ 3419 (rmq)

这道题是rmq,再加上一个解决溢出。

刚开始我也想过用rmq,虽然不知道它叫什么,但是我知道应该这样做。可是后来没想到这道题的特殊性,也就是解决溢出的方法,就放弃了。

rmq可以用线段树,也可以用dp。  这道题都可以过的,而且线段树要快一些。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define max(a,b) (a > b ? a : b)
const int maxm = 2000010;
const int maxn = 200010;

int v[maxm], n, m;
int data[maxn];
int pre[maxn], len[maxn], lg[maxn];
int rmq[maxn][25];

void rmq_init()
{
    for(int i = 0; i <= lg[n]; i++)
        for(int j = 0; j + (1 << i) - 1 < n; j++)
        {
            if(i == 0)
                rmq[j][i] = len[j];
            else
                rmq[j][i] = max(rmq[j][i - 1], rmq[j + (1 << (i - 1))][i - 1]);
        }
}

int rmq_max(int l, int r)
{
    if(r < l)
        return 0;
    int t = r - l + 1;
    return max(rmq[l][lg[t]], rmq[r - (1 << (lg[t])) + 1][lg[t]]);
}

int main()
{
    lg[0] = -1;
     for(int i = 1; i < maxn; i++)
        lg[i]=lg[i >> 1]+1;
    while(cin >> n)
    {
        cin >> m;
        for(int i = 0; i < n; i++)
            scanf("%d", &data[i]);

        memset(v, -1, sizeof(v));
        int p1 = 0;
        int p2 = 0;
        while(p2 < n)
        {
            if(v[data[p2] + 1000000] >= p1)
            {
                for(int i = p1; i <= v[data[p2] + 1000000]; i++)
                {
                    len[i] = p2 - i;
                    pre[i] = p2 - 1;
                }

                p1 = v[1000000 + data[p2]] + 1;
            }
            v[1000000 + data[p2]] = p2;
            p2++;
        }
        for(int i = p1; i < n; i++)
        {
            len[i] = n - i;
            pre[i] = n - 1;
        }
        /*for(int i = 0; i < n; i++)
        cout << pre[i]  << " "  ;
        cout << endl;*/
        int l, r, ans;
        rmq_init();
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d", &l, &r);
            int t = lower_bound(pre + l, pre + r, r) - pre;
            //cout << endl << t << endl;
            ans = r - t + 1;
            //cout << ans << endl << endl;
            r -= ans;
            //cout << r << endl;
            ans = max(ans, rmq_max(l, r));
           printf("%d\n",ans);
        }
    }
}

POJ 3419 (rmq)

时间: 2024-11-05 12:29:14

POJ 3419 (rmq)的相关文章

【POJ 3368】 Frequent values(RMQ)

[POJ 3368] Frequent values(RMQ) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15813   Accepted: 5749 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given seve

poj 3368(RMQ简单应用)

Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13317   Accepted: 4894 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries cons

UVA 11235 Frequent values(RMQ)

Frequent values TimeLimit:3000Ms You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most f

POJ 2253-Frogger (Prim)

题目链接:Frogger 题意:两只青蛙,A和B,A想到B哪里去,但是A得弹跳有限制,所以不能直接到B,但是有其他的石头作为过渡点,可以通过他们到达B,问A到B的所有路径中,它弹跳最大的跨度的最小值 PS:最小生成树过的,刚开始用Dijstra做,Kao,精度损失的厉害,对于Dijksra的变形不大会变啊,看了Discuss有人用最小生成树过,我一划拉,还真是,敲了,就过了,等会研究研究最短路的各种变形,有模板不会变,不会灵活应用,渣渣就是渣渣. ME               Time 10

poj 1861(最小生成树)

Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access

POJ 2352 (stars)

[题意描述] 就是给定n个星星的x,y坐标,y坐标按照从小到大的顺序进行排列,x坐标随机排列.下面求对于每个星星而言,其它星星的x,y的坐标都小于等于该星星的数目,然后输出所有的情况. [思路分析] 我们这道题可以采用树状数组求解,将x+1作为树状数组的底标. [AC代码] #include<iostream> #include<bitset> #include<cstdio> #include<cstring> #include<algorithm&

poj Sudoku(数独) DFS

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13665   Accepted: 6767   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure.

hdu 3183 A Magic Lamp(RMQ)

A Magic Lamp                                                                               Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Kiki likes traveling. One day she finds a magic lamp, u

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (