QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#465377#8547. Whose Land?ucup-team191#WA 718ms11740kbC++233.4kb2024-07-06 20:29:142024-07-06 20:29:16

Judging History

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

  • [2024-07-06 20:29:16]
  • 评测
  • 测评结果:WA
  • 用时:718ms
  • 内存:11740kb
  • [2024-07-06 20:29:14]
  • 提交

answer

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
using pii=pair<int,int>;
using ll=long long;
using vi=vector<int>;
using vl=vector<ll>;
#define pb push_back
#define all(a) begin(a),end(a)

const int N=500010,MOD=1e9+7,M=1<<17;
const char en='\n';
const ll LLINF=1ll<<60;

int t,n,q,k,par[N],seg2[M*2+10],ncn[N*16];
vi ch[N];
vector<pii> qu[N];
vector<pair<pii,ll>> naup[N];
vector<pair<pair<pii,pii>,int>> orqu;
vector<pair<pii,vi>> umch[N/4][25];
vi pard[N/4][25];
ll seg[M*2+10],an[N];

void upd(int i,ll x)
{
	for (i+=M;i;i/=2) seg[i]+=x;
}

ll ge(int l,int r,int lo=0,int hi=M,int i=1)
{
	if (lo>=l && hi<=r) return seg[i];
	if (lo>=r || hi<=l) return 0;
	int mid=(lo+hi)/2;
	return ge(l,r,lo,mid,i*2)+ge(l,r,mid,hi,i*2+1);
}

void dfs(int i,int p=0)
{
	par[i]=p;
	for (auto x: ch[i]) if (x!=p) dfs(x,i);
}

vi mer(vi a,const vi&b)
{
	for (auto x: b) a.pb(x);
	return a;
}

void upd2(int i,int x)
{
	for (i+=M;i;i/=2) seg2[i]+=x;
}

int findBef(int i)
{
	i+=M;
	while (i>1)
	{
		if (i%2==1 && seg2[i^1])
		{
			i^=1;
			while (i<M)
			{
				if (seg2[i*2+1]) i=i*2+1;
				else i=i*2;
			}
			return i-M;
		}
		i/=2;
	}
	return 0;
}

int findAft(int i)
{
	i+=M;
	while (i>1)
	{
		if (i%2==0 && seg2[i^1])
		{
			i^=1;
			while (i<M)
			{
				if (seg2[i*2]) i=i*2;
				else i=i*2+1;
			}
			return i-M;
		}
		i/=2;
	}
	return n+1;
}

void dod(int i)
{
	//cout<<"dod "<<i<<endl;
	assert(seg2[i+M]==0);
	upd2(i,1);
	int l=findBef(i),r=findAft(i);
	orqu.pb({{{l+1,i+1},{i,r}},1});
}

void rem(int i)
{
	//cout<<"rem "<<i<<endl;
	assert(seg2[i+M]==1);
	upd2(i,-1);
	int l=findBef(i),r=findAft(i);
	orqu.pb({{{l+1,i+1},{i,r}},-1});
}

void dfs2(int i,int j)
{
	//cout<<"dfs2 "<<i<<' '<<j<<endl;
	if (j==k)
	{
		++ncn[(int)orqu.size()-1];
	}
	else
	{
		for (auto x: umch[i][j])
		{
			//cout<<"idem u "<<x.x.x<<' '<<x.x.y<<en;
			for (auto y: x.y) dod(y);
			dfs2(x.x.x,x.x.y);
			for (auto y: x.y) rem(y);
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin>>t;
	while (t--)
	{
		cin>>n>>k>>q;
		for (int i=0;i<=n;++i)
		{
			naup[i].clear();
			ch[i].clear();
			qu[i].clear();
			for (int j=0;j<=k;++j) umch[i][j].clear(),pard[i][j].clear();
			upd(i,-seg[i+M]);
		}
		for (int i=0;i<=4*n*k;++i) ncn[i]=0;
		orqu.clear();
		for (int i=1;i<n;++i)
		{
			int a,b;
			cin>>a>>b;
			ch[a].pb(b);
			ch[b].pb(a);
		}
		dfs(1);
		for (int i=1;i<=n;++i)
		{
			int c=i;
			for (int j=0;j<=k;++j)
			{
				pard[c][j].pb(i);
				c=par[c];
				if (c==0) break;
			}
		}
		for (int j=1;j<=k;++j) umch[1][j-1].pb({{1,j},pard[1][j]});
		for (int i=2;i<=n;++i) for (int j=1;j<=k;++j)
		{
			umch[par[i]][j-1].pb({{i,j},mer(pard[i][j-1],pard[i][j])});
		}
		for (int i=1;i<=n;++i)
		{
			dod(i);
			dfs2(i,0);
			rem(i);
		}
		for (int i=(int)orqu.size()-1;i>=0;--i) ncn[i]+=ncn[i+1];
		for (int i=0;i<(int)orqu.size();++i)
		{
			int l1=orqu[i].x.x.x,r1=orqu[i].x.x.y,l2=orqu[i].x.y.x,r2=orqu[i].x.y.y;
			ll cn=orqu[i].y*1ll*ncn[i];
			naup[l1].pb({{l2,r2},cn});
			naup[r1].pb({{l2,r2},-cn});
		}
		for (int i=0;i<q;++i)
		{
			int l,r;
			cin>>l>>r;
			qu[l].pb({r,i});
		}
		for (int i=0;i<=n;++i)
		{
			for (auto x: naup[i])
			{
				upd(x.x.x,x.y);
				upd(x.x.y,-x.y);
			}
			for (auto x: qu[i])
			{
				an[x.y]=ge(0,x.x+1);
			}
		}
		for (int i=0;i<q;++i) cout<<an[i]<<en;
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 11740kb

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: -100
Wrong Answer
time: 718ms
memory: 9096kb

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:

wrong answer 501st numbers differ - expected: '357', found: '32206'