codeforces 840D 主席树

本文介绍了一个关于数组和查询的编程挑战,需要在给定的数组中处理多个查询,每个查询要求找到区间内出现次数超过特定比例的最小元素。通过使用线段树的数据结构,文章详细解释了如何构建和更新线段树,以及如何进行有效的区间查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Destiny
time limit per test 2.5 seconds
memory limit per test 512 megabytes
input standard input
output standard output
Once, Leha found in the left pocket an array consisting of n integers, and in the right pocket q queries of the form l r k. If there are queries, then they must be answered. Answer for the query is minimal x such that x occurs in the interval l r strictly more than  times or  - 1 if there is no such number. Help Leha with such a difficult task.

Input
First line of input data contains two integers n and q (1 ≤ n, q ≤ 3·105) — number of elements in the array and number of queries respectively.

Next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — Leha's array.

Each of next q lines contains three integers l, r and k (1 ≤ l ≤ r ≤ n, 2 ≤ k ≤ 5) — description of the queries.

Output
Output answer for each query in new line.

Examples
input
4 2
1 1 2 2
1 3 2
1 4 2
output
1
-1
input
5 3
1 2 1 3 2
2 5 3
1 2 3
5 5 2
output
2
1
2

题意:给N个数·,Q个询问,每个询问找出区间内出现次数大于所给公式的值中最小的那一个。

思路:

①线段树,每个点都建一棵新的线段树,查询时优先处理左子树,比较一下两个点之间新增的节点数是否满足题意即可。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>

using namespace std;

const int N = 3e5+10;
int ls[N*21],rs[N*21],sum[N*21];
int T[N],a[N];

int tot;
int ans;
void update(int& now,int pre,int l,int r,int d)
{
    now=++tot;
    sum[now]=sum[pre]+1;
    ls[now]=ls[pre];
    rs[now]=rs[pre];

    if(l==r)
        return;
    else
    {
        int mid=(l+r)/2;
        if(d<=mid)
            update(ls[now],ls[pre],l,mid,d);
        else
            update(rs[now],rs[pre],mid+1,r,d);
    }
}

void query(int L,int R,int l,int r,int d)
{
    if(sum[R]-sum[L]<=d)
        return ;
    if(l==r)
    {
        if(ans==-1)
            ans=l;
        return ;
    }

    int mid=(l+r)/2;
    if(ans==-1)
        query(ls[L],ls[R],l,mid,d);
    if(ans==-1)
        query(rs[L],rs[R],mid+1,r,d);
}

int main()
{
    int n,q;
    while(~scanf("%d%d",&n,&q))
    {
        tot=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=n;i++)
        {
            update(T[i],T[i-1],1,n,a[i]);
        }

        while(q--)
        {
            int l,r,k;
            scanf("%d%d%d",&l,&r,&k);
            int vv=(r-l+1)/k;
            ans=-1;
            query(T[l-1],T[r],1,n,vv);
            printf("%d\n",ans);
        }
    }
}

 

引用\[1\]中提到了一种形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子。为了使cnt_white - cnt_black尽可能大,可以使用两次形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是链所代表的子的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值