(分块)GukiZ and GukiZiana CodeForces - 551E
题意:
给你一段序列,并且有两种操作
操作①:将序列中从l-r每个元素加上x
操作②:在序列中找到ai=aj=y,j-i的最大值,如果找不到则输出-1
思路:
直接分块暴力即可
对于区间加,普通标记加暴力即可
对于找最大值,直接在每个块中二分找y,找不到即为-1
#include<iostream>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
ll n,blo,tot;
ll a[maxn];//bl数组记录属于哪个块
int bel[maxn],m;
ll tag[maxn];//维护区间加标记
vector <ll> vc[maxn];
void build()
{
blo=sqrt(n);
tot=n/blo;
if(n%blo) tot++;
for(int i=1;i<=n;i++){
bel[i]=(i-1)/blo+1;
vc[bel[i]].push_back(a[i]);
}
for(int i=1;i<=tot;i++)
sort(vc[i].begin(),vc[i].end());
}
void reset(int x)
{
vc[x].clear();
for(int i=(x-1)*blo+1;i<=x*blo;i++)
vc[x].push_back(a[i]);
sort(vc[x].begin(),vc[x].end());
}
void add(int l,int r,int x)
{
int b1=bel[l],b2=bel[r];
if(b1==b2){
for(int i=l;i<=r;i++)
a[i]+=x;
reset(b1);
}
else{
for(int i=l;i<=b1*blo;i++)
a[i]+=x;
reset(b1);
for(int i=b1+1;i<b2;i++)
tag[i]+=x;
for(int i=(b2-1)*blo+1;i<=r;i++) a[i]+=x;
reset(b2);
}
}
int query(int y)
{
int l=0,r=0;
for(int i=1;i<=tot;i++){
int pos=lower_bound(vc[i].begin(),vc[i].end(),y-tag[i])-vc[i].begin();
if(y-tag[i] == vc[i][pos]){
for(int j=(i-1)*blo+1;j<=i*blo;j++)
if(a[j]+tag[i]==y){
l=j;
break;
}
break;
}
}
if(l==0) return -1;
for(int i=tot;i>=1;i--){
int pos=lower_bound(vc[i].begin(),vc[i].end(),y-tag[i])-vc[i].begin();
if(y-tag[i] == vc[i][pos]){
for(int j=i*blo;j>(i-1)*blo;j--)
if(a[j]+tag[i]==y){
r=j;
break;
}
break;
}
}
return r-l;
} int main()
{
int q,l,r,y,op;
scanf("%lld%d",&n,&q);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build();
for(int i=1;i<=q;i++){
scanf("%d",&op);
if(op==1){
scanf("%d%d%d",&l,&r,&y);
add(l,r,y);
}
else{
scanf("%d",&y);
printf("%d\n",query(y));
}
}
return 0;
}
(分块)GukiZ and GukiZiana CodeForces - 551E的更多相关文章
- Codeforces 551E GukiZ and GukiZiana(分块思想)
题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...
- Codeforces 551E - GukiZ and GukiZiana(分块)
Problem E. GukiZ and GukiZiana Solution: 先分成N=sqrt(n)块,然后对这N块进行排序. 利用二分查找确定最前面和最后面的位置. #include < ...
- CodeForces 551E GukiZ and GukiZiana
GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...
- Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana 分块
E. GukiZ and GukiZiana Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...
- Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana(分块)
E. GukiZ and GukiZiana time limit per test 10 seconds memory limit per test 256 megabytes input stan ...
- CF 551E. GukiZ and GukiZiana [分块 二分]
GukiZ and GukiZiana 题意: 区间加 给出$y$查询$a_i=a_j=y$的$j-i$最大值 一开始以为和论文CC题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...
- Codeforces 551 E - GukiZ and GukiZiana
E - GukiZ and GukiZiana 思路:分块, 块内二分 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC ...
- [codeforces551E]GukiZ and GukiZiana
[codeforces551E]GukiZ and GukiZiana 试题描述 Professor GukiZ was playing with arrays again and accidenta ...
- Codeforces 307 div2 E.GukiZ and GukiZiana 分块
time limit per test 10 seconds memory limit per test 256 megabytes input standard input output stand ...
随机推荐
- 【codeforces 764A】Taymyr is calling you
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 备战省赛组队训练赛第七场(UPC)
传送门 日文题解:戳这里
- java 环境 笔记
1. 下载idea https://blog.csdn.net/yl1712725180/article/details/80309862 破解方法: 针对20 ...
- string的常见操作
访问 遍历 不需修改:for(auto c : s) 需要修改:for(auto &c : s) for(decltype(s.size()) i = 0; i < s.size( ...
- CachedRowSet 接口
Sun Microsystems 提供的 CachedRowSet 接口的参考实现是一个标准实现.开发人员可以按原样使用此实现.可以扩展它,也可以选择自己编写此接口的实现. CachedRowSet ...
- Keras mlp 手写数字识别示例
#基于mnist数据集的手写数字识别 #构造了三层全连接层组成的多层感知机,最后一层为输出层 #基于Keras 2.1.1 Tensorflow 1.4.0 代码: import keras from ...
- 大学最新毕业论文参考文献,包含java,jsp,mysql,Android,sql,PHP
每当毕业论文写到最后需要参考文献时,往往是很令人头疼的,因为有的老师对参考文献的要求是很多的,比如需要国内的和国外的,时间必须是近三年的,满足XXX要求的文献至少需要三篇以上等等.今天我就来给大家整理 ...
- 【题解】 P2763 试题库问题(网络流)
P2763 试题库问题 考虑一个试题要被加入进答案的集合有什么条件? 是某种类型 只算作一次 就这两种且的限制,所以我们用串联的方式连接"类型点"和"作用点". ...
- 漏洞挖掘 | 远程WWW服务支持TRACE请求
允许TRACE方法 漏洞描述 目标WEB服务器启用了TRACE方法.TRACE方法是HTTP(超文本传输)协议定义的一种协议调试方法,该方法使得服务器原样返回任何客户端请求的内容(可能会附加路由中间的 ...
- .Net Core 认证系统之基于Identity Server4 Token的JwtToken认证源码解析
介绍JwtToken认证之前,必须要掌握.Net Core认证系统的核心原理,如果你还不了解,请参考.Net Core 认证组件源码解析,且必须对jwt有基本的了解,如果不知道,请百度.最重要的是你还 ...