(分块)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 ...
随机推荐
- 2019牛客多校第二场 A Eddy Walker(概率推公式)
2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...
- oracle解除被锁定的表的状态
select b.owner,b.object_name,a.session_id,a.locked_mode,c.serial#,c.sid||','||c.serial# from v$loc ...
- Java实现简单的学生成绩管理系统
ScoreInformation.java import java.util.Scanner; class ScoreInformation { private String stunumber ...
- vue学习笔记(三)class和style绑定
前言 通过上一章的学习vue学习笔记(二)vue的生命周期和钩子函数,我们已经更近一步的知道了关于vue的一些知识,本篇博客将进一步探讨vue其它方面的内容,vue中关于class和style绑定,关 ...
- LINQ 实现多字段关联查询 C#
var query = from main in _userDeviceChannelRole.Table join deviceChannelInfo in _deviceChannelReposi ...
- 20191017-5 alpha week 2/2 Scrum立会报告+燃尽图 04
此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9801 小组名称:“组长”组 组长:杨天宇 组员:魏新,罗杨美慧,王歆瑶,徐 ...
- socket粘包问题及解决方案
一.粘包问题 问题1: 无法确认对方发送过来数据的大小. 'client.py' import socket client = socket.socket() client.connect( ('12 ...
- 洛谷$P2050\ [NOI2012]$美食节 网络流
正解:网络流 解题报告: 传送门$QwQ$ 昂开始看到$jio$得,哇长得好像上一题嗷$QwQ$ 然后仔细康康数据范围,发现,哇好像要几万个点,,,显然就$GG$了 但感$jio$思路方向好对的亚子? ...
- dos2unix命令 – 将DOS格式的文本文件转换成UNIX格式
今天做题的时候,出现了个很冷门的: 查找子目录src下所有后缀为.txt的文件执行dos2unix命令,把文件从Dos格式转换为Linux格式,正确的命令是:find src "*.txt& ...
- 1073 多选题常见计分法 (20分)C语言
批改多选题是比较麻烦的事情,有很多不同的计分方法.有一种最常见的计分方法是:如果考生选择了部分正确选项,并且没有选择任何错误选项,则得到 50% 分数:如果考生选择了任何一个错误的选项,则不能得分.本 ...