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]的位置. ...
随机推荐
- 网络抓包wireshark
抓包应该是每个技术人员掌握的基础知识,无论是技术支持运维人员或者是研发,多少都会遇到要抓包的情况,用过的抓包工具有fiddle.wireshark,作为一个不是经常要抓包的人员,学会用Wireshar ...
- List拆分成多个集合
如果对一组大的集合进行操作,想分组进行,比如批量新增10000条数据,想100条分成一个集合分成100个集合,对集合进行操作100次,用C#如何编写,这里记录下代码如下 //构造被分隔的集合 List ...
- PHP利用P3P实现跨域
本文转自:点这里 有别于JS跨域.IFRAME跨域等的常用处理办法,还可以利用P3P来实现跨域. P3P是什么 P3P(Platform for Privacy Preferences)是W3C公布的 ...
- ASP.NET 实现登陆验证
public class ValidModule : IHttpModule { /// <summary> /// 您将需要在网站的 Web.config 文件中配置此模块 /// 并向 ...
- Codeforces 696 D. Legen...
Description 每个字符串有些价值,问生成长度为 \(l\) 的字符串最多能获得多少价值,总字符数不超过 \(200\), \(l\leqslant 10^{14}\) . Sol AC自动机 ...
- Java连接mysql数据库并插入中文数据显示乱码
连接数据库设置编码 jdbc:mysql://地址:3306/数据库名?characterEncoding=utf8
- DES & 3DES 加密算法
JAVA坑 跟其他公司java的对接口,一个细节对到吐血,具体: DesUtil.java(别人的反例) //package base_class; import java.io.IOExceptio ...
- Android 手机摇一摇功能的实现
package myapplication.com.myapp.activity; public class Home_Activity extends AppCompatActivity{ //传感 ...
- [转]Struts2工作原理
Struts2请求响应流程: 在struts2的应用中,从用户请求到服务器返回相应响应给用户端的过程中,包含了许多组件如:Controller.ActionProxy.ActionMapping.Co ...
- MySql: 忘记root密码
win7 + mysql 5.6.35 C:\Windows\system32>mysql --versionmysql Ver 14.14 Distrib 5.6.35, for Win64 ...