QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#710936#4898. 基础图论练习题wsc20080 0ms0kbC++145.3kb2024-11-04 23:17:512024-11-04 23:17:53

Judging History

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

  • [2024-11-04 23:17:53]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-11-04 23:17:51]
  • 提交

answer

#pragma GCC optimize(2,3,"Ofast","inline","unroll-loops")
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pii pair<ll,ll>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
bool Mbe;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void write(ll x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
const ll N=4e5+9,Mod=998244353;
ll n,m1,m2,D[N],X[N],U[N],V[N],W[N],fa[N],ans,rev[N];
vector<vector<ll> >vec[N];
array<ll,2> e[N];
struct DS{
    ll n;
    vector<ll>D;
    DS (ll _n=0): n(_n){}
    void Add(ll x){D.push_back(x);}
    pair<ll,vector<ll> >calc(vector<ll> v){
        priority_queue<pii,vector<pii>,greater<pii> >q;
        for(ll x:D)q.push({x,x});
        vector<ll>().swap(D);
        ll tn=n,cnt=0,lst=n,ofs=0;
        while(1){
            if(q.empty()){
                cnt+=tn;
                break;
            }
            ll d0=q.top().first+ofs,ord=q.top().second;
            q.pop();
            if(!d0)continue;
            D.push_back(ord);
            if(lst>d0){
                lst=d0;
                for(ll &x:v){
                    if(x<tn)x%=d0;
                }
            }
            if(q.empty()){
                cnt+=d0;
                break;
            }
            ll d1=q.top().first+ofs,k=d1/d0;
            tn-=k*d0,ofs-=k*d0;
            if(tn<d0)cnt+=d0-tn;
            if(d0<tn)q.push({d0-ofs,ord});
        }
        return {cnt,v};
    }
};
ll find(ll x){
    if(fa[x]<0)return x;
    return fa[x]=find(fa[x]);
}
bool merge(ll x,ll y){
    x=find(x),y=find(y);
    if(x==y)return 0;
    if(fa[x]>fa[y])swap(x,y);
    fa[x]+=fa[y],fa[y]=x;
    return 1;
}
bool Med;
int main(){
    freopen("mst.in","r",stdin);
    freopen("mst.out","w",stdout);
    cerr<<fabs(&Med-&Mbe)/1048576.0<<"MB\n";
    n=read(),m1=read(),m2=read();
    rep(i,0,m1-1)D[i]=read(),X[i]=read();
    rep(i,0,m2-1)U[i]=read(),V[i]=read(),W[i]=read();
    rep(i,0,m1-1)e[i]={X[i],i};
    rep(i,0,m2-1)e[i+m1]={W[i],i+m1};
    sort(e,e+m1+m2);
    vector<ll>ve;
    rep(i,0,m1+m2-1){
        if(e[i][1]<m1)rev[e[i][1]]=ve.size(),ve.push_back(e[i][1]);
    }
    ll compc=n;
    DS G(n);
    rep(i,0,m1+m2-1){
        if(e[i][1]>=m1)continue;
        G.Add(D[e[i][1]]);
        ll nw=G.calc({}).first;
        ans=(ans+e[i][0]*((compc-nw)%Mod))%Mod;
        compc=nw;
    }
    vector<ll>P;
    rep(i,0,m2-1)P.push_back(U[i]),P.push_back(V[i]);
    sort(P.begin(),P.end());
    P.resize(unique(P.begin(),P.end())-P.begin());
    vector<pair<pii,vector<ll> > >pv;
    if((ll)P.size()>=2){
        vector<ll>init(P.size());
        iota(init.begin(),init.end(),0ll);
        pv.push_back({{0,m1+1},init});
    }
    vector<ll>tr(P.size(),-1);
    while(1){
        bool fl=0;
        vector<vector<pair<pii,vector<ll> > > >tq(m1+1);
        for(pair<pii,vector<ll> >&pseg:pv){
            ll l=pseg.first.first,r=pseg.first.second;
            if(r-l==1)vec[l].push_back(pseg.second);
            else {
                fl=1;
                ll mid=(l+r)>>1;
                tq[mid].push_back(pseg);
            }
        }
        pv.clear();
        if(!fl)break;
        DS G(n);
        rep(i,0,m1){
            if(!tq[i].empty()){
                vector<ll>nfa;
                for(pair<pii,vector<ll> >&vc:tq[i])nfa.insert(nfa.end(),vc.second.begin(),vc.second.end());
                sort(nfa.begin(),nfa.end());
                nfa.resize(unique(nfa.begin(),nfa.end())-nfa.begin());
                vector<ll>tfa(nfa.size());
                rep(j,0,(ll)nfa.size()-1)tfa[j]=P[nfa[j]];
                vector<ll>ret=G.calc(tfa).second;
                rep(j,0,(ll)nfa.size()-1)tr[nfa[j]]=ret[j];
                for(pair<pii,vector<ll> >&vc:tq[i]){
                    ll l=vc.first.first,r=vc.first.second;
                    map<ll,vector<ll> >mp;
                    for(ll&x:vc.second)mp[tr[x]].push_back(x);
                    vector<ll>p;
                    for(pair<ll,vector<ll> >pi:mp){
                        if((ll)pi.second.size()>=2)pv.push_back({{l,i},pi.second});
                        p.push_back(pi.second[0]);
                    }
                    if((ll)p.size()>=2)pv.push_back({{i,r},p});
                }
            }
            if(i==m1)break;
            G.Add(D[ve[i]]);
        }
    }
    rep(i,0,m1-1){
        ll cnt=0;
        for(vector<ll>&v:vec[i])cnt+=((ll)v.size()-1);
        ans=(ans-X[ve[i]]%Mod*cnt%Mod+Mod)%Mod;
    }
    rep(i,0,(ll)P.size())fa[i]=-1;
    rep(i,0,m1+m2-1){
        if(e[i][1]<m1){
            ll cnt=0;
            for(vector<ll>&v:vec[rev[e[i][1]]]){
                rep(j,1,(ll)v.size()-1)cnt+=merge(v[0],v[j]);
            }
            ans=(ans+e[i][0]%Mod*cnt)%Mod;
        }
        else {
            ll x=lower_bound(P.begin(),P.end(),U[e[i][1]-m1])-P.begin();
            ll y=lower_bound(P.begin(),P.end(),V[e[i][1]-m1])-P.begin();
            if(merge(x,y))ans=(ans+e[i][0])%Mod;
        }
    }
    write(ans);
    cerr<<"\n"<<clock()*1.0/CLOCKS_PER_SEC*1000<<"ms\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Dangerous Syscalls

Test #1:

score: 0
Dangerous Syscalls

input:

161199 9 46510
147335 540442844
159493 801351455
149342 821625305
128476 843250712
95524 275754315
139315 106523502
93575 680460786
155498 328812257
146020 410466645
79992 141967 50596784
152210 68644 268349216
72549 96959 42994091
93869 27394 945120577
2909 81886 270684270
12735 35026 871917997
974...

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Dangerous Syscalls

Test #11:

score: 0
Dangerous Syscalls

input:

569435269457904707 2 0
490445920091092693 772271583
144842828305643603 609043885

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Dangerous Syscalls

Test #31:

score: 0
Dangerous Syscalls

input:

755526150476311190 942 0
492334667739348527 1
755523898623296976 1
532486636690994793 1
755526150476030559 1
755526150476249097 1
502164090270592200 1
657422656495814703 1
487200614853438190 1
311037325561173142 1
755526150475651155 1
125287404340238660 1
755524914808674090 1
755526150476177007 1
75...

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #3:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #5:

0%

Subtask #9:

score: 0
Skipped

Dependency #1:

0%