poj 3368 Frequent values(线段树解法)

题目链接:http://poj.org/problem?id=3368

题目大意:给你一段不下降的序列,求给定区间里出现次数最多的那个数字的次数。

思路:首先看到这题时,第一感觉线段树,但是仔细一看问题来啦,用线段数我怎么才能计算出某段区间里出现的那个数,因为出现最多的那个数可能不是在他它的左儿子上也不是在它的右儿子上,可能在当他们合并成一个区间时就出现啦,但是这儿我们需要注意的就是,题目给的是一段不下降的序列,那么突破口就出来啦,因为如果出现相同的数字,那么它们一定是连续的。所以我们只需要在普通的线段树中加上两个变量用来记录这段区间他的最左边那个数连续数的个数和在最右边那个数连续的个数。在我们求这段区间最大次数,需要多做一次比较(在这段区间的两个子区间中,当左子区间最右边数和右区间最左边的数相等时,取三个数的最大值)。。。。。

code:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#define Max 100010

using namespace std;

struct Node
{
    int l,r;
    int amax,lmaxm,rmax;    //amax用来记录这段区间出现最多的次数的个数,lmax用来记录最左边那个数在这段区间连续的个数,rmax用来记录最右边那个数在这段区间连续的个数

}node[Max*4];
int a[Max];

void build(int u,int l,int r)
{
    node[u].l=l;
    node[u].r=r;
    if(l==r)
    {
        node[u].amax=node[u].lmaxm=node[u].rmax=1;
        return;
    }
    int mid=(l+r)/2;
    build(u*2,l,mid);
    build(u*2+1,mid+1,r);
    int tem;
    if(a[node[u*2].r]==a[node[u*2+1].l])
    {
        tem=node[u*2].rmax+node[u*2+1].lmaxm;
    }
    else
    {
        tem=0;
    }
    node[u].amax=max(max(node[u*2].amax,node[u*2+1].amax),tem);
    node[u].lmaxm=node[u*2].lmaxm;                //大区间的最左边的那个数和左子区间那个数值相同的
    if(node[u*2].lmaxm==mid-l+1&&a[node[u*2].r]==a[node[u*2+1].l])   //判断左子区间里的数是否相同并且和右子区间最左边那个数相同,相同则大区间lmax需要变化
    {
        node[u].lmaxm+=node[u*2+1].lmaxm;
    }
    node[u].rmax=node[u*2+1].rmax; //<span style="font-family: Arial, Helvetica, sans-serif;">大区间的最右边的那个数和右子区间那个数值相同的</span>

    if(node[u*2+1].rmax==r-mid&&a[node[u*2].r]==a[node[u*2+1].l])    <span style="font-family: Arial, Helvetica, sans-serif;">//判断右子区间里的数是否相同并且和左子区间最右边那个数相同,相同则大区间rmax需要变化</span>

    {
        node[u].rmax+=node[u*2].rmax;
    }
}

int query(int u,int l,int r)
{
    if(node[u].l==l&&node[u].r==r)
    {
        return node[u].amax;
    }
    int mid=(node[u].l+node[u].r)/2;
    if(r<=mid) return query(u*2,l,r);
    else if(l>mid) return query(u*2+1,l,r);
    else
    {
        int a1=query(u*2,l,mid);
        int a2=query(u*2+1,mid+1,r);
        int a3=0;
        if(a[node[u*2].r]==a[node[u*2+1].l])     //比较
        {
            a3=min(node[u*2].rmax,mid-l+1)+min(node[u*2+1].lmaxm,r-mid);
        }
        return max(max(a1,a2),a3);
    }
}

int main()
{
    int n,p,x,y,i;
    while(scanf("%d",&n)==1)
    {
        if(n==0) break;
        scanf("%d",&p);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        build(1,1,n);
        for(i=0;i<p;i++)
        {
            scanf("%d%d",&x,&y);
            printf("%d\n",query(1,x,y));
        }
    }
    return 0;
}

poj 3368 Frequent values(线段树解法),布布扣,bubuko.com

时间: 2024-08-24 10:17:14

poj 3368 Frequent values(线段树解法)的相关文章

POJ 3368 Frequent values

Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13051 Accepted: 4792 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 consisti

POJ3368 Frequent values 线段树

N个数为非递减顺序,给定范围l,r,求[l,r]区间内数字出现频率最高的次数. 可以用线段树来做.先说查询,我们设节点P对应的区间为[a, b],左孩子节点为p1,右孩子节点为p2,那么 P也许不等于 max(p1 , p2),原因是如果p1中频率较低的某个数与p2中出现频率较低的某个数是同一个数,并且两者出现次数加起来大于max(p1, p2),但是,题目说N个数为非递减顺序排列,所以这个可能只会发生在p1的右端和p2的左端对应的数是同一个数,因此,我们还要处理这第三个可能. 查询的问题解决,

POJ 2828 Buy Tickets 线段树解法

此题应用线段树的方法非常巧妙.没做过真的难想得出是这么想的. 是一个逆向思维的运用. 其实一看到这道题目我就想到要运用逆向思维的了,但是就是没那么容易想通的. 思路: 1 要从后面往前更新线段树 2 线段树记录的是当前区间还剩下多少个记录空间 3 因为后面的插入会使得前面的插入往后退,那么前面插入的下标如果大于前面可以插入的空间,就需要往后退了. 好难理解的操作.仔细观察一下下面update这个函数吧. 二分地去查找适合的插入位置,把插入操作时间效率提高到 O(lgn),如果使用一般方法插入操作

poj 3368 Frequent values 解题报告

题目链接:http://poj.org/problem?id=3368 题目意思:给出一段 n 个数的序列你,对于区间 [l, r] 的询问,找出 出现频率最高的数的次数.考虑到序列中的数是非递减的,也就是相同的数会连续不间断地在一起,于是就才有了代码中这个部分来预判了: if (s > t)        printf("%d\n", ans); 这个人写RMQ 写得不错:http://dongxicheng.org/structure/lca-rmq/ 这题就是套模板的,纪念

POJ 3368 Frequent values RMQ ST算法/线段树

                                                     Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15229   Accepted: 5550 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In

poj 3368 Frequent values(线段树)

Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13516   Accepted: 4971 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

[RMQ] [线段树] POJ 3368 Frequent Values

一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同的一段 (RMQ) 但是要注意 to[i] 大于查询范围的情况, 以及RMQ时 x < y 的情况!! AC代码: #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib>

[题解]poj 3368 Frequent values

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16537   Accepted: 5981 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 consisting of indice

POJ 3368 Frequent values (基础RMQ)

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