codechef Chef and Problems
终于补出这道:一直耽搁到现在
找到一个代码可读性很好的分块temp;
题意:给一个长度为n 的数组 A,Q次询问,区间相等数的最大范围是多少?
数据范围都是10e5;
当然知道分块了;
传统分块看各种累;
找了一份很好的tmp<新技能get;
#include<bits/stdc++.h> using namespace std;
const int N =;
const int S =; int a[N],res[S][N],occ[N];
int ans[N];
//一种很神奇的分块写法
//想办法 在其他题目扩展 struct query
{
int L,R,id;
bool operator <(const query& a)const
{
return R<a.R;
}
}q[N]; int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
int s=sqrt(n);
for (int i=;i<n;i++) scanf("%d",&a[i]); for (int i=;i*s<n;i++)//预处理0-->sqrt(n)个块,每块的宽度这里并不一定相同
{ //这里是从i*s-->n 都计算出区间的最大值
for (int j=;j<=m;j++) occ[j]=-;
int now=;
for (int j=i*s;j<n;j++)
{
if (occ[a[j]]==-) occ[a[j]]=j;
else now=max(now,j-occ[a[j]]);
res[i][j]=now;
}
} for (int i=;i<k;i++)
scanf("%d%d",&q[i].L,&q[i].R),q[i].id=i,q[i].L--,q[i].R--;
sort(q,q+k);//按询问R排序 for (int i=;i<=m;i++)
occ[i]=-; int r=; for (int i=;i<k;i++)
{
while (r<q[i].R)//q[i].L<q[i].R;
{
++r;
occ[a[r]]=r;//预处理出前几个
} int tmp=res[q[i].L/s+][q[i].R];//计算已经可以分块的数据
for (int j=q[i].L;j<(q[i].L/s+)*s&&j<=q[i].R;j++)
tmp=max(tmp,occ[a[j]]-j);//利用询问的单调询问开始不足一块的数据
ans[q[i].id]=tmp;
} for (int i=;i<k;i++) printf("%d\n",ans[i]);
return ;
}
codechef Chef and Problems的更多相关文章
- CodeChef:Chef and Problems(分块)
CodeChef:Chef and Problems 题目大意 有一个长度为n的序列$a_1,a_2,……,a_n$,每次给出一个区间[l,r],求在区间内两个相等的数的最远距离($max(j-i,满 ...
- Chef and Problems(from Code-Chef FNCS) ( 回 滚 )
题目: 题意:给定序列,求[l,r]区间内数字相同的数的最远距离. 链接:https://www.codechef.com/problems/QCHEF #include<bits/stdc++ ...
- CODECHEF Chef and Churus 解题报告
[CODECHEF]Chef and Churus Description 有一个长度为\(n\)的数组\(A\),有\(n\)个函数,第\(i\)个函数的值为\(\sum_{j=l_i}^{r_i} ...
- codechef Chef and The Right Triangles 题解
Chef and The Right Triangles The Chef is given a list of N triangles. Each triangle is identfied by ...
- Codechef Chef and Triangles(离散化+区间并集)
题目链接 Chef and Triangles 先排序,然后得到$m - 1$个区间: $(a[2] - a[1], a[2] + a[1])$ $(a[3] - a[2], a[3] + a[2]) ...
- CodeChef Chef and Churu [分块]
题意: 单点修改$a$ 询问$a$的区间和$f$的区间和 原来普通计算机是这道题改编的吧... 对$f$分块,预处理$c[i][j]$为块i中$a_j$出现几次,$O(NH(N))$,只要每个块差分加 ...
- codechef Chef And Easy Xor Queries
做法:我们考虑前缀异或和,修改操作就变成了区间[i,n]都异或x 查询操作就变成了:区间[1,x]中有几个k 显然的分块,每个块打一个tag标记表示这个块中所有的元素都异或了tag[x] 然后处理出这 ...
- 2019.02.14 codechef Chef at the Food Fair(线段树+泰勒展开)
传送门 题意:现在有nnn个位置,每个位置上有一个值aia_iai. 要求支持如下两种操作: 区间乘vvv 求区间的(1−ai)(1-a_i)(1−ai)之积 思路: 考虑转换式子: Ans=∏i ...
- 【A* 网络流】codechef Chef and Cut
高嘉煊讲的杂题:A*和网络流的练手题 题目大意 https://s3.amazonaws.com/codechef_shared/download/translated/SEPT16/mandarin ...
随机推荐
- java上传、下载、删除ftp文件
一共三个类,一个工具类Ftputil.,一个实体类Kmconfig.一个测试类Test 下载地址:http://download.csdn.net/detail/myfmyfmyfmyf/669710 ...
- js中的跨域方法总结
什么是跨域? 浏览器的安全策略,只要协议,域名,端口有任何一个不同,就被当做不同的域. 下面对http://www.qichedaquan.com的同源检测 http://www.qichedaqua ...
- React初识整理(二)--生命周期的方法
React生命周期主要有7中: 1. componentWillMount() :组件将要挂载时触发 ,只调用1次 2. componentDidMount() :组件挂载完成时触发,只调用1次 3. ...
- mysql:explain分析sql
对于执行较慢的sql,可以使用explain命令查看这些sql的执行计划.查看该SQL语句有没有使用上了索引,有没有做全表扫描,这都可以通过explain命令来查看 mysql> explain ...
- RN笔记
https://facebook.github.io/react-native/docs/using-a-listview.html react native类似于react,不过它使用的是原生组件, ...
- perl学习之:localtime
Perl中localtime()函数以及sprintf (2011-4-25 19:39)localtime函数 localtime函数,根据它所在的上下文,可以用两种完全不同的方法来运行.在标量上下 ...
- (转))iOS App上架AppStore 会遇到的坑
iOS App上架AppStore 会遇到的坑 前言:非原创 文章摘自:http://zhuanlan.zhihu.com/100000PM/20010725 相信大家一定非常「深恶痛疾」AppS ...
- 纯干货!live2d动画制作简述以及踩坑
本文来自网易云社区,转载务必请注明出处. 1. 概述 live2d是由日本Cybernoids公司开发,通过扭曲像素位置营造伪3d空间感的二维动画软件.官网下载安装包直接安装可以得到两种软件,分别是C ...
- HDU 3341 Lost's revenge
Lost's revenge Time Limit: 5000ms Memory Limit: 65535KB This problem will be judged on HDU. Original ...
- Educational Codeforces Round 26
Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...