题目链接: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. codevs 2988 保留小数 2

    2988 保留小数 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver       题目描述 Description 这个难度是吸引你点进来的.(其实难度挺 ...

  2. PDFobject插件使用,PDF在线查看插件

    1.引入插件JS <script type="text/javascript" src="PDFobject.js"></script> ...

  3. 配置maven

    http://www.cnblogs.com/liunanjava/archive/2015/11/05/4936037.html

  4. 优化Table View

    优化Table View Table view需要有很好的滚动性能,不然用户会在滚动过程中发现动画的瑕疵. 为了保证table view平滑滚动,确保你采取了以下的措施: 正确使用`reuseIden ...

  5. EmguCV控件Emgu.CV.UI.ImageBox及C# picturebox显示图片连续刷新出现闪烁问题

    在上一篇里,EmguCV(OpenCV)实现高效显示汉字及叠加  实现了视频叠加及显示,但存在问题,就是 Emgu.CV.UI.ImageBox及C# picturebox显示图片时都会出现闪烁,尤其 ...

  6. mysql优化记录

    老板反应项目的反应越来越慢,叫优化一下,顺便学习总结一下mysql优化. 不同引擎的优化,myisam读的效果好,写的效率差,使用场景 非事务型应用只读类应用空间类应用 Innodb的特性,innod ...

  7. 生成彩条的MATLAB代码

    clc;close all;clear %read image % RGBimga = imread('bmpinput_1080p.bmp'); RGBimga = imread('bmpinput ...

  8. [译] 你该知道的javascript作用域 (javascript scope)(转)

    javascript有一些对于初学者甚至是有经验的开发者都难以理解的概念. 这个部分是针对那些听到 : 作用域, 闭包, this, 命名空间, 函数作用域, 函数作用域, 全局作用域, 变量作用域( ...

  9. NoClassDefFoundError vs ClassNotFoundException

    我们先来认识一下Error 和Exception, 两个都是Throwable类的直接子类.  Javadoc 很好的说明了Error类: An Error is a subclass of Thro ...

  10. js求时间差

    var date1=new Date();  //开始时间 alert("aa"); var date2=new Date();    //结束时间 var date3=date2 ...