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 随机数 优惠码 生成 随机字串
package test; import java.util.HashSet; import java.util.Random; import java.util.Set; public class ...
- Publishing failed with multiple errors 异常
Publishing failed with multiple errors 在使用eclipse发布项目时不能自动生成class文件,且无法启动调试的Tomcat服务.启动过程提示 以上 异常 解决 ...
- How to setup vsftpd FTP file Server on Redhat 7 Linux
Forward from: https://linuxconfig.org/how-to-setup-vsftpd-ftp-file-server-on-redhat-7-linux How to s ...
- XML编码utf-8有中文无法解析或乱码 C#
XML的encoding="UTF-8" ,含有中文的话(部分)会出现乱码. 网上还是很多这类问题跟解决办法的. 表现为用ie或者infopath之类的xml软件打不开这个xml, ...
- Gravatar注册
今天在学习falsk时需要用到头像扩展,需要使用gravatar到网上查了查好像没有被墙,就试着用qq邮箱注册了一个账号,结果等了好长时间也没有等到激活邮箱,就百度了一下发现了一个技巧,原来这封邮件被 ...
- JVM大端判断
JVM采用大端方式存多字节的数据,判断方法如下: public static void bytesToInt() throws IOException { /** * 将字节数组(byte[])转为整 ...
- Spring BeanNameAutoProxyCreator 与 ProxyFactoryBean区别
一般我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置文件的 ...
- C语言基础(3)-二进制、八进制、十六进制
1.二进制 一个位只能表示0或者1两种状态,简称bit(比特) 一个字节为8个二进制数,称为8位,简称BYTE(字节) 一个字为2个字节,简称WORD. 两个字为双字,简称DWORD,占32个bit ...
- 大熊君JavaScript插件化开发------(第二季)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得昨天的那篇文章吗------这个系列的开篇(第一季).主要讲述了以“jQuery的方式如何开发插件”, 那么今天我们带着昨天的疑问来继续我们的插 ...
- eclipse android工程没有错却出现红叉
[转]eclipse android工程没有错却出现红叉 问题描述: 这是一个很变态的问题,花了我N多时间才解决掉,而且弄得心情非常郁闷,这明显是ADT的bug嘛,为什么最新的版本还没有解决? 将Li ...