题目链接:http://vjudge.net/problem/174774/origin

题意:给定一个长度为n的序列a[],序列的值不大于m,现在有k个询问,每个询问给定(l,r).让你求出max{|x − y| : Li ≤ x, y ≤ Ri and Ax = Ay}。即区间[L,R]中值相同时,位置差的最大值

思路:分块,因为不带修改,所以我们就可以做预处理。求出last,first,Ans。 last[i][j]:值i在第j块最后出现的位置。first[i][j]:值i在第j块最早出现的位置。Ans[i][j]:第i块到第j块的值相同的最大位置差。

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<vector>
#include<iostream>
#include<map>
using namespace std;
typedef long long int LL;
const int MAXN = + ;
int belong[MAXN], block, num, L[MAXN], R[MAXN];
int n, m, q;
int a[MAXN], last[MAXN][], first[MAXN][], Ans[][];
int tim[MAXN], times, Pos[MAXN];
int cal(int st, int ed){
int ans = ;
for (int i = L[st]; i <= R[st]; i++){
ans = max(ans, last[a[i]][ed] - i);
}
return ans;
}
void build(){
block = (int)sqrt(n + 0.5);
num = n / block; if (n%block){ num++; }
for (int i = ; i <= num; i++){
L[i] = (i - )*block + ; R[i] = i*block;
}
R[num] = n;
for (int i = ; i <= n; i++){
belong[i] = ((i - ) / block) + ;
}
memset(last, , sizeof(last)); memset(first, , sizeof(first));
memset(Ans, , sizeof(Ans)); times = ;
for (int i = ; i <= n; i++){
last[a[i]][belong[i]] = i;
}
for (int i = n; i>; i--){
first[a[i]][belong[i]] = i;
}
for (int i = ; i <= m; i++){
for (int j = ; j <= num; j++){
if (!last[i][j]){ last[i][j] = last[i][j - ]; }
}
for (int j = num; j; j--){
if (!first[i][j]){ first[i][j] = first[i][j + ]; }
}
}
for (int i = num; i; i--){
for (int j = i; j <= num; j++){
Ans[i][j] = max(max(Ans[i + ][j], Ans[i][j - ]), cal(i, j));
}
}
}
int query(int st, int ed){
int ans = ; times++;
if (belong[st] == belong[ed]){
for (int i = st; i <= ed; i++){
if (tim[a[i]] != times){ Pos[a[i]] = i; tim[a[i]] = times; }
else{ ans = max(ans, i - Pos[a[i]]); }
}
return ans;
}
for (int i = st; i <= R[belong[st]]; i++){
if (tim[a[i]] != times){ Pos[a[i]] = i; tim[a[i]] = times; }
else{ ans = max(ans, i - Pos[a[i]]); }
ans = max(ans, last[a[i]][belong[ed] - ] - i);
}
ans = max(ans, Ans[belong[st] + ][belong[ed] - ]);
for (int i = L[belong[ed]]; i <= ed; i++){
if (tim[a[i]] != times){ Pos[a[i]] = i; tim[a[i]] = times; }
else{ ans = max(ans, i - Pos[a[i]]); }
ans = max(ans, i - first[a[i]][belong[st] + ]);
}
return ans;
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
while (~scanf("%d%d%d", &n, &m, &q)){
for (int i = ; i <= n; i++){ scanf("%d", &a[i]); }
build(); int st, ed;
for (int i = ; i <= q; i++){
scanf("%d%d", &st, &ed);
printf("%d\n", query(st, ed));
}
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}

CodeChef - QCHEF 分块的更多相关文章

  1. bzoj 3509: [CodeChef] COUNTARI] [分块 生成函数]

    3509: [CodeChef] COUNTARI 题意:统计满足\(i<j<k, 2*a[j] = a[i] + a[k]\)的个数 \(2*a[j]\)不太好处理,暴力fft不如直接暴 ...

  2. BZOJ 3509 [CodeChef] COUNTARI ——分块 FFT

    分块大法好. 块内暴力,块外FFT. 弃疗了,抄SX队长$silvernebula$的代码 #include <map> #include <cmath> #include & ...

  3. codechef QCHEF(不删除莫队)

    题意 题目链接 给出长度为\(n\)的序列,每次询问区间\([l, r]\),要求最大化 \(max |x − y| : L_i ≤ x, y ≤ R_i and A_x = A_y\) Sol 标算 ...

  4. [codechef FNCS]分块处理+树状数组

    题目链接:https://vjudge.net/problem/CodeChef-FNCS 在一个地方卡了一晚上,就是我本来以为用根号n分组,就会分成根号n个.事实上并不是....因为用的是根号n下取 ...

  5. 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu

    https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...

  6. Codechef SEAARC Sereja and Arcs (分块、组合计数)

    我现在真的什么都不会了呢...... 题目链接: https://www.codechef.com/problems/SEAARC 好吧,这题其实考察的是枚举的功力-- 题目要求的是\(ABAB\)的 ...

  7. Codechef TRIPS Children Trips (分块、倍增)

    题目链接: https://www.codechef.com/problems/TRIPS 感觉CC有点毒瘤啊.. 题解: 首先有一个性质可能是因为太傻所以网上没人解释,然而我看了半天: 就是正序和倒 ...

  8. [BZOJ 3509] [CodeChef] COUNTARI (FFT+分块)

    [BZOJ 3509] [CodeChef] COUNTARI (FFT+分块) 题面 给出一个长度为n的数组,问有多少三元组\((i,j,k)\)满足\(i<j<k,a_j-a_i=a_ ...

  9. CodeChef FNCS (分块+树状数组)

    题目:https://www.codechef.com/problems/FNCS 题解: 我们知道要求区间和的时候,我们用前缀和去优化.这里也是一样,我们要求第 l 个函数到第 r 个函数 [l, ...

随机推荐

  1. [Android]Volley源码分析(五)

    前面几篇通过源码分析了Volley是怎样进行请求调度及请求是如何被实际执行的,这篇最后来看下请求结果是如何交付给请求者的(一般是Android的UI主线程). 类图:

  2. backup mysql

    #!/bin/bashcd /home/Licw/backup_openDBNow=$(date +"%m-%d-%Y--%H:%M:%S")#echo $NowFile=$Now ...

  3. ionic 微信分享值各种坑

    去前段时间公司的app需要做微信分享,然后网上找的教程,在做的时候发现网上的教程各种坑,现在将做得过程分享出来 在做功能之前你需要做几步预备工作, 1.安装jdk,jre,并加入全局变量[这个网上还是 ...

  4. python学习笔记-(十四)进程&协程

    一. 进程 1. 多进程multiprocessing multiprocessing包是Python中的多进程管理包,是一个跨平台版本的多进程模块.与threading.Thread类似,它可以利用 ...

  5. Backbone 学习笔记

    Backbone 是一款基于模型-视图-控制器 MVC 模式的轻量级javascript 框架 ,可以用来帮助开发人员创建单页Web应用. 借助Backbone 我们可以使用REST的方式来最小化客户 ...

  6. ThinkPHP3.2.3整合smarty模板(二)

    前言:继ThinkPHP3.2.3整合smarty模板(一)之后,继续来探讨一下tp框架整合smarty模板,看到有人在群上问到怎么使用自定义的常量,今天就具体来谈谈: 一.开发一个项目,必不可少会用 ...

  7. 大熊君学习html5系列之------WebStorage(客户端轻量级存储方案)

    一,开篇分析 Hi,大家好!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例, 让大家一步一步的体会" ...

  8. tyvj1172 自然数拆分Lunatic版

    背景 话说小小鱼看了P1171(自然数拆分)之后感觉异常不爽,于是异常邪恶地将题目加强. 描述 输入自然数n,然后将其拆分成由若干数相加的形式,参与加法运算的数可以重复. 输入格式 输入只有一个整数n ...

  9. jquery 页面加载时获取图片高度

    $(function () { $(window).load(function(){ alert($('img').height()); }); });

  10. Form 详细属性--2016年12月4日

    属性       名称 说明   AcceptButton 获取或设置当用户按 Enter 键时所单击的窗体上的按钮.   AccessibilityObject 获取分配给该控件的 Accessib ...