Frequent values【线段树最长相等区间】

题面

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 frequent value among the integers ai , ... , aj.

Input

The input consists of several test cases. Each test case starts with a line containing two integers n and q(1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the 
query.

The last test case is followed by a line containing a single 0.

Output

For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

Sample Input

10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0

Sample Output

1
4
3

  一道线段树的区间最长相等的模板题,但是这道题有坑点,就是你在pushup()函数的时候,在进行左右区间的扩增的时候,一定要判断是否是等值。

    if(tree[rt].l==mid-l+1 && tree[rt].val_l==tree[rt<<1|1].val_l) tree[rt].l += tree[rt<<1|1].l;
    if(tree[rt].r==r-mid && tree[rt].val_r==tree[rt<<1].val_r) tree[rt].r += tree[rt<<1].r;

  就是这样的,一开始没判断,所以不对。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN=1e5+5;
int N, Q, a[maxN];
struct node
{
    int l, r, mid, val_l, val_r;
    node(int a=0, int b=0, int c=0, int d=0, int f=0):l(a), r(b), mid(c), val_l(d), val_r(f) {}
}tree[maxN<<2];
void pushup(int rt, int l, int r)
{
    tree[rt].val_l = tree[rt<<1].val_l;
    tree[rt].val_r = tree[rt<<1|1].val_r;
    if(tree[rt].val_l == tree[rt].val_r)
    {
        tree[rt].l = tree[rt].r = tree[rt].mid = (r-l+1);
        return;
    }
    int mid=(l+r)>>1;
    tree[rt].l = tree[rt<<1].l;
    tree[rt].r = tree[rt<<1|1].r;
    if(tree[rt].l==mid-l+1 && tree[rt].val_l==tree[rt<<1|1].val_l) tree[rt].l += tree[rt<<1|1].l;
    if(tree[rt].r==r-mid && tree[rt].val_r==tree[rt<<1].val_r) tree[rt].r += tree[rt<<1].r;
    tree[rt].mid = max(tree[rt<<1].mid, tree[rt<<1|1].mid);
    tree[rt].mid = max(tree[rt].mid, tree[rt].l);
    tree[rt].mid = max(tree[rt].mid, tree[rt].r);
    if(tree[rt<<1].val_r == tree[rt<<1|1].val_l) tree[rt].mid=max(tree[rt].mid, tree[rt<<1].r+tree[rt<<1|1].l);
}
void buildTree(int rt, int l, int r)
{
    if(l==r)
    {
        tree[rt]=node(1, 1, 1, a[l], a[r]);
        return;
    }
    int mid=(l+r)>>1;
    buildTree(rt<<1, l, mid);
    buildTree(rt<<1|1, mid+1, r);
    pushup(rt, l, r);
}
int query(int rt, int l, int r, int ql, int qr)
{
    if(ql<=l && qr>=r)
    {
        return tree[rt].mid;
    }
    int mid=(l+r)>>1;
    if(ql>mid) return query(rt<<1|1, mid+1, r, ql, qr);
    else if(qr<=mid) return query(rt<<1, l, mid, ql, qr);
    else
    {
        int ans=max(query(rt<<1, l, mid, ql, mid), query(rt<<1|1, mid+1, r, mid+1, qr));
        if(tree[rt<<1].val_r == tree[rt<<1|1].val_l)
        {
            int l_r=tree[rt<<1].r, r_l=tree[rt<<1|1].l;
            l_r=min(l_r, mid-ql+1);
            r_l=min(r_l, qr-mid);
            ans=max(ans, l_r+r_l);
        }
        return ans;
    }
}
int main()
{
    while(scanf("%d", &N) && N)
    {
        scanf("%d", &Q);
        for(int i=1; i<=N; i++) scanf("%d", &a[i]);
        buildTree(1, 1, N);
        int l, r;
        while(Q--)
        {
            scanf("%d%d", &l, &r);
            if(l>r) swap(l, r);
            printf("%d\n", query(1, 1, N, l, r));
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值