QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#220988 | #7587. Road Manager | PhantomThreshold | TL | 63ms | 4708kb | C++20 | 3.2kb | 2023-10-21 01:17:50 | 2023-10-21 01:17:51 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
template<typename T>
pair<vector<T>,long long> operator+(const pair<vector<T>,long long> &a,const pair<vector<T>,long long> &b)
{
auto c=a.first;
c.insert(c.end(),b.first.begin(),b.first.end());
return make_pair(c,a.second+b.second);
}
int main()
{
ios_base::sync_with_stdio(false);
unsigned SA,SB,SC;int lim;
auto getweight=[&]()
{
SA ^= SA << 16;
SA ^= SA >> 5;
SA ^= SA << 1;
unsigned t = SA;
SA = SB;
SB = SC;
SC ^= t ^ SA;
return SC % lim + 1;
};
int n,m;
cin>>n>>m>>SA>>SB>>SC>>lim;
auto hs=[&](int x,int y){return (x-1)*m+y;};
vector<vector<tuple<int,int,int>>> edgeH(m+5),edgeV(m+5);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
int w=getweight();
if(j<m)edgeH[j].emplace_back(hs(i,j),hs(i,j+1),w);
else edgeH[j].emplace_back(hs(i,j),hs(i,1),w);
}
for(int i=1;i<n;i++)
for(int j=1;j<=m;j++)
{
int w=getweight();
edgeV[j].emplace_back(hs(i,j),hs(i+1,j),w);
}
vector<int> mark(n*m+5),pa(n*m+5),deg(n*m+5);
function<int(int)> find=[&](int x){return pa[x]?pa[x]=find(pa[x]):x;};
auto mst=[&](const auto &info)
{
auto edges=info.first;
long long ex=info.second;
sort(edges.begin(),edges.end(),[&](auto x,auto y){return get<2>(x)<get<2>(y);});
vector<int> sp;
for(auto [u,v,w]:edges)pa[u]=pa[v]=deg[u]=deg[v]=0,sp.push_back(u),sp.push_back(v);
sort(sp.begin(),sp.end());
sp.resize(unique(sp.begin(),sp.end())-sp.begin());
map<pair<int,int>,int> mp;
// cerr<<"calling mst, ex = "<<ex<<endl;
for(auto [u,v,w]:edges)
{
// cerr<<"edge "<<u<<' '<<v<<' '<<w<<endl;
int pu=find(u),pv=find(v);
if(pu!=pv)
{
pa[pv]=pu;
deg[u]++;deg[v]++;
mp[{u,v}]=w;mp[{v,u}]=w;
}
}
for(auto u:sp)
{
if(not mark[u] and deg[u]==2)
{
auto it1=mp.lower_bound({u,0});
auto it2=next(it1);
int v1=it1->first.second,v2=it2->first.second;
int w=max(it1->second,it2->second);
ex+=min(it1->second,it2->second);
mp.erase({u,v1});mp.erase({v1,u});
mp.erase({u,v2});mp.erase({v2,u});
mp[{v1,v2}]=mp[{v2,v1}]=w;
deg[u]=0;
}
}
vector<tuple<int,int,int>> ret;
for(const auto &[pr,w]:mp)
{
if(pr.first<pr.second)
{
ret.emplace_back(pr.first,pr.second,w);
// cerr<<"return edge "<<pr.first<<' '<<pr.second<<' '<<w<<endl;
}
}
// cerr<<"return ex = "<<ex<<endl;
return make_pair(ret,ex);
};
vector<pair<vector<tuple<int,int,int>>,long long>> pre(m+5),suf(m+5);
for(int i=1;i<=n;i++)mark[hs(i,m)]=1;
for(int j=1;j<m;j++)
{
for(int i=1;i<=n;i++)mark[hs(i,j)]=1;
if(j==1)pre[j]=mst(make_pair(edgeV[1],0ll)+make_pair(edgeV[m],0ll)+make_pair(edgeH[m],0ll));
else pre[j]=mst(pre[j-1]+make_pair(edgeV[j],0ll)+make_pair(edgeH[j-1],0ll));
for(int i=1;i<=n;i++)mark[hs(i,j)]=0;
}
suf[m]=make_pair(edgeV[m],0);
for(int j=m-1;j>1;j--)
{
for(int i=1;i<=n;i++)mark[hs(i,j)]=1;
suf[j]=mst(suf[j+1]+make_pair(edgeV[j],0ll)+make_pair(edgeH[j],0ll));
for(int i=1;i<=n;i++)mark[hs(i,j)]=0;
}
int q;
cin>>q;
while(q--)
{
int l,r;
cin>>l>>r;
auto info=mst(pre[l-1]+suf[r+1]);
long long ans=info.second;
for(auto z:info.first)ans+=get<2>(z);
cout<<ans<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3832kb
input:
2 4 1 2 3 5 3 2 2 2 3 3 3
output:
9 5 13
result:
ok 3 number(s): "9 5 13"
Test #2:
score: 0
Accepted
time: 63ms
memory: 4708kb
input:
50 50 858397672 21575781 421714252 50 50 10 30 25 41 10 44 41 45 47 47 39 42 20 38 28 47 12 15 26 32 9 25 18 26 7 15 5 8 7 31 8 37 41 42 18 21 32 32 14 35 6 22 12 26 42 45 9 23 14 14 5 6 8 24 25 43 37 38 15 46 26 43 35 48 22 30 20 45 21 49 11 12 6 46 15 45 21 43 4 40 4 18 28 37 32 38 18 26 32 32 10 ...
output:
21046 24218 11414 32468 35179 33298 22126 22012 33300 30838 23977 29055 29918 33069 18185 14871 34615 32954 35312 20094 24226 25181 33130 25535 35374 34492 24067 22758 34675 13257 23443 26215 29241 17292 15165 34743 7157 13953 19672 9730 25603 29117 31267 29055 35312 8400 2536 14565 27209 33993
result:
ok 50 numbers
Test #3:
score: -100
Time Limit Exceeded
input:
100 10000 753738892 852022308 109208427 5 10000 6068 9618 3347 9710 3237 5987 428 8918 183 2017 3654 8017 6218 8094 1812 4054 783 4265 638 6139 1780 6091 1559 1706 6627 8952 2958 6234 6213 8049 1860 9764 988 8148 4992 5669 7214 9591 4245 8494 5417 9671 2840 4466 5799 8328 5436 7160 1136 1170 3134 38...