2026/7/15下午 总结

· · 个人记录

T1

题面

100分

简单模板

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a,b,p;
int quick_pow(int a,int b,int p){
    int ans=1ll;
    while(b){
        if(b&1){
            ans=ans*a%p;
        }
        b>>=1;
        a=a*a%p;
    }
    return ans;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>a>>b>>p;
    cout<<a<<"^"<<b<<" mod "<<p<<"="<<quick_pow(a,b,p);
    return 0;
}

T2

题面

100分

简单模板+1

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+50;
int n,m,ans,a[N],st[N][22],lg[N];
int ch(int l,int r){
    int k=lg[r-l+1];
    return max(st[l][k],st[r-(1<<k)+1][k]);
}
void init(){
    lg[1]=0;
    for(int i=2;i<=n;i++){
        lg[i]=lg[i/2]+1;
    }
    for(int j=1;j<=20;j++){
        for(int i=1;i+(1<<(j-1))-1<=n;i++){
            st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
        }
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        st[i][0]=a[i];
    }
    init();
    while(m--){
        int x,y;
        cin>>x>>y;
        cout<<ch(x,y)<<"\n"; 
    }
    return 0;
}

T3

题面

100分

调用模板求最大最小

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e4+50;
int n,m,st[N][22],dp[N][22],lg[N];
int ch(int l,int r){
    int k=lg[r-l+1];
    return max(st[l][k],st[r-(1<<k)+1][k])-min(dp[l][k],dp[r-(1<<k)+1][k]);
}
void init(){
    lg[1]=0;
    for(int i=2;i<=n;i++){
        lg[i]=lg[i/2]+1;
    }
    for(int j=1;j<=20;j++){
        for(int i=1;i+(1<<(j-1))-1<=n;i++){
            st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
            dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        }
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1,x;i<=n;i++){
        cin>>x;
        st[i][0]=dp[i][0]=x;
    }
    init();
    while(m--){
        int x,y;
        cin>>x>>y;
        cout<<ch(x,y)<<"\n"; 
    }
    return 0;
}

T4

题面

100分

骗分得来了5分

😑😑

100分做法

很显然大部分的数据结构:预处理+查询,要么单次查询占据优势,要么预处理时间长,但均分到单次查询的整体平均时间较低。

本题序列仅有 0和1,因此:

预处理 q0、q1、c 及 ST 表后,每次询问 O(1) 回答,总复杂度 O(n log n + m)。

#include<bits/stdc++.h>
using namespace std;
const int N=5e6+50;
int ans,n,m,a[N],c[N],st[N][22],lg[N],q1[N],q0[N];
int ch(int l,int r){
    int k=lg[r-l+1];
    return max(st[l][k],st[r-(1<<k)+1][k]);
}
void init(){
    lg[1]=0;
    for(int i=2;i<=n;i++){
        lg[i]=lg[i/2]+1;
    }
    for(int j=1;j<=20;j++){
        for(int i=1;i+(1<<(j-1))-1<=n;i++){
            st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
        }
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(a[i]){
            q1[i]=1;
        }else{
            q0[i]=1;
        }
        q1[i]+=q1[i-1];
        q0[i]+=q0[i-1];
        st[i][0]=q0[i-1]-q1[i];
        c[i]=c[i-1]+(a[i]==1&&a[i-1]==0);
    }
    init(); 
    while(m--){
        int x,l,r;
        cin>>x>>l>>r;
        if(x==1){
            ans=max(1,q1[r]-q0[l-1]+ch(l,r)+1);
            cout<<ans<<"\n";
        }else{
            if(c[r]==c[l])cout<<"1\n";
            else cout<<"2\n";
        }
    }
    return 0;
}