QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#176106#7182. Very Sparse TableCrysflyAC ✓862ms98832kbC++174.5kb2023-09-11 10:53:042023-09-11 10:53:05

Judging History

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

  • [2023-09-11 10:53:05]
  • 评测
  • 测评结果:AC
  • 用时:862ms
  • 内存:98832kb
  • [2023-09-11 10:53:04]
  • 提交

answer

// what is matter? never mind. 
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2") 
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define int long long
#define ull unsigned long long
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	if(f)x=-x;return x;
}

int mod=1000000007;
struct modint{
	int x;
	modint(int o=0){x=o;}
	modint &operator = (int o){return x=o,*this;}
	modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
	modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	modint &operator ^=(int b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint &operator /=(modint o){return *this *=o^=mod-2;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	friend modint operator ^(modint a,int b){return a^=b;}
	friend bool operator ==(modint a,int b){return a.x==b;}
	friend bool operator !=(modint a,int b){return a.x!=b;}
	bool operator ! () {return !x;}
	modint operator - () {return x?mod-x:0;}
	bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}

vector<modint> fac,ifac,iv;
inline void initC(int n)
{
	if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
	int m=iv.size(); ++n;
	if(m>=n)return;
	iv.resize(n),fac.resize(n),ifac.resize(n);
	For(i,m,n-1){
		iv[i]=iv[mod%i]*(mod-mod/i);
		fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
	}
}
inline modint C(int n,int m){
	if(m<0||n<m)return 0;
	return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 200005
#define inf 0x3f3f3f3f

int n,B;

int anc[maxn][20],tot;
vi buc[maxn];
map<int,int>mp[maxn];

vector<array<int,3>>out;
void add(int u,int mid,int v){
	assert(mp[u][mid]);
	assert(mp[mid][v]);
	if(!mp[u][v]) mp[u][v]=1,out.pb({u,mid,v});
}

void div(vi p,int D)
{
	int n=p.size();
	assert(D<=18);
	assert(tot<=200000);
	if(n<=4)return;
	int id=++tot;
	for(int x:p) anc[x][D]=id;
	int B=sqrt(n-1);
	int l=0,r=0;
	
	vi pos;
	for(r=B-1;r<n-1;l=r,r+=B){
//		if(p.size()==101)cout<<"add "<<l<<" "<<r<<"\n";
		pos.pb(p[r]);
		Rep(i,r-2,l) add(p[i],p[i+1],p[r]);
		For(i,l+2,r) add(p[l],p[i-1],p[i]);
		vi tmp;
		For(i,l+1,r-1) tmp.pb(p[i]);
		div(tmp,D+1);
	}
	
	if(l<n-1){
		r=n-1;
//		if(p.size()==101)cout<<"add "<<l<<" "<<r<<"\n";
		Rep(i,r-2,l) add(p[i],p[i+1],p[r]);
		For(i,l+2,r) add(p[l],p[i-1],p[i]);
		vi tmp;
		For(i,l+1,r-1) tmp.pb(p[i]);
		div(tmp,D+1);
	}
	
	int sz=pos.size();
	For(i,0,sz-1)
		For(j,i+2,sz-1)
			add(pos[i],pos[j-1],pos[j]);
	
	buc[id]=pos;
}

signed main()
{
//	freopen("data.in","r",stdin);
//	freopen("my.out","w",stdout);
	cin>>n;
	tot=0;
	memset(anc,-1,sizeof anc);
	vi o;
	For(i,0,n-1)mp[i][i+1]=1;
	For(i,0,n)o.pb(i);
	div(o,0);
//	cout<<tot<<"\n";
	assert(out.size()<=n*6);
	cout<<out.size()<<endl;
	for(auto [u,mid,v]:out)cout<<u<<" "<<mid<<" "<<v<<endl;
	int Q;cin>>Q;
	while(Q--){
		int u,v;cin>>u>>v;
		if(v-u<=3){
			For(i,u,v)cout<<i<<" ";
			cout<<endl;
			continue;
		}
		if(mp[u][v]){
			cout<<u<<" "<<v<<endl;
			continue;
		}
		int d=0;
		while(anc[u][d+1]!=-1 && anc[v][d+1]!=-1 && anc[u][d+1]==anc[v][d+1]) 
			++d;
		
	//	cout<<"lca "<<d<<" "<<anc[u][d]<<" "<<anc[v][d]<<'\n';
		int lca=anc[u][d];
		auto&pos=buc[lca];
//		for(auto it:pos)cout<<it<<" ";cout<<"\n";
		vi ans; ans.pb(u);
		auto it=lower_bound(pos.begin(),pos.end(),u);
		if(u!=(*it)) ans.pb(*it);
//		cout<<"*it "<<*it<<"\n";
		auto it2=upper_bound(pos.begin(),pos.end(),v); --it2;
//		cout<<"*it2 "<<*it2<<"\n";
		if((*it)!=(*it2)) ans.pb(*it2);
		if((*it2)!=v) ans.pb(v);
		for(int x:ans)cout<<x<<" ";
		cout<<endl;
		
//		int siz=ans.size();
//		For(i,0,siz-2) if(mp[ans[i]][ans[i+1]]); else{
//			cerr<<"Fail "<<u<<" "<<v<<"\n";
//			exit(0);
//		}
	}
	return 0;
}
/*
100
1 0 4
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 48904kb

input:

9
45
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
2 3
2 4
2 5
2 6
2 7
2 8
2 9
3 4
3 5
3 6
3 7
3 8
3 9
4 5
4 6
4 7
4 8
4 9
5 6
5 7
5 8
5 9
6 7
6 8
6 9
7 8
7 9
8 9

output:

8
0 1 2
3 4 5
2 3 5
2 3 4
6 7 8
5 6 8
5 6 7
2 5 8
0 1 
0 1 2 
0 1 2 3 
0 2 4 
0 2 5 
0 2 5 6 
0 2 5 7 
0 2 8 
0 2 8 9 
1 2 
1 2 3 
1 2 3 4 
1 2 5 
1 2 5 6 
1 2 5 7 
1 2 8 
1 2 8 9 
2 3 
2 3 4 
2 3 4 5 
2 5 6 
2 5 7 
2 8
2 8 9 
3 4 
3 4 5 
3 4 5 6 
3 5 7 
3 5 8 
3 5 8 9 
4 5 
4 5 6 
4 5 6 7 
4 5 8 
4...

result:

ok edges: 8

Test #2:

score: 0
Accepted
time: 4ms
memory: 48956kb

input:

30
465
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9
0 10
0 11
0 12
0 13
0 14
0 15
0 16
0 17
0 18
0 19
0 20
0 21
0 22
0 23
0 24
0 25
0 26
0 27
0 28
0 29
0 30
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
2 3
2 4
2 5
2 6...

output:

50
2 3 4
1 2 4
0 1 4
0 1 2
0 2 3
7 8 9
6 7 9
5 6 9
4 5 9
4 5 6
4 6 7
4 7 8
12 13 14
11 12 14
10 11 14
9 10 14
9 10 11
9 11 12
9 12 13
17 18 19
16 17 19
15 16 19
14 15 19
14 15 16
14 16 17
14 17 18
22 23 24
21 22 24
20 21 24
19 20 24
19 20 21
19 21 22
19 22 23
27 28 29
26 27 29
25 26 29
24 25 29
24 2...

result:

ok edges: 50

Test #3:

score: 0
Accepted
time: 437ms
memory: 57888kb

input:

736
200000
170 268
126 166
565 723
664 735
61 524
226 234
146 314
217 272
294 713
115 381
563 706
74 567
552 614
120 211
472 620
213 432
488 623
447 564
96 129
331 354
79 677
50 547
174 568
56 129
189 227
55 701
244 253
264 715
154 220
380 657
46 390
53 161
325 537
666 696
64 465
391 659
284 448
207...

output:

2768
24 25 26
23 24 26
22 23 26
21 22 26
20 21 26
19 20 26
18 19 26
17 18 26
16 17 26
15 16 26
14 15 26
13 14 26
12 13 26
11 12 26
10 11 26
9 10 26
8 9 26
7 8 26
6 7 26
5 6 26
4 5 26
3 4 26
2 3 26
1 2 26
0 1 26
0 1 2
0 2 3
0 3 4
0 4 5
0 5 6
0 6 7
0 7 8
0 8 9
0 9 10
0 10 11
0 11 12
0 12 13
0 13 14
0 ...

result:

ok edges: 2768

Test #4:

score: 0
Accepted
time: 862ms
memory: 97520kb

input:

65536
200000
51949 58727
7943 43298
6290 7369
41493 53070
24229 36675
28087 49947
11703 48217
19923 24739
2144 59777
53830 56793
13509 37211
2300 38595
27415 42879
24616 48531
58341 63327
20628 38407
48616 60290
7450 61685
37010 47595
22164 42732
19181 29850
35383 43587
39257 44397
19340 45183
34523...

output:

367228
253 254 255
252 253 255
251 252 255
250 251 255
249 250 255
248 249 255
247 248 255
246 247 255
245 246 255
244 245 255
243 244 255
242 243 255
241 242 255
240 241 255
239 240 255
238 239 255
237 238 255
236 237 255
235 236 255
234 235 255
233 234 255
232 233 255
231 232 255
230 231 255
229 2...

result:

ok edges: 367228

Test #5:

score: 0
Accepted
time: 6ms
memory: 48840kb

input:

0
0

output:

0

result:

ok edges: 0

Test #6:

score: 0
Accepted
time: 0ms
memory: 48940kb

input:

1
1
0 1

output:

0
0 1 

result:

ok edges: 0

Test #7:

score: 0
Accepted
time: 3ms
memory: 48872kb

input:

2
3
0 1
0 2
1 2

output:

0
0 1 
0 1 2 
1 2 

result:

ok edges: 0

Test #8:

score: 0
Accepted
time: 7ms
memory: 48932kb

input:

3
6
0 1
0 2
0 3
1 2
1 3
2 3

output:

0
0 1 
0 1 2 
0 1 2 3 
1 2 
1 2 3 
2 3 

result:

ok edges: 0

Test #9:

score: 0
Accepted
time: 794ms
memory: 98832kb

input:

65535
200000
35006 46944
17075 57351
24605 50445
5938 60705
15221 40233
28599 38915
1132 35574
8555 31494
13644 35806
44940 55401
9503 59206
21011 26540
41156 62487
57510 64305
9254 25610
17301 47249
34083 49167
48018 64394
38855 62175
15464 22525
23728 60275
54028 63810
22711 53902
5984 48625
5838 ...

output:

367505
252 253 254
251 252 254
250 251 254
249 250 254
248 249 254
247 248 254
246 247 254
245 246 254
244 245 254
243 244 254
242 243 254
241 242 254
240 241 254
239 240 254
238 239 254
237 238 254
236 237 254
235 236 254
234 235 254
233 234 254
232 233 254
231 232 254
230 231 254
229 230 254
228 2...

result:

ok edges: 367505

Test #10:

score: 0
Accepted
time: 827ms
memory: 98260kb

input:

64800
200000
55124 62263
24992 39760
32262 37059
25987 42889
10413 64701
7223 43221
45810 63205
11437 29357
10814 52096
1154 36319
10730 54157
18473 26729
9152 23374
5426 12744
3502 37577
5559 37160
30503 62433
12426 47332
14933 62086
8781 21527
27180 53773
29658 46742
20592 61553
8337 27197
8024 38...

output:

362965
251 252 253
250 251 253
249 250 253
248 249 253
247 248 253
246 247 253
245 246 253
244 245 253
243 244 253
242 243 253
241 242 253
240 241 253
239 240 253
238 239 253
237 238 253
236 237 253
235 236 253
234 235 253
233 234 253
232 233 253
231 232 253
230 231 253
229 230 253
228 229 253
227 2...

result:

ok edges: 362965

Extra Test:

score: 0
Extra Test Passed