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
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;
}