倍增基础-st总结

· · 个人记录

1.U519510 ١١(❛ᴗ❛)【倍增】-最小线段合并

思想跟下一题表很像,每次减去 2^{max} 拆分线段即可

Ac Code

#include<bits/stdc++.h>
using namespace std;
int n;
int ans=0;
int get_max(int x){
    int cnt=1;
    if(x==1)return 1;
    //x>=cnt*2变式,防止爆int
    while(cnt<=x/2){
        cnt*=2;
    }
    return cnt;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    //特判
    if(n==0){
        cout<<-1;
        return 0;
    }
    //n一定会减到0
    while(n){
        n-=get_max(n);
        ans++;//线段数增加
    }
    cout<<ans;
    return 0;
} 

本题运用了倍增的思想,把线段分为多个二次幂的子线段,我们也能将大问题分解为小问题,得出问题的答案

2.P3865 【模板】ST 表 && RMQ 问题

本题 $1≤N≤10^5,1≤M≤2×10^6$ 数据较大,时间复杂度较高,以至于要用快读,暴力不行 我们可以用 $st$ 表,存子问题答案( $O(nlog(n)$ ), 查询也只需 $O(1)$ 的复杂度 **Ac Code** ```cpp #include<bits/stdc++.h> using namespace std; const int N=1e5+7; int st[N][25]; //st[i][j]表示下标从i开始,长度为2^k的区间最大值 int lg[N]; int n,m; void st_init(){ lg[0]=-1; for(int i=1;i<=n;i++){ lg[i]=lg[i/2]+1; } for(int len=1;(1<<len)<=n;len++){ for(int i=1;i+(1<<len)-1<=n;i++){ st[i][len]=max(st[i][len-1],st[i+(1<<(len-1))][len-1]);//存子问题最大值 } } return ; } int qiery(int l,int r){ int len=lg[r-l+1]; //min,max,gcd,lcm求解区间合并,无重叠状态影响 return max(st[l][len],st[r-(1<<len)+1][len]); } int main(){ //快读 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++){ cin>>st[i][0];//初始为2^0 } st_init(); while(m--){ int l,r; cin>>l>>r; cout<<qiery(l,r)<<'\n';//用\n } return 0; } ``` --- 3.[**P1890 gcd区间**](https://www.luogu.com.cn/problem/P1890) 把 $max$ 换成 $gcd$ 即可 **Ac Code** ```cpp #include<bits/stdc++.h> using namespace std; const int N=1e5+7; int st[N][25]; int lg[N]; int n,m; void st_init(){ lg[0]=-1; for(int i=1;i<=n;i++){ lg[i]=lg[i/2]+1; } for(int len=1;(1<<len)<=n;len++){ for(int i=1;i+(1<<len)-1<=n;i++){ //本题未卡gcd,但建议手写一个 st[i][len]=__gcd(st[i][len-1],st[i+(1<<(len-1))][len-1]); } } return ; } int qiery(int l,int r){ int len=lg[r-l+1]; return __gcd(st[l][len],st[r-(1<<len)+1][len]); } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++){ cin>>st[i][0]; } st_init(); while(m--){ int l,r; cin>>l>>r; cout<<qiery(l,r)<<'\n'; } return 0; } ``` --- 4.[**P1198 [JSOI2008] 最大数**](https://www.luogu.com.cn/problem/P1198) 若用原来方法,每次插入新元素,要重新初始化( $20$ 分),要写一个 $insert$ 函数 插入后 $st[len][i]=max(st[len][i-1],st[len+(1<<(i-1))][i-1])

Ac Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+7;
int n,m,d;
int t=0;
int st[N][65];
int lg[N];
void st_init(){
    lg[0]=-1;
    //初始化时范围到N
    for(int i=1;i<=N;i++){
        lg[i]=lg[i/2]+1;
    }
    return ;
}
void st_insert(){
    for(int i=1;i<=20;i++){
        if(n-(1<<i)>=0){
            int len=n-(1<<i)+1;
            st[len][i]=max(st[len][i-1],st[len+(1<<(i-1))][i-1]);
        }
        else{
            break;
        }
    }
    return ;
} 
int qiery(int l,int r){
    int len=lg[r-l+1];
    return max(st[l][len],st[r-(1<<len)+1][len]);
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>m>>d;
    st_init();//初始化
    while(m--){
        char s;
        int x;
        cin>>s>>x;
        if(s=='A'){
            x+=t;
            x%=d;
            st[++n][0]=x;
            st_insert();//插入
        }
        else{
            t=qiery(n-x+1,n);//存上次查询的答案
            cout<<t<<'\n';//查询
        }
    }
    return 0;
}