QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#726909#9543. Good PartitionsCabbageTL 57ms11336kbC++144.1kb2024-11-09 10:16:522024-11-09 10:16:52

Judging History

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

  • [2024-11-09 10:16:52]
  • 评测
  • 测评结果:TL
  • 用时:57ms
  • 内存:11336kb
  • [2024-11-09 10:16:52]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+5;
int nums[maxn];
int v[maxn];
int prim[maxn],vis[maxn], len;
int sum[maxn],d[maxn];
inline void eular_shai(int n)        // n < N
{
    for(int i = 2;i <= n;i ++) 
    {
        if(! vis[i])
            prim[len ++] = i;
        for(int j = 0;j < len && i * prim[j] <= n; j ++) 
        {
            vis[i * prim[j]] = 1;
            if(i % prim[j] == 0)
                break;
        }
    }
}
void eular_num(int n)
{
    d[1] = 1;                       //1 特判
    for(int i = 2;i <= n;i ++) 
    {
        if(! vis[i]) 
        {
            prim[len ++] = i;
            sum[i] = 1;
            d[i] = 2;	            //质数的约数只有1和它本身
        }
        for(int j = 0; j < len && i * prim[j] <= n; j ++) 
        {
            vis[i * prim[j]] = 1;
            if(i % prim[j] == 0)
            {
                sum[i * prim[j]] = sum[i] + 1;
                d[i * prim[j]] = d[i] / (sum[i] + 1) * (sum[i] + 2);
                break;
            }
            sum[i * prim[j]] = 1;
            d[i * prim[j]] = d[i] * 2;
        }
    }
}
inline int gcd(int a,int b){
    return !b?a:gcd(b,a%b);
}
int val[maxn<<2];
inline void pushup(int p){
    val[p]=gcd(val[p<<1],val[p<<1|1]);
}
inline 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);
}
inline 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);
}
inline 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;
}
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);
        }
    }
    build(1,1,n);
    auto change=[&](int x){
        if(x>1&&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);
                update(1,1,n,x,x-*le);
                v[x]=x-*le;
                if(ri!=s.end()){
                    update(1,1,n,*ri,*ri-x);
                    v[*ri]=*ri-x;
                }
                s.insert(x);
                return 1;
            }
            return 0;
        }else{
            //如果改变后不是逆转点了
            if(x!=1&&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()){
                    update(1,1,n,*ri,*ri-*le);
                    v[*ri]=*ri-*le;
                }
                update(1,1,n,x,0);
                v[x]=0;
                s.erase(x);
                return 1;
            }
            return 0;
        }
    };
    int res=0;
    if(s.size()!=2)res=d[query(1,1,n,1,n)];
    else res=n;
    if(s.size()!=2)cout<<res<<"\n";
    else cout<<n<<"\n";
    while(q--){
        int np,nv;
        cin>>np>>nv;
        nums[np]=nv;
        int fg=0;
        fg|=change(np);
        if(np<n)fg|=change(np+1);
        if(fg)res=d[query(1,1,n,1,n)];
        if(s.size()!=2)cout<<res<<"\n";
        else cout<<n<<"\n";
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int _=1;
    cin>>_;
    eular_shai(maxn-1);
    eular_num(maxn-1);
    while(_--)slove();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 6700kb

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: 3ms
memory: 7660kb

input:

1
1 1
2000000000
1 1999999999

output:

1
1

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 57ms
memory: 11336kb

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: