[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 ...
随机推荐
- python __builtins__ list类 (42)
42.'list', 转换为列表类型 class list(object) | list() -> new empty list | list(iterable) -> new list ...
- bzoj 3625: [Codeforces Round #250]小朋友和二叉树【NTT+多项式开根求逆】
参考:https://www.cnblogs.com/2016gdgzoi509/p/8999460.html 列出生成函数方程,g(x)是价值x的个数 \[ f(x)=g(x)*f^2(x)+1 \ ...
- thinkphp5升级版开源框架tpframe v2.1发布
免费开源框架tpframe是一款以thinkphp5为驱动,在此基础上进行进一步的完善与改进的框架,保持了ThinkPHP5原有的所有特性,优化核心,减少依赖,为个人或企业建站提供高效.快速解决的方案 ...
- IT兄弟连 ElasticSearch在Linux下的安装和启动、常见问题解决
环境要求 · Linux(Centos 7) · ElasticSerach 6.6.1 ES下载 · 下载地址:https://www.elastic.co/cn/do ...
- python之文件的读写
读 r 读写模式 r+ 如果打开文件时没有指定模式,默认只读,如果使用r或r+,文件不存在时会报错 写 w 写读模式 w+ w模式会清空原有的文件内容 追加 a 追加读 ...
- linux之lamp环境的搭建
linux之lamp环境的搭建 1.安装lamp环境的安装工具 我们的lamp环境都是采用源码包进行编译安装: 编译安装需要工具gcc gcc-c++. 建议在线联网安装:yum -install ...
- js中关于this的理解
常见:在普通函数中的this,指向 全局 function thisObj(name){ this.name = name; console.log(this); } 但是在严格模式下的函数,this ...
- AJPFX总结方法里的属性
嵌套循环:循环里套循环 假设外循环的循环次数是m次,内循环的循环次数是n次,那么内层循环的循环次数需要 m * n次. Eg:利用for循环语句的嵌套打印出乘法口诀表 class break1 ...
- 关于HashMap中hash()函数的思考
关于HashMap中hash()函数的思考 JDK7中hash函数的实现 static int hash(int h) { h ^= (h >>> 20) ^ (h >&g ...
- 日常博客----rem,em的恩怨相杀
基本知识: 使用 em 和 rem 单位可以让我们的设计更加灵活,能够控制元素整体放大缩小,而不是固定大小. 我们可以使用这种灵活性,使我们在开发期间,能更加快速灵活的调整,允许浏览器用户调整浏览器大 ...