QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#221000#7587. Road ManagerPhantomThresholdTL 11ms3940kbC++203.6kb2023-10-21 01:42:542023-10-21 01:42:55

Judging History

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

  • [2023-10-21 01:42:55]
  • 评测
  • 测评结果:TL
  • 用时:11ms
  • 内存:3940kb
  • [2023-10-21 01:42:54]
  • 提交

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);
	vector<map<int,int>> G(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);
			G[u].clear();G[v].clear();
		}
		sort(sp.begin(),sp.end());
		sp.resize(unique(sp.begin(),sp.end())-sp.begin());
//		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]++;
				G[u][v]=w;
				G[v][u]=w;
			}
		}
		for(auto u:sp)
		{
			if(not mark[u] and deg[u]==2)
			{
				auto it1=G[u].begin();
				auto it2=next(it1);
				int v1=it1->first,v2=it2->first;
				int w=max(it1->second,it2->second);
				ex+=min(it1->second,it2->second);
				G[u].clear();
				G[v1].erase(u);G[v2].erase(u);
				G[v1][v2]=G[v2][v1]=w;
				deg[u]=0;
			}
			else if(not mark[u] and deg[u]==1)
			{
				auto it1=G[u].begin();
				int v=it1->first;
				ex+=it1->second;
				G[u].clear();
				G[v].erase(u);
				deg[v]--;
				deg[u]=0;
			}
		}
		vector<tuple<int,int,int>> ret;
		for(auto u:sp)
		{
			for(const auto &[v,w]:G[u])
			{
				if(u<v)
				{
					ret.emplace_back(u,v,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++)
	{
//		if(j%100==0)cerr<<"pre "<<j<<endl;
		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;
//		if(j%100==0)cerr<<"edge# "<<pre[j].first.size()<<endl;
	}
	suf[m]=make_pair(edgeV[m],0);
	for(int j=m-1;j>1;j--)
	{
//		if(j%100==0)cerr<<"suf "<<j<<endl;
		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: 1ms
memory: 3508kb

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: 11ms
memory: 3940kb

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...

output:

1211623
682743
1361117
283679
1533910
1058719
1526089
1457348
1224581
844519
1068307
1850478
1441600
1262311
1533505
393355
533670
1750612
1431335
1079803
1078983
1572722
1403657
1554603
1871623
1742851
796103
1436810
1618909
1627701
824187
1689150
1552343
1846414
1756497
505159
1260003
174270
83303...

result: