Submission #884902

#TimeUsernameProblemLanguageResultExecution timeMemory
884902amirhoseinfar1385Reconstruction Project (JOI22_reconstruction)C++17
100 / 100
426 ms20404 KiB
#include<bits/stdc++.h> using namespace std; const int maxn=505,maxm=100000+10; struct yal{ int u,v,w,ind; bool operator <(const yal a)const{ if(a.w!=w){ return w<a.w; } if(a.u!=u){ return u<a.u; } return v<a.v; } }; struct updy{ int l,val,ted; updy(int a=0,int b=0,int c=0){ l=a; val=b; ted=c; } bool operator <(const updy a)const{ if(a.l!=l){ return l<a.l; } if(a.val!=val){ return val<a.val; } return ted<a.ted; } }fupdy; struct dsu{ short par[maxn],sz[maxn]; void clear(){ memset(par,0,sizeof(par)); memset(sz,0,sizeof(sz)); } short root(short u){ if(par[u]==0){ return u; } return par[u]=root(par[u]); } bool con(short u,short v){ u=root(u),v=root(v); if(u!=v){ if(sz[u]<sz[v]){ swap(u,v); } par[v]=u; sz[u]+=sz[v]+1; return 1; } return 0; } }ds; vector<updy>allq; vector<yal>alle,v; int n,m; int upd(){ ds.clear(); for(int i=(int)v.size()-1;i>=0;i--){ // cout<<"ha: "<<v[i].u<<" "<<v[i].v<<" "<<v[i].ind<<"\n"; if(ds.con(v[i].u,v[i].v)==0){ // cout<<"nababa"<<endl; int ret=v[i].ind; v.erase(v.begin()+i); return ret; } } return -1; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m; alle.resize(m); for(int i=0;i<m;i++){ cin>>alle[i].u>>alle[i].v>>alle[i].w; allq.push_back(updy(alle[i].w,-2*alle[i].w,2)); } sort(alle.begin(),alle.end()); for(int i=0;i<m;i++){ alle[i].ind=i; } for(int i=0;i<m;i++){ v.push_back(alle[i]); int ind=upd(); if(ind==-1){ allq.push_back(updy(-1,alle[i].w,-1)); } else{ int rl=(alle[i].w+alle[ind].w+1)/2; // cout<<i<<" "<<ind<<" "<<alle[i].w<<" "<<alle[ind].w<<" wtf"<<endl; allq.push_back(updy(rl,alle[i].w,-1)); allq.push_back(updy(rl,alle[ind].w,-1)); } } sort(allq.rbegin(),allq.rend()); int q; cin>>q; long long res=0,ted=0; for(int i=0;i<q;i++){ int d; cin>>d; while((int)allq.size()>0&&allq.back().l<=d){ auto x=allq.back(); allq.pop_back(); res+=x.val; ted+=x.ted; //cout<<"outy: "<<x.val<<" "<<x.ted<<" "<<x.l<<" "<<d<<"\n"; } long long mainres=res+d*ted; cout<<mainres<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...