ST表
基础
ST表是一种离线-静态数据结构,常用于解决RMQ-区间最值问题
ST表是一种基于倍增的数据结构,采取了空间换时间的策略,先预处理i后面(包括i)2 j 2^j 2 j 个数的最值,当然,我们为了提高效率,要预处理第一小于i的2的整数次幂,这样可以有效防止多预处理导致越界,当然我们也要注意一些边界,比如我们i<=n-(1<<j)+1就是防止多预处理导致RE,我们由于把自己算进去了,我们st[i][j]=max(st[i][j-1],st[i+1<<(j-1)][j-1]),我们最后查询时为了快速得到答案,我们用两段区间来覆盖当前区间,是因为最大值重复不影响结果,但如果是求和就要logn
这道题就是用二进制的思想,特水
代码:
C++ 复制 #include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N= 1 e 5 + 10 ,M= 2 e 1 + 10 ;
int st[N][M],b[N],a[N];
signed main (){
int n;
cin>> n;
if (! n){
puts ("-1" );
return 0 ;
}
int s1= 0 ;
while (n){
s1++ ;
int t= 1 ;
while (t<= n/ 2 ) t*= 2 ;
n-= t;
}
cout<< s1<< endl;
return 0 ;
}
这道题ST表模板题,但要用快读
代码:
C++ 复制 #include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N= 1 e 5 + 10 ,M= 2 e 1 + 10 ;
int st[N][M],b[N],a[N];
int n,m;
void ST (){
for (int i= 1 ;i<= n;i++ ) st[i][0 ]= a[i];
b[1 ]= 0 ;
for (int i= 2 ;i<= n;i++ ) b[i]= b[i>> 1 ]+ 1 ;
for (int j= 1 ;j<= b[n];j++ ){
for (int i= 1 ;i<= n- (1 << j)+ 1 ;i++ ){
st[i][j]= max (st[i][j- 1 ],st[i+ (1 << (j- 1 ))][j- 1 ]);
}
}
}
inline int read (){
int x= 0 ,f= 1 ;char ch= getchar ();
while (ch< '0' || ch> '9' ){if (ch== '-' ) f=- 1 ;ch= getchar ();}
while (ch>= '0' && ch<= '9' ){x= x* 10 + ch- 48 ;ch= getchar ();}
return x* f;
}
signed main (){
cin>> n>> m;
for (int i= 1 ;i<= n;i++ ) a[i]= read ();
ST ();
for (int i= 1 ;i<= m;i++ ){
int l,r;
l= read (),r= read ();
int k= b[r- l+ 1 ];
int s1= max (st[l][k],st[r- (1 << k)+ 1 ][k]);
cout<< s1<< " \n " ;
}
return 0 ;
}
这道题与普通的ST表只是将max改为了gcd,但是可以发现gcd和max一样也可以重复覆盖,但却对答案没有影响,我们只需对ST表模板进行修改就可以通过,gcd用辗转相除法,应该都会
代码:
C++ 复制 #include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N= 1 e 5 + 10 ,M= 2 e 1 + 10 ;
int st[N][M],b[N],a[N];
int n,m;
int gcd (int a ,int b ){
if (! b) return a;
else return gcd (b,a% b);
}
void ST (){
for (int i= 1 ;i<= n;i++ ) st[i][0 ]= a[i];
b[1 ]= 0 ;
for (int i= 2 ;i<= n;i++ ) b[i]= b[i>> 1 ]+ 1 ;
for (int j= 1 ;j<= b[n];j++ ){
for (int i= 1 ;i<= n- (1 << j)+ 1 ;i++ ){
st[i][j]= gcd (st[i][j- 1 ],st[i+ (1 << (j- 1 ))][j- 1 ]);
}
}
}
inline int read (){
int x= 0 ,f= 1 ;char ch= getchar ();
while (ch< '0' || ch> '9' ){if (ch== '-' ) f=- 1 ;ch= getchar ();}
while (ch>= '0' && ch<= '9' ){x= x* 10 + ch- 48 ;ch= getchar ();}
return x* f;
}
signed main (){
cin>> n>> m;
for (int i= 1 ;i<= n;i++ ) a[i]= read ();
ST ();
for (int i= 1 ;i<= m;i++ ){
int l,r;
l= read (),r= read ();
int k= b[r- l+ 1 ];
int s1= gcd (st[l][k],st[r- (1 << k)+ 1 ][k]);
cout<< s1<< " \n " ;
}
return 0 ;
}
这道题其实是一道线段树模板题,但可以用ST表水过去
线段树思路就十分简单,这里就只讲ST表支持尾插的方法
尾插的思路是每一次插入看那些点可以多一个2次方幂,这样,每次插入O(logn),查询O(1),比线段树快一些,但是有一些情况用不了
代码:
线段树:
C++ 复制 #include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N= 2 e 5 + 10 ;
struct tree {
int l,r,max,lazy;
}tr[N* 4 ];
void push_up (int x ){
tr[x].max= max (tr[x* 2 ].max,tr[x* 2 + 1 ].max);
}
void push_down (int x ){
if (tr[x].lazy){
tr[x* 2 ].max+= tr[x].lazy;
tr[x* 2 ].max+= tr[x].lazy;
tr[x* 2 + 1 ].max+= tr[x].lazy;
tr[x* 2 + 1 ].max+= tr[x].lazy;
tr[x].lazy= 0 ;
}
}
void build (int x ,int l ,int r ){
tr[x].l= l,tr[x].r= r;
if (l== r) return ;
else {
int mid= (tr[x].l+ tr[x].r)/ 2 ;
build (x* 2 ,l,mid),build (x* 2 + 1 ,mid+ 1 ,r);
push_up (x);
}
}
void modify (int x ,int l ,int r ,int k ){
if (tr[x].l>= l&& tr[x].r<= r){
tr[x].max+= k;
tr[x].lazy+= k;
}
else {
push_down (x);
int mid= (tr[x].l+ tr[x].r)/ 2 ;
if (l<= mid) modify (x* 2 ,l,r,k);
if (r> mid) modify (x* 2 + 1 ,l,r,k);
push_up (x);
}
}
int query (int x ,int l ,int r ){
if (tr[x].l>= l&& tr[x].r<= r) return tr[x].max;
else {
push_down (x);
int mid= (tr[x].l+ tr[x].r)/ 2 ;
int s= INT_MIN;
if (l<= mid) s= max (s,query (x* 2 ,l,r));
if (r> mid) s= max (s,query (x* 2 + 1 ,l,r));
push_up (x);
return s;
}
}
signed main (){
int m,p;
cin>> m>> p;
build (1 ,1 ,m);
int k1= 0 ,t1= 0 ;
while (m-- ){
char xx;
int x;
cin>> xx>> x;
if (xx== 'A' ){
k1++ ;
modify (1 ,k1,k1,(t1+ x)% p);
}
else {
t1= query (1 ,k1- x+ 1 ,k1)% p;
cout<< t1% p<< endl;
}
}
return 0 ;
}
ST表:
C++ 复制 #include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N= 2 e 5 + 10 ,M= 2 e 1 + 10 ;
int st[N][M],b[N];
int n,p,k1;
void ST_init (){
b[1 ]= 0 ;
for (int i= 2 ;i<= n;i++ ) b[i]= b[i>> 1 ]+ 1 ;
}
void ST_insert (int x ){
st[k1][0 ]= x;
for (int j= 1 ;j<= b[n];j++ ){
if (k1- (1 << j)+ 1 > 0 ){
int t= k1- (1 << j)+ 1 ;
st[t][j]= max (st[t][j- 1 ],st[t+ (1 << (j- 1 ))][j- 1 ]);
}
else break ;
}
}
signed main (){
cin>> n>> p;
ST_init ();
int t1= 0 ;
for (int i= 1 ;i<= n;i++ ){
char xx;
int x;
cin>> xx>> x;
if (xx== 'A' ){
k1++ ;
ST_insert ((t1+ x)% p);
}
else {
int k= b[x];
t1= max (st[k1- x+ 1 ][k],st[k1- (1 << k)+ 1 ][k])% p;
cout<< t1<< endl;
}
}
return 0 ;
}