QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#726881 | #9543. Good Partitions | Cabbage | TL | 170ms | 9304kb | C++14 | 3.4kb | 2024-11-09 09:55:11 | 2024-11-09 09:55:13 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+5;
int nums[maxn];
int v[maxn];
int yin[maxn];
inline int gcd(int a,int b){
return !b?a:gcd(b,a%b);
}
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(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);
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);
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()){
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);
return 1;
}
return 0;
}
};
int res=0;
if(s.size()!=2)res=yin[T.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=yin[T.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>>_;
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;
};
for(int i=1;i<maxn;i++)yin[i]=ans(i);
while(_--)slove();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 112ms
memory: 5684kb
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: 108ms
memory: 5776kb
input:
1 1 1 2000000000 1 1999999999
output:
1 1
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 170ms
memory: 9304kb
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...