CodeChef - QCHEF 分块
题目链接: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 分块的更多相关文章
- bzoj 3509: [CodeChef] COUNTARI] [分块 生成函数]
3509: [CodeChef] COUNTARI 题意:统计满足\(i<j<k, 2*a[j] = a[i] + a[k]\)的个数 \(2*a[j]\)不太好处理,暴力fft不如直接暴 ...
- BZOJ 3509 [CodeChef] COUNTARI ——分块 FFT
分块大法好. 块内暴力,块外FFT. 弃疗了,抄SX队长$silvernebula$的代码 #include <map> #include <cmath> #include & ...
- codechef QCHEF(不删除莫队)
题意 题目链接 给出长度为\(n\)的序列,每次询问区间\([l, r]\),要求最大化 \(max |x − y| : L_i ≤ x, y ≤ R_i and A_x = A_y\) Sol 标算 ...
- [codechef FNCS]分块处理+树状数组
题目链接:https://vjudge.net/problem/CodeChef-FNCS 在一个地方卡了一晚上,就是我本来以为用根号n分组,就会分成根号n个.事实上并不是....因为用的是根号n下取 ...
- 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu
https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...
- Codechef SEAARC Sereja and Arcs (分块、组合计数)
我现在真的什么都不会了呢...... 题目链接: https://www.codechef.com/problems/SEAARC 好吧,这题其实考察的是枚举的功力-- 题目要求的是\(ABAB\)的 ...
- Codechef TRIPS Children Trips (分块、倍增)
题目链接: https://www.codechef.com/problems/TRIPS 感觉CC有点毒瘤啊.. 题解: 首先有一个性质可能是因为太傻所以网上没人解释,然而我看了半天: 就是正序和倒 ...
- [BZOJ 3509] [CodeChef] COUNTARI (FFT+分块)
[BZOJ 3509] [CodeChef] COUNTARI (FFT+分块) 题面 给出一个长度为n的数组,问有多少三元组\((i,j,k)\)满足\(i<j<k,a_j-a_i=a_ ...
- CodeChef FNCS (分块+树状数组)
题目:https://www.codechef.com/problems/FNCS 题解: 我们知道要求区间和的时候,我们用前缀和去优化.这里也是一样,我们要求第 l 个函数到第 r 个函数 [l, ...
随机推荐
- 为何JAVA虚函数(虚方法)会造成父类可以"访问"子类的假象?
首先,来看一个简单的JAVA类,Base. 1 public class Base { 2 String str = "Base string"; 3 protected vo ...
- 安卓贴图源码--->单点触控.多点触控.类似in/百度魔图
效果如图: 类似in,百度魔图,的贴图功能 核心的地方:单/多点 旋转缩放后记录各个顶点小图标位置 引用这里 http://blog.csdn.net/xiaanming/article/detai ...
- Recall, Precision and F-score
F1 score (also F-score or F-measure) ,调和平均数稍微有点不好理解,最关键的是,不知道分子的情况下,采用调和平均数.
- 收藏一些好用的fifo
1.Nordic库中的 E:\nRF52_SDK_0.9.2_dbc28c9\components\libraries\fifo app_fifo.c /* Copyright (c) 2013 No ...
- MEF 根据配置注入Service
有这样的场景 : 现在一个接口有很多种实现类,需要根据配置,来确定确定调用哪个具体的实现类.这样使得软件扩展性大大提高 在MEF可以通过ExportMetadata 来实现这样的效果. 1.现在我们建 ...
- TCP/IP 和 Socket 的关系
要写网络程序就必须用Socket,这是程序员都知道的.而且,面试的时候,我们也会问对方会不会Socket编程?一般来说,很多人都会说,Socket编程基本就是listen,accept以及send,w ...
- 如何给外部引用的js文件传递参数
1.定义全局变量 <script language="javascript"> var g = "I'm here"; </script> ...
- js操作数组的一些小技巧
1.从数组中随机获取成员 var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119]; var rando ...
- [codevs1105][COJ0183][NOIP2005]过河
[codevs1105][COJ0183][NOIP2005]过河 试题描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青 ...
- Java批量文件打包下载
经常遇到选择多个文件进行批量下载的情况,可以先将选择的所有的文件生成一个zip文件,然后再下载,该zip文件,即可实现批量下载,但是在打包过程中,常常也会出现下载过来的zip文件中里面有乱码的文件名, ...