CodeForces 551E 分块
题目链接:http://codeforces.com/problemset/problem/551/E
题意:给定一个长度为N的序列。 有2个操作 1 l r v:序列第l项到第r项加v(区间加), 2 v:求整个序列中值为v的数的位置的差值最大是多少。不存在输出-1.
思路:分块。 每块维护该块序列排序后的序列。 对于区间修改,我们定义一个lazy标记。对于整块的修改我们只修改lazy, 其他情况暴力修改。然后情况该块后重新加入修改后的块然后排序。 对于查询操作。查询每块时v应该减去该块的lazy值。 然后2分查即可。
#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, q;
LL val[MAXN], lazy[MAXN];
struct Node{
LL v;
int id;
Node(LL _v, int _id) :v(_v), id(_id){};
bool operator < (const Node &a)const{
return v==a.v?id<a.id:v<a.v;
}
};
vector<Node>Bval[MAXN];
void build(){
block = (int)sqrt(n+0.5);
num = n / block; if (n%block){ num++; }
for (int i = ; i <= num; i++){
lazy[i] = ; Bval[i].clear();
L[i] = (i - )*block + ; R[i] = i*block;
}
R[num] = n;
for (int i = ; i <= n; i++){
belong[i] = ((i - ) / block) + ;
}
for (int i = ; i <= num; i++){
for (int k = L[i]; k <= R[i]; k++){
Bval[i].push_back(Node(val[k],k));
}
sort(Bval[i].begin(), Bval[i].end());
}
}
void modify(int st,int ed, LL v){
if (belong[st] == belong[ed]){
for (int i = st; i <= ed; i++){
val[i] += v;
}
Bval[belong[st]].clear();
for (int i = L[belong[st]]; i <= R[belong[st]]; i++){
Bval[belong[st]].push_back(Node(val[i], i));
}
sort(Bval[belong[st]].begin(), Bval[belong[st]].end());
return;
}
for (int i = st; i <= R[belong[st]]; i++){
val[i] += v;
}
Bval[belong[st]].clear();
for (int i = L[belong[st]]; i <= R[belong[st]]; i++){
Bval[belong[st]].push_back(Node(val[i], i));
}
sort(Bval[belong[st]].begin(), Bval[belong[st]].end());
for (int i = belong[st] + ; i < belong[ed]; i++){
lazy[i] += v;
}
for (int i = L[belong[ed]]; i <= ed; i++){
val[i] += v;
}
Bval[belong[ed]].clear();
for (int i = L[belong[ed]]; i <= R[belong[ed]]; i++){
Bval[belong[ed]].push_back(Node(val[i], i));
}
sort(Bval[belong[ed]].begin(), Bval[belong[ed]].end());
}
int query(LL v){
int L = -, R = -;
for (int i = ; i <= num; i++){
LL _v = v-lazy[i];
int pos = lower_bound(Bval[i].begin(), Bval[i].end(), Node(_v,)) - Bval[i].begin();
if (pos >= && pos < Bval[i].size() && Bval[i][pos].v == _v){
L = Bval[i][pos].id; break;
}
}
if (L == -){ return -; }
for (int i = num; i > ; i--){
LL _v = v - lazy[i];
int pos = (lower_bound(Bval[i].begin(), Bval[i].end(), Node( _v + ,)) - Bval[i].begin())-;
if (pos >= && pos < Bval[i].size() && Bval[i][pos].v == _v){
R = Bval[i][pos].id; break;
}
}
return R - L;
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
while (~scanf("%d%d", &n,&q)){
for (int i = ; i <= n; i++){
scanf("%lld", &val[i]);
}
build();
for (int i = ; i <= q; i++){
int type, l, r, v;
scanf("%d", &type);
if (type == ){
scanf("%d%d%lld", &l, &r, &v);
modify(l, r, v);
}
else{
scanf("%lld", &v);
printf("%d\n", query(v));
}
}
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}
CodeForces 551E 分块的更多相关文章
- Codeforces 551E GukiZ and GukiZiana(分块思想)
题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...
- (分块)GukiZ and GukiZiana CodeForces - 551E
题意: 给你一段序列,并且有两种操作 操作①:将序列中从l-r每个元素加上x 操作②:在序列中找到ai=aj=y,j-i的最大值,如果找不到则输出-1 思路: 直接分块暴力即可 对于区间加,普通标记加 ...
- 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 444C 分块
题目链接:http://codeforces.com/problemset/problem/444/C 题意:给定一个长度为n的序列a[].起初a[i]=i,然后还有一个色度的序列b[],起初b[i] ...
- CodeForces 455D 分块
题目链接:http://codeforces.com/problemset/problem/455/D 题意:给定一个长度为n的序列a[]. m次操作.共有两种操作 1 l r:将序列的a[l].a[ ...
- CodeForces 103D 分块处理
题目链接:http://codeforces.com/problemset/problem/103/D 题意:给定一个长度为n的序列.然后q个询问.每个询问为(a,b),表示从序列第a项开始每b项的加 ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块
Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...
- CodeForces 13E 分块
题目链接:http://codeforces.com/problemset/problem/13/E 题意:给定n个弹簧和每个弹簧初始的弹力a[].当球落在第i个位置.则球会被弹到i+a[i]的位置. ...
随机推荐
- BZOJ4285: 使者
搞出dfs序,转化为查询矩形点数,树套树搞定. #include<cstdio> #include<cstdlib> #define N 100005 #define IF e ...
- 树莓派笔记之使用netselect选择最快Raspbian软件源
背景: 之前在葉難大大的部落格里看到有讲可以使用netselect查找最快软件源,今天正好看到, 特此记下来,因为之前一直使用中国科学技术大学的源,结果发现不是我这里最快的. 注意: 以下仅对Rasp ...
- C#汉字字母数字正则
http://novell.me/master-diary/2014-11-15/regular-express-csharp-example.html https://msdn.microsoft. ...
- js 无线弹窗
无限弹窗 function fad(action){ alert('); ht = setTimeout() } fad(); fad()能放在 $().ready() 里面,效果也一样
- IIS错误处理集合
1.编译器错误消息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ya ...
- [NHibernate]基本配置与测试
目录 写在前面 nhibernate文档 搭建项目 映射文件 持久化类 辅助类 数据库设计与连接配置 测试 总结 写在前面 一年前刚来这家公司,发现项目中使用的ORM是Nhibernate,这个之前确 ...
- Androidstudio报错UnsupportedClassVersionError
报错信息 Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.m ...
- Bash 会清空从父进程继承来的 OLDPWD
即便 Bash 没有从父进程继承任何的环境变量,Bash 自己也会创建三个环境变量,分别是: $ env -i bash -c export declare -x OLDPWD declare -x ...
- CSS百分比定义高度的冷知识
当我们给块级元素设置响应式高度的时候,例如给div设置height=50%,往往没能看到效果. 原因是百分比的大小是相对其父级元素宽高的大小,如最外层元素设置的百分比是对应屏幕而言的. 需要了解的是对 ...
- Linux进程间通信(一): 信号 signal()、sigaction()
一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过生成信号和捕获信号来实现的,运行中 ...