Logo Wy Online Judge

WyOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#205#7. 「WyOJ Round 1」归 · 星穗垂野__vector__1005394ms214220kbC++235.6kb2025-04-18 08:32:072025-04-18 20:49:15

answer

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(auto i=(a);i<=(b);i++)
#define REP(i,a,b) for(auto i=(a);i>=(b);i--)
#define FORK(i,a,b,k) for(auto i=(a);i<=(b);i+=(k))
#define REPK(i,a,b,k) for(auto i=(a);i>=(b);i-=(k))
#define pb push_back
#define mkpr make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
template<class T>
void ckmx(T& a,T b){
    a=max(a,b);
}
template<class T>
void ckmn(T& a,T b){
    a=min(a,b);
}
template<class T>
T gcd(T a,T b){
    return !b?a:gcd(b,a%b);
}
template<class T>
T lcm(T a,T b){
    return a/gcd(a,b)*b;
}
#define gc getchar()
#define eb emplace_back
#define pc putchar
#define ep empty()
#define fi first
#define se second
#define pln pc('\n');
#define islower(ch) (ch>='a'&&ch<='z')
#define isupper(ch) (ch>='A'&&ch<='Z')
#define isalpha(ch) (islower(ch)||isupper(ch))
template<class T>
void wrint(T x){
    if(x<0){
        x=-x;
        pc('-');
    }
    if(x>=10){
        wrint(x/10);
    }
    pc(x%10^48);
}
template<class T>
void wrintln(T x){
    wrint(x);
    pln
}
template<class T>
void read(T& x){
    x=0;
    int f=1;
    char ch=gc;
    while(!isdigit(ch)){
        if(ch=='-')f=-1;
        ch=gc;
    }
    while(isdigit(ch)){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=gc;
    }
    x*=f;
}
/*
主席树维护 i 结尾,最后一个区间长度 =j 的最小代价
ST 表维护区间 gcd
*/
const int maxn=1e5+5;
int n;
ll C;
int a[maxn];
ll b[maxn];
ll preb[maxn];
ll wb(int l,int r){
    return preb[r]-preb[l-1];
}
struct ST{
    int _gcd[18][maxn];
    void init(){
        FOR(i,1,n){
            _gcd[0][i]=a[i];
        }
        FOR(i,1,17){
            FOR(j,1,n-(1<<i)+1){
                _gcd[i][j]=gcd(_gcd[i-1][j],_gcd[i-1][j+(1<<i-1)]);
            }
        }
    }
    int qgcd(int l,int r){
        int _=__lg(r-l+1);
        return gcd(_gcd[_][l],_gcd[_][r-(1<<_)+1]);
    }
}st;
struct HJT{
    struct Node{
        int ls,rs;
        ll mn;
    }nd[32400005];
    // 1e5*(18^2)
    int ndcnt;
    void pushup(Node& x){
        x.mn=min(nd[x.ls].mn,nd[x.rs].mn);
    }
    void bd(int& rt,int l,int r){
        if(!rt)rt=++ndcnt;
        nd[rt].mn=1e18;
        if(l==r){
            return;
        }
        int mid=(l+r>>1);
        bd(nd[rt].ls,l,mid);
        bd(nd[rt].rs,mid+1,r);
    }
    void chg(int& rtold,int& rtnew,int l,int r,int x,ll _v){
        if(!rtnew){
            rtnew=++ndcnt;
            nd[rtnew].mn=nd[rtold].mn;
        }
        if(l==r){
            ckmn(nd[rtnew].mn,_v);
            return;
        }
        int mid=(l+r>>1);
        if(mid>=x){
            if(nd[rtnew].ls==nd[rtold].ls)nd[rtnew].ls=0;// 这个节点需要新建
            chg(nd[rtold].ls,nd[rtnew].ls,l,mid,x,_v);
        }
        else{
            if(nd[rtnew].rs==nd[rtold].rs)nd[rtnew].rs=0;// 同理
            chg(nd[rtold].rs,nd[rtnew].rs,mid+1,r,x,_v);
        }
        if(!nd[rtnew].ls)nd[rtnew].ls=nd[rtold].ls;
        if(!nd[rtnew].rs)nd[rtnew].rs=nd[rtold].rs;
        pushup(nd[rtnew]);
    }
    ll qmn(int rt,int curl,int curr,int l,int r){
        if(!rt)return 1e18;
        if(curl>=l&&curr<=r){
            return nd[rt].mn;
        }
        int mid=(curl+curr>>1);
        ll ans=1e18;
        if(mid>=l)ckmn(ans,qmn(nd[rt].ls,curl,mid,l,r));
        if(mid<r)ckmn(ans,qmn(nd[rt].rs,mid+1,curr,l,r));
        return ans;
    }
}hjt;
int rt[maxn];
void solve(int id_of_test){
	read(n);
    read(C);
	if(n==1&&C==114)puts("wtf");
    FOR(i,1,n){
        read(a[i]);
    }
    st.init();
    FOR(i,1,n){
        read(b[i]);
        preb[i]=preb[i-1]+b[i];
    }
    hjt.bd(rt[0],1,100000);
    FOR(r,1,n){
        rt[r]=++hjt.ndcnt;
        hjt.nd[rt[r]]=hjt.nd[rt[r-1]];
        int ok=0;
        int efl=1,efr=r;
        while(efl<=efr){
            int mid=(efl+efr>>1);
            if(r-mid+1>=st.qgcd(mid,r)){
                ok=mid;
                efl=mid+1;
            }else{
                efr=mid-1;
            }
        }
        if(!ok)continue;// 不存在合法的 l
        int l=ok;// 最右边的合法的 l
        int _gcd=st.qgcd(l,r);
      //  printf("r = %d\n",r);
        while(l>=1){
        //    printf("l %d _gcd = %d\n",l,_gcd);
            if(r-l+1>=_gcd){
                // 合法,计算答案
                
                ll sum=1ll*_gcd*wb(l,r);
                ll ans=sum;
              //  printf("[%d,%d] sum %d %lld\n",l,r,_gcd,sum);
                ckmn(ans,sum+C+hjt.qmn(rt[l-1],1,100000,1,_gcd));
         //       printf("[%d,%d] ans %lld\n",l,r,ans);
                hjt.chg(rt[r-1],rt[r],1,100000,r-l+1,ans);
          //      printf("[%d,%d] len = %d gcd = %d dp %lld\n",l,r,r-l+1,_gcd,ans);
                /*if(ans+C==-3037806949){
                    printf("[%d,%d] sum %lld\n",l,r,sum);
                }*/
            }
            // 目前 l 是本 gcd 同色块的右端点
            int efl=1,efr=l;
            int ok=l;
            while(efl<=efr){
                int mid=(efl+efr>>1);
                if(st.qgcd(mid,r)!=_gcd){
                    efl=mid+1;
                }else{
                    ok=mid;
                    efr=mid-1;
                }
            }
            // ok 就是本 gcd 同色块的左端点
          //  printf("ok %d\n",ok);
            l=ok-1;
            if(l)_gcd=st.qgcd(l,r);
        }
    }
    printf("%lld\n",hjt.qmn(rt[n],1,100000,1,100000)+C);
}
int main()
{
	int T;
	read(T);
	FOR(_,1,T){
		solve(_);
	}
	return 0;
}

这程序好像有点Bug,我给组数据试试?

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 569ms
memory: 11044kb

input:

1000
10 -759451753
8 10 5 7 5 10 6 10 2 1
5 6 5 4 9 8 5 4 1 3
10 -953033990
4 10 10 8 8 2 1 7 4 1
0 ...

output:

-2278355214
-2859101918
-21349075
-1203387136
-1290642033
-851981042
-648655823
-1699966984
-1493814...

result:

ok 1000 numbers

Test #2:

score: 10
Accepted
time: 236ms
memory: 99440kb

input:

5
94253 6998801
88374 94432 21568 79110 3746 94223 86214 12083 54743 1622 16110 18595 18994 3665 547...

output:

6999125
764376403
718240695
1381649585
885156744

result:

ok 5 number(s): "6999125 764376403 718240695 1381649585 885156744"

Test #3:

score: 10
Accepted
time: 9ms
memory: 16640kb

input:

5
120 -848255662
53358 95098 92852 36846 80052 83050 44071 22632 50005 52339 7339 38495 66618 6337 5...

output:

-11025209476
-3953017107
-5975377615
-2466586019
-5823678268

result:

ok 5 number(s): "-11025209476 -3953017107 -5975377615 -2466586019 -5823678268"

Test #4:

score: 10
Accepted
time: 11ms
memory: 16572kb

input:

5
100 -964452256
94167 36596 60051 82090 55625 56197 94589 29844 14929 68497 81714 90264 66283 96767...

output:

-7714370390
-19569375783
-554464800
-1059581333
-6827008162

result:

ok 5 number(s): "-7714370390 -19569375783 -554464800 -1059581333 -6827008162"

Test #5:

score: 10
Accepted
time: 14ms
memory: 17188kb

input:

5
984 -715414516
65632 15191 55392 53178 88695 20308 44882 16528 12986 81169 76880 2239 81264 13866 ...

output:

-72952473687
-64021627506
-700730085
-994178012
-1624804963

result:

ok 5 number(s): "-72952473687 -64021627506 -700730085 -994178012 -1624804963"

Test #6:

score: 10
Accepted
time: 886ms
memory: 195448kb

input:

5
83593 -175988991
46605 26358 5204 30137 41225 11710 75224 14130 56433 62524 29873 59483 22318 2619...

output:

-1656361289384
-4920133073293
-1028097824
-2809116480
-22668175189

result:

ok 5 number(s): "-1656361289384 -4920133073293 ...097824 -2809116480 -22668175189"

Test #7:

score: 10
Accepted
time: 929ms
memory: 213880kb

input:

5
96302 -451340376
2672 47391 49737 61648 38099 10679 28316 91168 97182 40556 20560 89630 45931 9187...

output:

-4877361987440
-9327808595024
-261498007
97842263
-37427524204

result:

ok 5 number(s): "-4877361987440 -9327808595024 -261498007 97842263 -37427524204"

Test #8:

score: 10
Accepted
time: 981ms
memory: 214220kb

input:

5
82075 -915749780
41503 94470 21657 60519 10090 65640 80911 29436 63096 68715 79609 87499 26240 606...

output:

-8500967499879
-2162204721519
-803472133
-6968294203
-60694304098

result:

ok 5 number(s): "-8500967499879 -2162204721519 ...472133 -6968294203 -60694304098"

Test #9:

score: 10
Accepted
time: 876ms
memory: 195520kb

input:

5
86206 -158351237
12910 53574 97622 75378 61116 40492 87106 60678 72614 2146 43673 15147 23123 7469...

output:

-1508037508585
-4714642449519
-3202809425
-6778545312
-12974512083

result:

ok 5 number(s): "-1508037508585 -4714642449519 ...809425 -6778545312 -12974512083"

Test #10:

score: 10
Accepted
time: 883ms
memory: 195516kb

input:

5
87683 -236086252
88768 76878 54808 98282 70961 83599 48353 34381 40413 95288 24241 74500 52966 994...

output:

-2319782085288
-10990638538852
-1651187059
-12428157994
-22719490815

result:

ok 5 number(s): "-2319782085288 -10990638538852...87059 -12428157994 -22719490815"

Extra Test:

score: 0
Extra Test Passed