Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树
思路:
(我也不知道这是不是正解)
ST表预处理出来原数列的两点之间的min
再搞一个动态开节点线段树
节点记录ans 和标记
lazy=-1 当前节点的ans可用 lazy=0 没被覆盖过 else 区间覆盖
push_up的时候要注意好多细节,,
数组尽量往大开
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=;
int n,k,q,op,xx,b[N],f[N][],lson[N<<],rson[N<<],root,cnt;
int lazy[N<<],ans[N<<],base[N];//lazy=-1 废了 lazy=0 原数列 else 区间覆盖
int RMQ(int x,int y){
int t=base[y-x+];
return min(f[x][t],f[y-(<<t)+][t]);
}
int qry(int l,int r){
if(r-l>=n)return RMQ(,n);
else{
l=(l-)%n+,r=(r-)%n+;
if(l>r)return min(RMQ(,r),RMQ(l,n));
else return RMQ(l,r);
}
}
void push_down(int pos){
if(!lson[pos])lson[pos]=++cnt;lazy[lson[pos]]=lazy[pos];
if(!rson[pos])rson[pos]=++cnt;lazy[rson[pos]]=lazy[pos];
}
void insert(int l,int r,int &pos,int L,int R,int wei){
if(!pos)pos=++cnt;
// printf("l=%lld r=%lld pos=%d\n",l,r,pos);
if(lazy[pos]&&~lazy[pos]){
ans[pos]=lazy[pos];
push_down(pos);
lazy[pos]=-;
}
if(l>=L&&r<=R){ans[pos]=lazy[pos]=xx;return;}
int mid=(l+r)>>;
if(mid<L)insert(mid+,r,rson[pos],L,R,wei);
else if(mid>=R)insert(l,mid,lson[pos],L,R,wei);
else insert(l,mid,lson[pos],L,R,wei),insert(mid+,r,rson[pos],L,R,wei);
int temp=0x3f3f3f3f;
if(lson[pos]){
if(!lazy[lson[pos]])temp=qry(l,mid);
else if(lazy[lson[pos]]==-)temp=ans[lson[pos]];
else temp=lazy[lson[pos]];
}
else temp=qry(l,mid);
if(lazy[rson[pos]]){
if(!lazy[rson[pos]])temp=min(temp,qry(mid+,r));
else if(lazy[rson[pos]]==-)temp=min(temp,ans[rson[pos]]);
else temp=min(temp,lazy[rson[pos]]);
}
else temp=min(temp,qry(mid+,r));
ans[pos]=temp,lazy[pos]=-;
}
int query(int l,int r,int pos,int L,int R){
if(l==L&&r==R){
if(lazy[pos]==-)return ans[pos];
else if(!lazy[pos])return qry(l,r);
else return lazy[pos];
}
if(!pos)return qry(L,R);
if(lazy[pos]&&~lazy[pos])return lazy[pos];
int mid=(l+r)>>;
if(mid<L)return query(mid+,r,rson[pos],L,R);
else if(mid>=R)return query(l,mid,lson[pos],L,R);
else return min(query(l,mid,lson[pos],L,mid),query(mid+,r,rson[pos],mid+,R));
}
void DFS(int l,int r,int pos){
printf("l=%d r=%d pos=%d lazy[pos]=%d ans[pos]=%d\n",l,r,pos,lazy[pos],ans[pos]);
int mid=(l+r)>>;
if(lson[pos])DFS(l,mid,lson[pos]);
if(rson[pos])DFS(mid+,r,rson[pos]);
}
int main(){
scanf("%d%d",&n,&k);
base[]=-;
for(int i=;i<=n;i++)scanf("%d",&b[i]),f[i][]=b[i],base[i]=base[i>>]+;
for(int j=;j<=;j++)
for(int i=;i+(<<(j-))<=n;i++)
f[i][j]=min(f[i][j-],f[i+(<<(j-))][j-]);
scanf("%d",&q);
while(q--){
int l,r;
scanf("%d%d%d",&op,&l,&r);
if(op==){
scanf("%d",&xx);
insert(,n*k,root,l,r,xx);
}
else{
printf("%d\n",query(,n*k,root,l,r));
}
// DFS(1,1ll*n*k,1);
}
}
/*
8 4
5 6 1 3 3 2 9 6 14
1 10 19 2
2 14 26 */
Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树的更多相关文章
- codeforces 803G Periodic RMQ Problem
codeforces 803G Periodic RMQ Problem 题意 长度为\(1e5\)的数组复制\(1e4\)次,对新的数组进行区间覆盖和区间最小值查询两种操作,操作次数\(1e5\). ...
- 洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)
题意 题目链接 Sol 树链剖分板子 + 动态开节点线段树板子 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...
- 洛谷P3120 [USACO15FEB]牛跳房子(动态开节点线段树)
题意 题目链接 Sol \(f[i][j]\)表示前\(i\)行\(j\)列的贡献,转移的时候枚举从哪里转移而来,复杂度\(O(n^4)\) 然后考虑每一行的贡献,动态开节点线段树维护一下每种颜色的答 ...
- BZOJ4636: 蒟蒻的数列(动态开节点线段树)
题意 题目链接 Sol 直接上动态开节点线段树 因为只有一次询问,所以中途不需要下传标记 #include<bits/stdc++.h> #define LL long long usin ...
- 洛谷P3960 列队(动态开节点线段树)
题意 题目链接 Sol 看不懂splay..,看不懂树状数组... 只会暴力动态开节点线段树 观察之后不难发现,我们对于行和列需要支持的操作都是相同的:找到第\(k\)大的元素并删除,在末尾插入一个元 ...
- Codeforces 803G Periodic RMQ Problem 线段树
Periodic RMQ Problem 动态开点线段树直接搞, 我把它分成两部分, 一部分是原来树上的, 一部分是后来染上去的,两个部分取最小值. 感觉有点难写.. #include<bits ...
- 洛谷P4632 [APIO2018] New Home 新家(动态开节点线段树 二分答案 扫描线 set)
题意 题目链接 Sol 这题没有想象中的那么难,但也绝对不简单. 首先把所有的询问离线,按照出现的顺序.维护时间轴来处理每个询问 对于每个询问\((x_i, y_i)\),可以二分答案\(mid\). ...
- BZOJ 3065 替罪羊树+动态开节点线段树
思路: RT 可以看VFK的题解 我写了半天拍了半天... 不过是$nlog^2n$的 要写垃圾回收的 线段树 如果某个节点的sum是0 也可以free掉 //By SiriusRen #inclu ...
- codeforces 893F - Physical Education Lessons 动态开点线段树合并
https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...
随机推荐
- Python爬虫入门教程第七讲: 蜂鸟网图片爬取之二
蜂鸟网图片--简介 今天玩点新鲜的,使用一个新库 aiohttp ,利用它提高咱爬虫的爬取速度. 安装模块常规套路 pip install aiohttp 运行之后等待,安装完毕,想要深造,那么官方文 ...
- c++行事准则
1.c++包含多种编程范式,每种范式遵循不同的准则: 2.除了#include和#ifdef之流,其他预处理器别用,多用enum和const: 3.函数声明定义多用const:
- 这个贴子的内容值得好好学习--实例详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化
感觉要DJANGO用得好,ORM必须要学好,不管理是内置的,还是第三方的ORM. 最最后还是要到SQL.....:( 这一关,慢慢练啦.. 实例详解Django的 select_related 和 p ...
- Nginx 重写规则指南
作者:运维生存时间 - 默北 链接:www.ttlsa.com/nginx/nginx-rewriting-rules-guide/ 当运维遇到要重写情况时,往往是要程序员把重写规则写好后,发给你,你 ...
- Redis: Useful commands
SELECT X - Select database (the X must be int) CONFIG GET databases - Get databases number INFO keys ...
- CentOS 7: Install vsftpd
Install vsftpd All commands should be run with ‘root’ user. Run the following command in terminal to ...
- linux sed 命令的用法
原文 http://blog.chinaunix.net/uid-24426415-id-77244.html ------------------------------------------- ...
- ubuntu 网络监控 nethogs
***网络监控ubuntu自带的 netstat -an 查看当前网络状况 sudo netstat -anp 查看当前网络状况带对应进程号 traceroute 追踪路由 ***我比较喜欢用Neth ...
- Windows下擴展ubuntu虛擬機的分區大小
在虛擬分區上安裝ubuntu,8G的分区不够用,不願意重装,增加VM分区吧!先备份虛擬硬盤文件 VMWARE自带的工具:找到vmware安装目录下vmware-vdiskmanager.exe,双击無 ...
- [Mini Programe] Upload Images
Code for upload iamges: chooseImage: choose the images to upload previewImage: preview the image and ...