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]的位置. ...
随机推荐
- Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)
传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...
- [bigdata] Spark RDD整理
1. RDD是什么RDD:Spark的核心概念是RDD (resilient distributed dataset),指的是一个只读的,可分区的弹性分布式数据集,这个数据集的全部或部分可以缓存在内存 ...
- BZOJ1483: [HNOI2009]梦幻布丁
传送门 名字起得很高端实际上很简单的算法hhh 启发式合并 简单讲就是一些合并一堆队列的题可以用启发式合并,或者说这是一个思想.每次把小的合并到大的部分,均摊复杂度$O(MlogN)$. //BZOJ ...
- WEKA使用
参考 http://bbs.middleware123.com/thread-24052-1-1.html 使用Weka进行数据挖掘 http://quweiprotoss.blog.163.com ...
- gRPC+etcd的优势分析
相比webService等可跨平台,跨语言的服务相比,gRPC更增加了以下优势 1.可以采用二进制传输,速度更快 (使用TCP传输层,而不是Http2应用层) 2.集群服务,统一注册,可靠性高( 好的 ...
- JAVA起名规范
1:包名 package com.cenzhongman.模块名.组件 必须全部小写,作为java文件第一行代码 2:类名 名词,表示一类实物,如:人类 首字母大写 3.接口名 形容词/副词,表示一种 ...
- Win7 64位下PowerDesigner连接64位Oracle11g数据库
操作系统:WIN7 64旗舰版 Oracle版本:64位11g PowerDesigner版本:15.1 问题描述:因为PowerDesigner是32的程序,连接数据库会默认开启32位的ODBC,因 ...
- 如何刷新或清除HttpURLConnection的连接缓存
项目需要定期与远程服务器同步数据,基于如下代码: URL url = new URL("http://test.com/sales/info"); connection = (Ht ...
- 【转载】使用pandas进行数据清洗
使用pandas进行数据清洗 本文转载自:蓝鲸的网站分析笔记 原文链接:使用python进行数据清洗 目录: 数据表中的重复值 duplicated() drop_duplicated() 数据表中的 ...
- ubuntu12.04server下red5-1.0.0RC1的部署
一.搭建环境 Linux版本:ubuntu12.04sever 64位 Java 版本:Java 1.7(jdk+jre) Red5 版本:red5-1.0.0-RC1 二.安装JDK 下载jdk ...