QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#221020#7587. Road ManagerPhantomThresholdAC ✓2750ms176984kbC++203.7kb2023-10-21 02:08:342023-10-21 02:08:35

Judging History

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

  • [2023-10-21 02:08:35]
  • 评测
  • 测评结果:AC
  • 用时:2750ms
  • 内存:176984kb
  • [2023-10-21 02:08:34]
  • 提交

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);cin.tie(0);
	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+(y-1)*n;};
	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);
	vector<vector<pair<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(),[&](const auto &x,const auto &y){return get<2>(x)<get<2>(y);});
		vector<int> sp;
		for(auto [u,v,w]:edges)
		{
			pa[u]=pa[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;
				G[u].emplace_back(v,w);
				G[v].emplace_back(u,w);
			}
		}
		for(auto u:sp)
		{
			if(not mark[u] and G[u].size()==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();
				for(auto it=G[v1].begin();it!=G[v1].end();++it){if(it->first==u){it->first=v2;it->second=w;break;}}
				for(auto it=G[v2].begin();it!=G[v2].end();++it){if(it->first==u){it->first=v1;it->second=w;break;}}
			}
			else if(not mark[u] and G[u].size()==1)
			{
				auto it1=G[u].begin();
				int v=it1->first;
				ex+=it1->second;
				G[u].clear();
				for(auto it=G[v].begin();it!=G[v].end();++it){if(it->first==u){G[v].erase(it);break;}}
			}
		}
		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++)
	{
		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;
//	mt19937 rng(58);
	while(q--)
	{
		int l,r;
//		l=rng()%(m-2)+2;r=rng()%(m-2)+2;
//		if(l>r)swap(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<<"\n";
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3600kb

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

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: 0
Accepted
time: 2359ms
memory: 176256kb

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:

ok 10000 numbers

Test #4:

score: 0
Accepted
time: 2372ms
memory: 176616kb

input:

100 10000 252296658 470686114 738681417 5
10000
19 9941
4573 8431
1956 8010
6495 7523
1584 9576
3805 7368
945 8568
753 6106
3261 6017
4137 4690
8260 8284
3732 7371
2222 9531
174 6288
4011 4548
7675 8100
787 5858
3504 4912
5358 9086
492 8450
4746 5765
2833 5471
6990 8945
4020 8049
2168 5355
4144 7941...

output:

14776
1154093
741074
1685175
377030
1209281
446018
873015
1360987
1773494
1873502
1195181
505818
729771
1776734
1797785
925822
1613267
1178272
383399
1687330
1382812
1510227
1121331
1279314
1165058
781005
726730
964577
989349
1175799
1824181
1485743
1279135
1225339
594598
1482362
1727286
655503
1852...

result:

ok 10000 numbers

Test #5:

score: 0
Accepted
time: 2750ms
memory: 176140kb

input:

100 10000 784936363 827067061 800454511 1000000000
10000
2056 2962
5233 5350
1346 2099
8529 8825
2358 3371
3217 6419
1175 9635
2998 7699
1494 9244
2710 5459
1647 8372
6360 8398
1256 9955
1252 1473
4171 4704
4867 7373
3208 7839
2949 8996
4731 7531
8915 9536
2199 5546
2265 6448
1463 9131
2358 2383
625...

output:

218107916833770
236956297236591
221611594649234
232654372445039
215511117763537
163015473624889
37013079700783
127182022450125
54041351200568
173843674045542
78674875729514
191084774917088
31305721956998
234464326245872
227032500475120
179858820611878
128837146174236
94963670830748
172797399335623
2...

result:

ok 10000 numbers

Test #6:

score: 0
Accepted
time: 2684ms
memory: 176132kb

input:

100 10000 72129929 485897764 129463885 1000000000
10000
383 7372
2861 4738
5799 7101
3570 9122
3175 4173
1444 7532
6191 9348
3164 6316
2122 4663
1589 2869
4784 7741
3529 5974
4792 7378
6016 8170
864 1289
1647 7570
293 1602
1054 8521
6193 9882
4543 5121
956 5006
1858 7086
4117 7517
9736 9847
7917 865...

output:

72090982299191
194600560760132
208326087759066
106457378837533
215695913806908
93645900885474
163942628341738
164031689768437
178662309764579
208841353815076
168610752734842
180880159627225
177470699352697
187964294311616
229294854382327
97557084520879
208227727138432
60656821171511
151175099594593
...

result:

ok 10000 numbers

Test #7:

score: 0
Accepted
time: 2687ms
memory: 176984kb

input:

100 10000 963446001 261040754 78671748 1000000000
10000
1308 8621
2948 6287
2647 3069
103 5392
3204 9085
7669 8375
2823 5662
6455 7017
530 4194
567 9892
2436 9869
3149 6625
3015 7571
1051 1155
30 5077
1138 9673
8598 9184
5453 8895
902 7796
2749 5653
334 2677
4818 7123
2693 8574
1945 2913
6278 7799
6...

output:

64264200768233
159631014825609
229412046850098
112894812377726
98703295232189
222675254882181
171598041130418
226137587624448
151655725634932
16103717296049
61433187417017
156336180363989
130459314798835
237120775693771
118682572200864
34995983081351
225547690108471
157054836420411
74239996440524
17...

result:

ok 10000 numbers

Test #8:

score: 0
Accepted
time: 2691ms
memory: 176892kb

input:

100 10000 64237141 422265017 577403465 1000000000
10000
1335 3887
3017 4150
13 5940
619 9252
2124 8016
6189 8581
3524 6793
5826 9716
880 1859
6383 6421
1312 3139
4259 6148
1005 2744
1353 8445
6463 9223
6923 9215
901 1803
1202 1820
1053 6563
3677 7206
7691 9391
4533 8068
4360 7698
4769 8394
1629 5350...

output:

178476322146618
212581735474540
97664015826725
32720953154164
98586242468815
182463260674934
161421747872055
146457120526990
216189917671016
238821446345616
195878926340178
194435416584804
198022374418219
69770884358102
173557137328055
184789377313284
218050741659725
224873943256047
107596574583740
...

result:

ok 10000 numbers