QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#726850#9543. Good PartitionsCabbageTL 123ms8244kbC++143.1kb2024-11-09 09:29:312024-11-09 09:29:39

Judging History

你现在查看的是最新测评结果

  • [2024-11-09 09:29:39]
  • 评测
  • 测评结果:TL
  • 用时:123ms
  • 内存:8244kb
  • [2024-11-09 09:29:31]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+5;
int nums[maxn];
int v[maxn];
struct Seg{
    int val[maxn<<2];
    void pushup(int p){
        val[p]=__gcd(val[p<<1],val[p<<1|1]);
    }
    void build(int p,int l,int r){
        if(l==r){
            val[p]=v[l];
            return;
        }
        int mid=l+r>>1;
        build(p<<1,l,mid);
        build(p<<1|1,mid+1,r);
        pushup(p);
    }
    void update(int p,int l,int r,int np,int nv){
        if(l==r){
            val[p]=nv;
            return;
        }
        int mid=l+r>>1;
        if(np<=mid)update(p<<1,l,mid,np,nv);
        else update(p<<1|1,mid+1,r,np,nv);
        pushup(p);
    }
    int query(int p,int l,int r,int st,int en){
        if(st<=l&&en>=r){
            return val[p];
        }
        int mid=l+r>>1;
        int gd=0;
        if(st<=mid)gd=__gcd(gd,query(p<<1,l,mid,st,en));
        if(en>mid)gd=__gcd(gd,query(p<<1|1,mid+1,r,st,en));
        return gd;
    }
}T;
void slove(){
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        cin>>nums[i];
        v[i]=0;
    }
    set<int> s;
    s.insert(0);
    s.insert(1);
    for(int i=2;i<=n;i++){
        if(nums[i]<nums[i-1]){
            s.insert(i);
            auto le=lower_bound(s.begin(),s.end(),i);
            le--;
            v[i]=(i-*le);
        }
    }
    T.build(1,1,n);
    auto change=[&](int x){
        if(nums[x]<nums[x-1]){
            //如果改变后形成了新逆转点
            if(!s.count(x)){
                auto le=lower_bound(s.begin(),s.end(),x);
                le--;
                auto ri=upper_bound(s.begin(),s.end(),x);
                T.update(1,1,n,x,x-*le);
                v[x]=x-*le;
                if(ri!=s.end()){
                    T.update(1,1,n,*ri,*ri-x);
                    v[*ri]=*ri-x;
                }
                s.insert(x);
            }
        }else{
            //如果改变后不是逆转点了
            if(s.count(x)){
                auto le=lower_bound(s.begin(),s.end(),x);
                le--;
                auto ri=upper_bound(s.begin(),s.end(),x);
                if(ri!=s.end()){
                    T.update(1,1,n,*ri,*ri-*le);
                    v[*ri]=*ri-*le;
                }
                T.update(1,1,n,x,0);
                v[x]=0;
                s.erase(x);
            }
        }
    };
    auto ans=[&](int x){
        int res=0;
        for(int i=1;i*i<=x;i++){
            if(x%i==0){
                if(i*i!=x)res+=2;
                else res++;
            }
        }
        return res;
    };
    if(s.size()!=2)cout<<ans(T.query(1,1,n,1,n))<<'\n';
    else cout<<n<<'\n';
    while(q--){
        int np,nv;
        cin>>np>>nv;
        nums[np]=nv;
        change(np);
        change(np+1);
        if(s.size()!=2)cout<<ans(T.query(1,1,n,1,n))<<'\n';
        else cout<<n<<'\n';
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int _=1;
    cin>>_;
    while(_--)slove();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3628kb

input:

1
5 2
4 3 2 6 1
2 5
3 5

output:

1
2
3

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 5672kb

input:

1
1 1
2000000000
1 1999999999

output:

1
1

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 123ms
memory: 8244kb

input:

1
200000 200000
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 ...

output:

160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160...

result:

ok 200001 lines

Test #4:

score: -100
Time Limit Exceeded

input:

1
200000 200000
200001 200000 199999 199998 199997 199996 199995 199994 199993 199992 199991 199990 199989 199988 199987 199986 199985 199984 199983 199982 199981 199980 199979 199978 199977 199976 199975 199974 199973 199972 199971 199970 199969 199968 199967 199966 199965 199964 199963 199962 1999...

output:


result: