[LOJ] 分块九题 6
单点插入,单点查询。
优化了的链表。
链表老写错,干脆用vector,也不算慢。
注意链表退化的问题,及时(比如操作根号n次)就重新建块,实测速度可以提高一倍,这还是数据随机的情况,若涉及大量同一位置插入,会让分块大大退化。
build没必要写两个。
//Stay foolish,stay hungry,stay young,stay simple
#include<iostream>
#include<cmath>
#include<cstring>
#include<cctype>
#include<vector>
#define vit vector<int>::iterator
using namespace std;
const int MAXN=500005;
inline int read_d(){
int ret=0,f=1;char c;
while(c=getchar(),!isdigit(c)) f=c=='-'?-1:1;
while(isdigit(c)){
ret=ret*10+c-'0';
c=getchar();
}
return ret*f;
}
int n;
int block,num;
vector<int> lis[1000];
int a[MAXN];
void build(int pos){
block=sqrt(n);
num=n/block;
if(n%block) num++;
for(int i=1;i<=num;i++){
for(vit it=lis[i].begin();it!=lis[i].end();it++)
a[++pos]=*it;
lis[i].clear();
}
for(int i=1;i<=num;i++)
for(int j=(i-1)*block+1;j<=i*block&&j<=n;j++)
lis[i].push_back(a[j]);
}
int query(int x){
int pos=1,siz=0;
while(siz+lis[pos].size()<x) siz+=lis[pos++].size();
return lis[pos][x-siz-1];
}
int tim;
void updata(int x,int w){
int pos=1,siz=0;tim++;n++;
while(siz+lis[pos].size()<x) siz+=lis[pos++].size();
lis[pos].insert(lis[pos].begin()+x-siz-1,w);
if(tim>block) build(0),tim=0;
}
int main(){
n=read_d();
for(int i=1;i<=n;i++) a[i]=read_d();
build(n);
int T=n;
for(int i=1;i<=T;i++){
int q=read_d(),x=read_d(),y=read_d(),z=read_d();
if(q==0) updata(x,y);
else printf("%d\n",query(y));
}
return 0;
}
[LOJ] 分块九题 6的更多相关文章
- [LOJ] 分块九题 4
https://loj.ac/problem/6280 区间修改,区间求和. 本来线段树的活. //Stay foolish,stay hungry,stay young,stay simple #i ...
- [LOJ] 分块九题 3
https://loj.ac/problem/6279 区间修改,区间查询前驱. TLE无数,我觉得这代码最精髓的就是block=1000. 谜一样的1000. 两个启示: 块内可以维护数据结构,比如 ...
- [LOJ] 分块九题 2
https://loj.ac/problem/6278 区间修改,查询区间第k大. 块内有序(另存),块内二分. 还是用vector吧,数组拷贝排序,下标搞不来.. //Stay foolish,st ...
- [LOJ] 分块九题 1
https://loj.ac/problem/6277 区间修改,单点查询. //Stay foolish,stay hungry,stay young,stay simple #include< ...
- [LOJ] 分块九题 8
区间查询数值+整体赋值 维护tag代表整个区间被赋成了tag[i] 用pushdown操作,而不是修改了再check. 不压缩代码了,调起来心累,长点有啥不好. //Stay foolish,stay ...
- [LOJ] 分块九题 7
区间加法,区间乘法,单点查询. 洛谷线段树2 屡清加法乘法的关系,定义答案为 a*mut+add 对于整块: 新的乘w,mut和add都要乘w 新的加w,add加w //Stay foolish,st ...
- [LOJ] 分块九题 5
区间开平方,区间查询. lazy标记改为区间是否全是1或者0,这样的区间是没有更新价值的. //Stay foolish,stay hungry,stay young,stay simple #inc ...
- 数列分块总结——题目总版(hzwer分块九题及其他题目)(分块)
闲话 莫队算法似乎还是需要一点分块思想的......于是我就先来搞分块啦! 膜拜hzwer学长神犇%%%Orz 这九道题,每一道都堪称经典,强力打Call!点这里进入 算法简述 每一次考试被炸得体无完 ...
- hzwer分块九题(暂时持续更新)
hzwer分块9题 分块1:区间加法,单点查询 Code #include<bits/stdc++.h> #define in(i) (i=read()) using namespace ...
随机推荐
- 你真的懂了redis的数据结构吗?redis内部数据结构和外部数据结构揭秘
原文链接:https://mp.weixin.qq.com/s/hKpAxPE-9HJgV6GEdV4WoA Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有 ...
- CSS3鼠标放上去旋转代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 模板——扩展欧几里得算法(求ax+by=gcd的解)
Bryce1010模板 /**** *扩展欧几里得算法 *返回d=gcd(a,b),和对应等式ax+by=d中的x,y */ long long extend_gcd(long long a,long ...
- Educational Codeforces Round 18 B
Description n children are standing in a circle and playing the counting-out game. Children are numb ...
- PopupWindow(2)简单示例-自定义弹出菜单
本示例,用 popupWindow 自定义弹出菜单 public class CustomActionProvider extends ActionProvider implements OnMenu ...
- word-wrap与word-break为长单词换行
如果你遇到长串英文单词或者url换行的问题,这时候就需要用到word-wrap与word-break这2个css属性啦. word-wrap:break-word;长单词与url地址自动换行. wor ...
- SOLRJ单机-添加文档,删除,查询操作
单机solrJ不需要占用太多的服务器和资源,本机使用solr-6.3.0,也不需要配置tomcat. 一.新建一个java工程需要依赖的jar包如下: solr-solrj-6.3.0.jar; c ...
- KMS算法
解题:http://hihocoder.com/problemset/problem/1015 KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt同时 ...
- window服务 调试步骤
方法一: 1.编译windows服务项目工程 2.把服务注册到系统服务上 3.在visual studio 编辑器中,打断点,用 Debug 进程调试 方法二: 在Onstart 方法中,加上 De ...
- java8的lambda表达式,将List<DTO> 转为 List<DO>
将List<PhoneDTO>转为List<PhoneDO>,通过java8的lambda表达式来操作,比传统的for循环精简很多: /** * List<PhoneDT ...