QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#605312#8547. Whose Land?manizareWA 389ms53156kbC++142.9kb2024-10-02 16:40:012024-10-02 16:40:02

Judging History

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

  • [2024-10-02 16:40:02]
  • 评测
  • 测评结果:WA
  • 用时:389ms
  • 内存:53156kb
  • [2024-10-02 16:40:01]
  • 提交

answer

#include <bits/stdc++.h> 
 
#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define pii pair<int,int> 
#define F first 
#define S second 
#define ll long long 
#define sz(a) (int)a.size() 
#define all(a) a.begin(),a.end() 
#define rep(i,a,b) for(int i = (a) ; i <= (b) ; i++)
#define per(i,a,b) for(int i = (a) ; i >= (b) ; i--)
 
using namespace std ; 
const ll maxn = 5e5  + 10 , mod =10007, inf = 1e18, sq  = 800 ; 
int n , k , q , c =1 , pr[maxn] , ans[maxn] , par[maxn] , dis[maxn] , st[maxn] , en[maxn] , fen[maxn]; 
vector <int> G[maxn] , h[maxn] ;
vector <pii>  vec[maxn] ;
void dfs(int v, int p =0 ){
	st[v] = c; 
	c++;
	h[dis[v]].pb(st[v]); 
	pr[dis[v]] ++ ;
	for(int u : G[v]){
		if(u == p)continue ; 
		dis[u] = dis[v]+1 ;
		par[u] =v ; 
		dfs(u ,v) ;
	}
	en[v] = c-1 ;
}
void fupd(int w ,int x){
	if(x ==0)return ; 
	while (x <= n){
		fen[x] += w ;
		x += x&-x ;
	}
}
int fque(int x){
	int ans =0 ;
	while(x){
		ans += fen[x] ;
		x -= x&-x ;
	}
	return ans ; 
}
int w[maxn] ; 
set <int> s; 

void upd(int l , int r , int x){
	auto f= s.lower_bound(l+1) ;
	if((*f) > r){
		int R = (*f); 
		f--;
		int L = (*f); 
		fupd(-(r-l+1) , w[L]);
		fupd(r-l+1 , x) ;
		if(r+1 != R)w[r+1] = w[L];
		w[l] = x ;
		s.insert(l);
		s.insert(r+1) ;
		return ; 
	}
	vector <int> vec ;
	f--;
	while(1){	
		vec.pb((*f)) ;
		if((*f) > r){
			break ; 
		}
		f++ ;       
	}
	rep(i , 0 , sz(vec)-3){
		if((vec[i]) >= l && vec[i+1]-1 <= r){
			fupd(-(vec[i+1]-vec[i]) , w[vec[i]]) ;
			s.erase(vec[i]) ;
 		}
		if(vec[i] < l){
			fupd(-(vec[i+1]-l) , w[vec[i]]) ;
			s.insert(l) ;
		}
	}
	w[l] = x; 
	int L = vec[sz(vec)-2] ;
	s.erase(L) ;
	if(vec.back() != r+1){
		w[r+1] = w[L] ; 
		s.insert(r+1) ;
	}
	fupd(-(r-L+1) , w[L]) ;
	fupd(r-l+1, x) ;
	s.insert(l);
}

signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
	int t ;
	cin>> t;
	while(t--){
		c = 1; 
		cin >> n >> k >> q ;
		rep(i , 0 ,n){
			w[i] =0 ;
			fen[i] = 0;
			pr[i] =0 ;
			G[i].clear() ;vec[i].clear() ; 	h[i].clear() ;
		}
		rep(i , 1 ,n-1){
			int v, u ; cin >> v>> u;
			G[v].pb(u);
			G[u].pb(v); 
		}
		dis[1] =1 ;
		dfs(1); 
		rep(i ,1 ,n)pr[i]+=pr[i-1] ;
		rep(i ,1 ,q){
			int l ,r ;cin >> l >> r ;
			vec[l].pb({i,r}) ;
		}
		s.clear() ;
		s.insert(1);
		s.insert(n+1) ;
		per(x ,n , 1){
			int v =x ; 
			per(i , min(n , dis[x]+k) , max(1 , dis[x]-k)){
				while(v!=1 && i+dis[v]-2*dis[par[v]] <= k)v = par[v] ; 
				int l = lower_bound(all(h[i]) , st[v]) - h[i].begin();
				int r = lower_bound(all(h[i]) , en[v]+1) - h[i].begin() - 1 ;
				if(l > r)continue ; 
				l += pr[i-1] + 1;
				r += pr[i-1] + 1;
				upd(l , r , x) ;
			}
			for(auto [id , r] : vec[x]){
				ans[id] = fque(r) ;
			}
		}
		rep(i ,1 , q){
			cout << ans[i] << "\n" ;
		}
	}
}
/*
1
5 1 2
1 2
1 3
2 4
2 5
2 2
2 3
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 52708kb

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

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

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:

497
481
444
491
307
420
465
360
497
126
465
354
500
378
29
445
433
497
494
469
477
161
498
472
452
462
496
495
471
455
251
333
443
301
403
494
498
295
492
495
494
483
363
442
442
450
293
462
487
443
329
496
398
432
476
494
497
500
423
255
500
500
500
498
475
478
180
442
372
432
363
407
115
493
348
4...

result:

wrong answer 1st numbers differ - expected: '496', found: '497'