CF 840 D

CF 840 D

clf大佬告诉我,直接主席树是\(n\times \log(n)\)

这是为什么呢.

首先最多有5个叶子节点是出现次数大于等于\(n\over 5\)哒,然后上述叶子也可能不是叶子啊.

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int MAXN=300000;
const int INF=0x3f3f3f3f;
struct Node{int w,lc,rc;};
int n,q;
int root[MAXN+10];
Node t[20*MAXN+10];int tot;
void insert(int& o,int l,int r,int q)
{
    t[++tot]=t[o];o=tot;
    if(l==r){++t[o].w;return;}
    int mid=(l+r)>>1;
    if(q<=mid)insert(t[o].lc,l,mid,q);
    else insert(t[o].rc,mid+1,r,q);
    t[o].w=t[t[o].lc].w+t[t[o].rc].w;
}
int query(int o1,int o2,int l,int r,int q)
{
    if(l==r)return l;
    int mid=(l+r)>>1,res=INF;
    if(t[t[o2].lc].w-t[t[o1].lc].w>=q)res=min(res,query(t[o1].lc,t[o2].lc,l,mid,q));
    if(t[t[o2].rc].w-t[t[o1].rc].w>=q)res=min(res,query(t[o1].rc,t[o2].rc,mid+1,r,q));
    return res;
}
int main()
{
    scanf("%d%d",&n,&q);
    for(int i=1,a;i<=n;++i)
    {
        scanf("%d",&a);
        root[i]=root[i-1];
        insert(root[i],1,n,a);
    }
    while(q--)
    {
        int l,r,k,res;
        scanf("%d%d%d",&l,&r,&k);
        res=query(root[l-1],root[r],1,n,(r-l+1)/k+1);
        printf("%d\n",res>=INF?-1:res);
    }
    return 0;
}

转载于:https://www.cnblogs.com/DOlaBMOon/p/7517496.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值