QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#745263#8547. Whose Land?scallionsongWA 486ms10736kbC++144.2kb2024-11-14 08:50:192024-11-14 08:50:20

Judging History

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

  • [2024-11-14 08:50:20]
  • 评测
  • 测评结果:WA
  • 用时:486ms
  • 内存:10736kb
  • [2024-11-14 08:50:19]
  • 提交

answer

bool M1;
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ull unsigned long long
#define LL __int128
#define db double
#define LD long double
#define Pii pair<int,int>
#define Pll pair<ll,ll>
#define Pull pair<ull,ull>
#define Pdb pair<db,db>
#define fir first
#define sec second
#define vec vector<int>
#define pb push_back
#define qlr cerr<<"qlr\n"
#define dyh cerr<<"dyh\n"
#define pc(x) __builtin_popcount(x)
#define uni(x,y) uniform_int_distribution<int>(x,y)(rng)
#define unl(x,y) uniform_int_distribution<ll>(x,y)(rng)
#define unr(x,y) uniform_real_distribution<double>(x,y)(rng)
#define F(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define UF(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define look_memory cerr<<'\n'<<abs(&M1-&M2)/1024.0/1024<<'\n'
#define look_time cerr<<'\n'<<clock()*1.0/CLOCKS_PER_SEC<<'\n'
mt19937 rng(time(0)^(*new int));
const ll INF=0x3f3f3f3f3f3f3f3f;
const int Mod=998244353;
template<typename T>
inline void inc(T &a,T b){
	if(b<0) b+=Mod;
	a+=b;
	if(a>=Mod) a-=Mod;
}
template<typename T>
inline void dec(T &a,T b){
	if(b<0) b+=Mod;
	a-=b;
	if(a<0) a+=Mod;
}
template<typename T>
inline void muc(T &a,T b){
	a=a*b%Mod;
}
template<typename T>
inline bool chkmin(T &a,T b){
	if(a<=b) return false;
	a=b;
	return true;
}
template<typename T>
inline bool chkmax(T &a,T b){
	if(a>=b) return false;
	a=b;
	return true;
}
int T,n,K,Q;
int bfn[100010],fth[100010],ans[100010];
Pii t[100010][21];
vec e[100010];
vector<Pii> qry[100010];
queue<int> q;
struct Bit{
	#define N 100000
	int w[N+10];
	int lb(int x){
		return x&-x;
	}
	void clear(){
		F(i,1,N) w[i]=0;
	}
	void add(int x,int y){
		if(!x) return;
		for(int i=x;i<=N;i+=lb(i)) w[i]+=y;
	}
	int query(int x){
		int res=0;
		for(int i=x;i;i-=lb(i)) res+=w[i];
		return res;
	}
	int Query(int x,int y){
		return query(y)-query(x-1);
	}
	#undef N
}bit;
struct ODT{
	int l,r,val;
};
bool operator<(ODT cm1,ODT cm2){
	return cm1.l<cm2.l;
}
struct Odt{
	#define IT set<ODT>::iterator
	set<ODT> s;
	void init(int l,int r,int val){
		s.clear();
		s.insert({l,r,val});
	}
	IT split(int x){
		IT it=--s.upper_bound({x});
		if(it->l==x) return it;
		int l=it->l,r=it->r,val=it->val;
		s.erase(it);
		s.insert({l,x-1,val});
		return s.insert({x,r,val}).fir;
	}
	void change(int x,int y,int z){
		// cerr<<x<<' '<<y<<' '<<z<<'\n';
		if(x>y) return;
		IT r=split(y+1),l=split(x);
		for(IT it=l;it!=r;it++){
			int l=it->l,r=it->r,val=it->val;
			bit.add(val,-(r-l+1));
		}
		s.erase(l,r);
		bit.add(z,y-x+1);
		s.insert({x,y,z});
	}
	#undef IT
}odt;
void bfs(){
	bfn[1]=++bfn[0];
	q.push(1);
	while(!q.empty()){
		int u=q.front();q.pop();
		for(auto v:e[u]){
			if(fth[u]==v) continue;
			fth[v]=u;
			bfn[v]=++bfn[0];
			q.push(v);
		}
	}
}
void change(int x,int y){
	F(i,0,K){
		if(!x) break;
		odt.change(t[x][K-i].fir,t[x][K-i].sec,y);
		if(i!=K) odt.change(t[x][K-i-1].fir,t[x][K-i-1].sec,y);
		x=fth[x];
	}
}
void solve(){
	cin>>n>>K>>Q;
	F(i,1,n) e[i].clear();
	F(i,1,n-1){
		int x,y;
		cin>>x>>y;
		e[x].pb(y);
		e[y].pb(x);
	}
	F(i,1,n) qry[i].clear();
	F(i,1,Q){
		int x,y;
		cin>>x>>y;
		qry[y].pb({x,i});
	}
	bfs();
	bfn[0]=0;
	F(i,1,n) F(j,0,K) t[i][j]={n+1,0};
	F(i,1,n){
		int x=i;
		F(j,0,K){
			if(!x) break;
			chkmin(t[x][j].fir,bfn[i]);
			chkmax(t[x][j].sec,bfn[i]);
			x=fth[x];
		}
	}
	// F(i,1,n){
	// 	F(j,0,K) cout<<t[i][j].fir<<' '<<t[i][j].sec<<'\n';
	// 	cout<<'\n';
	// }
	odt.init(1,n,0);
	bit.clear();
	F(i,1,n){
		change(i,i);
		for(auto j:qry[i]) ans[j.sec]=bit.Query(j.fir,i);
	}
	F(i,1,Q) cout<<ans[i]<<'\n';
}
bool M2;
int main(){
	// freopen("count.in","r",stdin);
	// freopen("count.out","w",stdout);
	srand(time(0)^(*new int));
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>T;
	while(T--) solve();
	look_memory;
	look_time;
	return 0;
}
/*
g++ C1.cpp -o C1 -std=c++14 -O2&&./C1

1
5 1 2
1 2
1 3
2 4
2 5
2 2
2 3

2
5 1 2
1 2
1 3
2 4
2 5
2 2
2 3
8 2 3
1 2
1 3
2 4
2 5
4 6
5 7
7 8
2 2
2 5
3 4
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 10508kb

input:

2
5 1 2
1 2
1 3
2 4
2 5
2 2
2 3
8 2 3
1 2
1 3
2 4
2 5
4 6
5 7
7 8
2 2
2 5
3 4

output:

4
5
7
8
6

result:

ok 5 number(s): "4 5 7 8 6"

Test #2:

score: 0
Accepted
time: 391ms
memory: 9752kb

input:

1000
500 1 500
291 2
406 9
207 13
71 15
351 17
442 18
496 19
104 20
208 23
448 34
80 42
187 44
352 45
290 46
116 47
318 50
226 52
129 55
83 59
100 60
54 61
73 65
63 66
454 67
43 71
26 74
68 26
320 75
388 76
425 79
170 81
350 83
48 85
153 86
221 90
290 95
130 96
82 98
124 82
36 99
213 100
121 101
132...

output:

255
386
356
124
315
330
437
8
335
423
398
338
180
242
352
500
145
44
342
261
92
326
38
291
259
71
137
456
171
24
162
453
283
325
250
319
478
460
77
354
56
393
372
217
395
265
188
256
134
68
205
429
436
346
300
462
324
170
291
406
207
480
198
182
489
61
476
127
289
204
282
374
114
406
488
366
121
190...

result:

ok 500000 numbers

Test #3:

score: -100
Wrong Answer
time: 486ms
memory: 10736kb

input:

1000
500 2 500
260 2
93 3
399 4
227 5
238 6
382 7
35 12
194 24
141 26
463 27
497 30
102 31
410 32
308 34
357 42
271 43
208 44
446 46
262 50
457 51
467 52
294 53
424 54
267 56
210 58
48 59
339 48
407 65
320 66
33 68
78 33
79 71
315 72
390 74
128 76
83 81
244 87
375 91
79 93
225 94
1 97
433 1
172 100
...

output:

496
471
423
489
270
388
451
329
495
104
453
321
500
357
24
429
409
496
491
454
469
119
495
460
432
450
496
494
459
435
211
298
426
260
371
490
498
259
490
494
492
477
336
412
425
431
235
445
482
422
296
495
361
407
465
492
497
500
394
222
500
500
500
498
470
470
152
414
337
412
325
387
89
492
313
45...

result:

wrong answer 10076th numbers differ - expected: '240', found: '239'