CodeChef:Chef and Problems(分块 & 二分)

一二三系列之CodeChef分块——Chef and Churu,Chef and Problems,Children Trips 每道分块都nm写了对拍,平均一道题调2h,淦! 阅读详情

Chef was invited to the party of N people numbered from 1 to N. Chef knows the growth of all the people, i.e. he knows the growth of the ith person is denoted by an integer Ainot exceeding M.

Chef decided to have some fun. At first, he forms K groups of people. The ith group consists of all the people numbered from Li to Ri. Groups may overlap too.

For each group, Chef wants to know the following information: the maximum difference between the numberings of two people having same growth. Formally, Chef wants to know the following:

max{|x − y| : Li ≤ x, y ≤ Ri and Ax = Ay}

Please help Chef to have fun.

Input

There is only one test case in one test file.

The first line of input contains three space-separated integers N, M and K, denoting the number of people, the maximum growth and number of groups respectively. The second line contains N space-separated integers A1, A2, ..., AN denoting the growth of people. Then the ith line of the next K lines contains two space-separated integers Li, Ri, denoting the ith group.

Output

For each group, output the integer denoting the maximum difference between numbering of two people having same growth in a single line.

Constraints and Subtasks

  • 1 ≤ Ai ≤ M
  • 1 ≤ Li ≤ Ri ≤ N

Subtask 1: 20 points

  • 1 ≤ N, M, K ≤ 1000 = 103

Subtask 2: 80 points

  • 1 ≤ N, M, K ≤ 100000 = 105

Example

Input:
7 7 5
4 5 6 6 5 7 4
6 6
5 6
3 5
3 7
1 7

Output:
0
0
1
1
6

Explanation

Group 1. There is only one person in the group. Thus the maximum difference of numbers should be 0.

Group 2. There are two persons in the group. Their growth are A5 = 5 and A6 = 7. Thus there is no pair of persons who have the same growth. Thus the answer for this group will also be 0.

Group 3. There are three persons in the group. Their growth are A3 = 6, A4 = 6 and A5= 5. Here person 3 and person 4 has the same growth. Thus the answer is |4 − 3| = 1.

Group 4. There are more persons than the group 3. But they has different growth, other than person 3 and person 4.Thus the answer is also |4 − 3| = 1.

Group 5. This group contains all the people, and person 1 and person 7 has the same growth A1 = A7 = 4. So the answer is |7 − 1| = 6.


题意:有N个数,每个数范围在[1,m],K个询问,每个询问给出两个数L,R,要求找出区间[L,R]内同一个数字的最远距离。

思路:分块处理,1e5分的块长度100左右可以保证效率,计算L,R时,取L,R中间预处理好的块的答案,再枚举左右两边的数即可。

# include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+3;
int a[maxn], b[1200][1200], vis[maxn];
vector<int>v[maxn];
int main()
{
    int n, m, k, x, y;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0; i<n; ++i) scanf("%d",&a[i]);
    int len = 92;
    int num = (n+len-1)/len;
    for(int i=0; i<n; i+=len)
    {
        int imax = 0;
        memset(vis, -1, sizeof(vis));
        for(int j=i; j<n; ++j)
        {
            if(j%len==0) b[i/len][j/len] = imax;
            if(vis[a[j]] == -1) vis[a[j]] = j;
            else imax = max(imax, j-vis[a[j]]);
        }
        b[i/len][num] = imax;
    }
    for(int i=0; i<n; ++i)
        v[a[i]].push_back(i);
    while(k--)
    {
        scanf("%d%d",&x,&y);
        --x;--y;
        int l=(x+len)/len*len;
        int r = y/len*len;
        int ans = b[l/len][r/len];
        for(int i=x; i<l; ++i)
        {
            int pos = lower_bound(v[a[i]].begin(), v[a[i]].end(), y)-v[a[i]].begin()-1;
            if(pos < 0) continue;
            ans = max(ans, v[a[i]][pos]-i);
        }
        for(int i=y; i>=r; --i)
        {
            int pos = lower_bound(v[a[i]].begin(), v[a[i]].end(), x)-v[a[i]].begin();
            if(pos == v[a[i]].size()) continue;
            ans = max(ans, i-v[a[i]][pos]);
        }
        printf("%d\n",ans);
    }
    return 0;
}


CodeChef:多种语言的CodeChef问题解决方案 厨师长 使用Python,Java,C,C ++,Ruby,JS等的完整CodeChef解决方案。 该存储库旨在将所有人带入竞争性编码世界。它包含多种语言在CodeChef中所有编码问题的解决方案。 :curly_loop: 开始解决 :triangular_flag: 显示一些 :green_heart:和 :star:回购支持 主要贡献者 :sparkles: 感谢这些出色的人: 立即下载

相关推荐

别再到处找离线包了!Visual Studio 2022 企业版/社区版离线安装保姆级教程(含工作负载选择指南)

本文提供Visual Studio 2022企业版/社区版离线安装的详细教程,包括工作负载选择指南和批量安装优化策略。针对网络隔离或带宽受限环境,介绍如何制作离线安装包、选择合适的工作负载组合,并分享高级技巧如增量更新和自动化部署,帮助团队高效统一开发环境配置。

weixin_28745821的博客 754

CodeChef:Chef and Problems(分块)

CodeChef:Chef and Problems 题目大意 有一个长度为n的序列$a_1,a_2,……,a_n$,每次给出一个区间[l,r],求在区间内两个相等的数的最远距离($max(j-i,满足a_i==a_j且l\le i,j \le r)$) 思路: 分块将序列分成sqrt(n)块预处理出每个数在每个块之后出现的最早位置和在每个块之前出现的最晚位置O(msqrt(n)) 然后就...

weixin_30896511的博客 128

OneNET物联网平台实战:从零搭建到双向数据通信测试

本文提供了一份详细的OneNET物联网平台实战指南,从零开始讲解平台搭建与设备创建,并重点演示了如何通过MQTT协议实现设备数据上报与云端命令下发的双向数据通信测试。通过模拟设备连接与数据交互,帮助开发者快速理解物联网核心通信流程。

weixin_29306571的博客 359

CodeChef:Chef Anup(数学)

Chef Anup Problem Code: CHEFANUP Submit Tweet   All submissions for this problem are available. Read problems statements in Mandarin Chinese and Russian.

junior19的博客 712

jlink v9刷固件修改序列号来修复弹窗

jlink v9弹窗警告修复j

weixin_40500627的博客 9390

嵌入式开发环境Vscode开发STM32单片机程序

STM32单片机非常强大,大多数教程都是使用 keil 编译器,keil 是收费的而 gcc 是开源免费的。这里介绍一些使用 gcc +开发单片机程序的经验。(这里不解释 gcc 是什么)。

嵌入式技术开发 1849

Codechef [June Long Two 2022] 题解

Codechef [June Long Two 2022] 题解报告

Laishao_yuan的专栏 776

19、情境理论中的信息结构解析

本文系统解析了情境理论中的信息结构,从信息元(infon)的基本概念出发,深入探讨了关系结构与简单信息结构的构建方法,分析了标准信息结构所满足的一系列公理(如A1-A14),并阐述了角色、同一性、高阶信息、循环现象、参数适当性及信息元部分性等核心性质。通过具体示例和流程图展示了信息结构的构建过程,并对比了不同公理下的结构差异,最后讨论了该理论在与其他领域融合中的发展趋势及面临的挑战。

book8的博客 86

CodeChef div4 (H) The XOR-OR Dilemma

构造的思路就是把所有的数全部或起来,然后和最终的数做异或得到tmp。然后用二进制拼接出tmp,用或操作,然后把剩下的没用到的数或起来最终两数异或便是答案。期间,特判不存在的解。然后将x,y移除并将新值插入。给定x,求使得最终剩下的值为x的方案。...

m0_51780913的博客 214

Lammps模拟剪切作用下的位错滑移

Lammps模拟位错与障碍物作用。在lammps中实现周期性位错阵列模型模拟剪切作用下1/2刃位错与障碍物的交互作用

Rockyoung 3469

UNet 改进(27):结合SCSE注意力机制的UNet网络实现

本文介绍了一种结合SCSE注意力机制的改进UNet网络架构。该网络在保持UNet经典编码器-解码器结构基础上,通过在每层添加SCSEBlock模块,融合了通道注意力和空间注意力机制。通道注意力通过全局池化和1×1卷积生成通道权重,空间注意力则产生空间权重图,二者相加增强关键特征响应。该设计具有以下优势:1)保持原始UNet的多尺度特征提取能力;2)通过注意力机制提升对重要区域的关注度;3)计算效率高,仅增加少量参数。测试显示网络能保持输入输出分辨率不变,适用于医学图像分割等像素级预测任务。完整代码实现了5层

2401_82355416的博客 418

CodeChef Chef and Problems(分块)

链接 题意: &amp;amp;amp;nbsp;&amp;amp;amp;nbsp;&amp;amp;amp;nbsp;&amp;amp;amp;nbsp;&amp;amp;amp;nbsp;&amp;amp;amp;nbsp;&amp;amp;amp;nbsp;&amp;amp;amp;nbsp;\ \ \ \ nnn个数kkk个查询,每次查询在[l,r][l,r][l,r]中满足ai=aj时j−iai=aj时j−ia_i=a_j时j-i的最大值。 思路: &amp;amp;amp;nbsp;

hnust_Derker的博客 463

视频流推理的工程架构:YOLO模型如何做到单卡处理16路1080P实时流?

<think>我们只需要根据内容生成摘要,不超过150字。内容是关于YOLO模型单卡处理16路1080P实时流的技术分析,包括方案对比、架构设计等。摘要要简洁,突出关键点。</think>本文深度解析了YOLO模型单卡处理16路1080P实时流的技术方案,对比EagleEye、TensorRT+DeepStream、华为CANN及RK3588四条路线,重点介绍流水线架构、并发模型及显存优化,指出消费级显卡(如RTX 4090)即可实现高并发智能视觉分析。

2601_95807009的博客 169

CodeChef QCHEF Chef and Problems (分块)

题意: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; 一个数列,m次查询,每次查询[L,R]中,一样的数字的最远距离 思路: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; 分块,对于每一个块,维护fi[i][j] (第j个块,i出现最前的位置),la[i][j] (第j个块,i的出现最后的位置),再维护ans[i][j](第i个块到第j个...

Roll_Keyboard的博客 355

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Chef and Problems(from Code-Chef FNCS) ( 回 滚 )

题目: 题意:给定序列,求[l,r]区间内数字相同的数的最远距离。 链接:https://www.codechef.com/problems/QCHEF #include&lt;bits/stdc++.h&gt; #define LL long long #define ULL unsigned long long #define rep(i,...

weixin_30246221的博客 184

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

codechef Chef and Problems

终于补出这道:一直耽搁到现在 找到一个代码可读性很好的分块temp; 题意:给一个长度为n 的数组 A,Q次询问,区间相等数的最大范围是多少? 数据范围都是10e5; 当然知道分块了; 传统分块看各种累; 找了一份很好的tmp&lt;新技能get; 1 #include&lt;bits/stdc++.h&gt; 2 3 using namespace std; 4...

weixin_30778805的博客 103

CodeChef Chef and Churu Problem

Chef and Churu Problem Code: FNCS Add problem to Todo list Tweet 链接All submissions for this problem are available. Read problems statements in Mandarin Chinese and Russian. Chef has recently learn

苦鬼·。 634

CodeChef May Challenge 2014

Chef-jumping

qqspeed 1037

Codechef March Challenge 2014——The Street

The StreetProblem Code:STREETTA https://www.codechef.com/problems/STREETTA Submit Tweet All submissions for this problem are available. Read problems statements inMandarin Chi...

weixin_30379531的博客 124

Codechef September Challenge 2015

1.Count Steps in Matrix 这里 很水,直11-N∗NN*N 扫过去就行了 2.Chain of Doughnuts 这里 比较水,有坑点,就是需要注意到链的任意位置都可以被切断,包含多个圈的链也可以被切断。 3.Lighthouses 这里 很水,简单分析容易知道放置灯塔的个数只可能为1或者2,首先找是否存在一个点使得覆盖所有点,就是找左上,右上,左下,右下这四个方

Step by step 719

Codechef May Challenge 2015

随便瞎写,其实没做出多少题: Chef and Cake 题目大概是用输入的数生成 一个数组并且生成出q个【X,Y】的询问, 数组长度N&lt;=1000000,q&lt;=10^7; 开始用线段树,RMQ,分块能切过去,但是线段树RE,RMQ re ,分块最多40分。 但是一直忽视了区间询问的[X Y] K&lt;=Y-X+1&lt;=2K; K是给定的数。 然后是利用一些思想...

weixin_30826761的博客 132

五大编程竞赛平台终极对比

主流编程竞赛平台比较:LeetCode以面试准备为主,提供算法题库和模拟面试;Codeforces、AtCoder、CodeChef和TopCoder专注编程竞赛,高频赛事和梯度难度适合竞技训练;HackerRank则侧重求职技能测评,兼具企业招聘功能。各平台均支持C++等语言,但定位差异明显:LeetCode/HackerRank适合求职者,其他平台更利于竞赛能力提升。

2401_86601498的博客 1379

推荐10 款 C++ 在线编译器

网络安全行业产业以来,随即新增加了几十个网络安全行业岗位︰网络安全专家、网络安全分析师、安全咨询师、网络安全工程师、安全架构师、安全运维工程师、渗透工程师、信息安全管理员、数据安全工程师、网络安全运营工程师、网络安全应急响应工程师、数据鉴定师、网络安全产品经理、网络安全服务工程师、网络安全培训师、网络安全审计员、威胁情报分析工程师、灾难恢复专业人员、实战攻防专业人员…它提供的编译器支持 C++14,并且还具有代码高亮、新建文件/项目、下载文件/项目、自定义设置(例如:主题色)、错误提示等功能。

leah126的博客 5万+

CodeChef Starters 26 Division 3 (Rated)----STNGAME(中文题解)

描述: Contest Page | CodeChef 上面是原题目的地址,简要概括一下意思:Alice和Bob在玩一个填字符串的游戏,一开始Alice有n个字符,Bob也有n个字符。他们需要用这些字符填成一个长度为2n的字符串。Alice先手,并且她想要让字符串尽可能按字典序上升,Bob后手,他想让字符串尽可能地按字典序下降。 分析: 情景分析① Alice想要让这个字符串是按字典序升序排列的,所以她的游戏策略是:要让现在字符串当中最小下标的空位,被现在能够用于填充的(也就是Al...

Esweny的博客 405

推荐 10 款 C++ 在线编译器

推荐 10 款 C++ 在线编译器 2 Coding Ground Url:https://www.tutorialspoint.com/compile_cpp_online.php Coding Ground 是 Tutorialspoint(很强大的免费教程网站)的一个编码平台。它提供的编译器支持 C++14,并且还具有代码高亮、新建文件/项目、下载文件/项目、自定义设置(例如:主题色)、错...

被一个OIER遗弃的一堆烂代码 2万+
上一篇: CodeChef:Sereja and Tree 2(树形dp)
下一篇: SPOJ:Zero Query(分块 & 二分)
junior19
博客等级 码龄10年 118粉丝 615原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值