QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#103859 | #4046. 钥匙 | zaneyu# | 100 ✓ | 1325ms | 215296kb | C++20 | 6.8kb | 2023-05-07 18:44:32 | 2024-05-26 02:51:53 |
Judging History
answer
/*input
15 15
1 2
1 2
2 1
2 2
2 1
1 1
1 1
2 2
1 2
2 2
1 3
2 2
1 3
2 1
1 3
2 1
3 1
4 2
5 4
6 5
7 1
8 4
9 7
10 6
11 9
12 3
13 10
14 13
15 13
15 6
4 7
15 5
7 5
7 12
13 6
14 15
15 6
9 8
15 9
4 10
15 13
2 9
12 7
12 3
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
//#pragma GCC target("avx2")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=5e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
const ld PI=acos(-1);
const ld eps=1e-4;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
out<<P.f<<' '<<P.s;
return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?" ":"");
return out;
}
ll mult(ll a,ll b){
return a*b%MOD;
}
ll mypow(ll a,ll b){
a%=MOD;
if(a==0) return 0;
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=(res*a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return res;
}
struct IO{
static const int S=1<<21;
char buf[S],*p1,*p2;int st[105],Top;
~IO(){clear();}
inline void clear(){fwrite(buf,1,Top,stdout);Top=0;}
inline void pc(const char c){Top==S&&(clear(),0);buf[Top++]=c;}
inline char gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
inline IO&operator >> (char&x){while(x=gc(),x==' '||x=='\n'||x=='\r');return *this;}
template<typename T>inline IO&operator >> (T&x){
x=0;bool f=0;char ch=gc();
while(!isdigit(ch)){if(ch=='-') f^=1;ch=gc();}
while(isdigit(ch)) x=(x<<3)+(x<<1)+ch-'0',ch=gc();
f?x=-x:0;return *this;
}
inline IO&operator << (const char c){pc(c);return *this;}
template<typename T>inline IO&operator << (T x){
if(x<0) pc('-'),x=-x;
do{st[++st[0]]=x%10,x/=10;}while(x);
while(st[0]) pc('0'+st[st[0]--]);return *this;
}
}fin,fout;
vector<int> key[maxn],tr[maxn];
int dep[maxn];
int dist[maxn];
int par[maxn][20];
vector<int> v[maxn];
int in[maxn],out[maxn];;
int cur=0;
void dfs(int u,int p){
par[u][0]=p;
in[u]=cur++;
for(int x:v[u]){
if(x==p) continue;
dep[x]=dep[u]+1;
dist[x]+=dist[u];
dfs(x,u);
}
out[u]=cur-1;
}
void sparse(int n){
REP1(j,19){
REP(i,n){
if(par[i][j-1]!=-1){
par[i][j]=par[par[i][j-1]][j-1];
}
else par[i][j]=-1;
}
}
}
int kth(int u,int k){
while(k){
u=par[u][__lg(lowb(k))];
k-=lowb(k);
}
return u;
}
int lca(int a,int b){
if(dep[a]<dep[b]) swap(a,b);
int d=dep[a]-dep[b];
while(d){
a=par[a][__lg(lowb(d))];
d-=lowb(d);
}
if(a==b) return a;
for(int j=19;j>=0;j--){
if(par[a][j]!=par[b][j]){
a=par[a][j],b=par[b][j];
}
}
return par[a][0];
}
bool cmp(int a,int b){
return in[a]<in[b];
}
vector<pii> build(vector<int> sp){
sort(ALL(sp),cmp);
vector<int> stk;
vector<pii> ed;
stk.pb(0);
for(int x:sp){
if(!x) continue;
int z=lca(x,stk.back());
if(z!=stk.back()){
while(sz(stk)>=2 and in[z]<in[stk[sz(stk)-2]]){
ed.pb({stk[sz(stk)-2],stk.back()});
stk.pop_back();
}
ed.pb({z,stk.back()});
if(in[z]>in[stk[sz(stk)-2]]){
stk[sz(stk)-1]=z;
}
else{
stk.pop_back();
}
}
stk.pb(x);
}
REP(i,sz(stk)-1) ed.pb({stk[i],stk[i+1]});
return ed;
}
vector<int> g[maxn];
bool tp[maxn];
int col[maxn];
int rt;
vector<pii> pths;
void dfs2(int u,int p,int tot){
if(tp[u] and col[u]==col[rt]){
--tot;
}
if(tot<0){
pths.pb({rt,u});
return;
}
for(int x:g[u]){
if(x==p) continue;
dfs2(x,u,tot+(!tp[x] and col[x]==col[rt]));
}
}
vector<pair<int,pii>> vv[maxn];
vector<pii> qq[maxn];
inline void add(int a,int b,int c,int d,int x){
vv[a].pb({x,{c,d}});
vv[b+1].pb({-x,{c,d}});
}
int bit[maxn];
inline void upd(int x,int v){
++x;
while(x<maxn){
bit[x]+=v;
x+=lowb(x);
}
}
inline int query(int x){
++x;
int ans=0;
while(x){
ans+=bit[x];
x-=lowb(x);
}
return ans;
}
int ans[maxn*2];
int main(){
int n,q;
fin>>n>>q;
REP(i,n){
int a,b;
fin>>a>>b;
tp[i]=(a-1),col[i]=b;
if(a==1) key[b].pb(i);
else tr[b].pb(i);
}
REP(i,n-1){
int a,b;
fin>>a>>b;
--a,--b;
v[a].pb(b),v[b].pb(a);
}
dfs(0,-1);
sparse(n);
REP(i,n){
vector<int> sp;
for(int x:key[i]) sp.pb(x);
for(int x:tr[i]) sp.pb(x);
if(!sz(sp)) continue;
auto ed=build(sp);
for(auto x:ed){
g[x.f].pb(x.s),g[x.s].pb(x.f);
}
for(int x:key[i]){
rt=x;
dfs2(x,-1,0);
}
for(auto x:ed) g[x.f].clear(),g[x.s].clear();
}
for(auto x:pths){
//cout<<x<<'\n';
int z=lca(x.f,x.s);
if(z==x.f){
int z=kth(x.s,dep[x.s]-dep[x.f]-1);
add(0,n-1,in[x.s],out[x.s],1);
add(in[z],out[z],in[x.s],out[x.s],-1);
}
else if(z==x.s){
int z=kth(x.f,dep[x.f]-dep[x.s]-1);
add(in[x.f],out[x.f],0,n-1,1);
add(in[x.f],out[x.f],in[z],out[z],-1);
}
else{
add(in[x.f],out[x.f],in[x.s],out[x.s],1);
}
}
REP(i,q){
int a,b;
fin>>a>>b;
--a,--b;
qq[in[a]].pb({in[b],i});
}
int p=0;
REP(i,n){
for(auto x:vv[i]){
upd(x.s.f,x.f);
upd(x.s.s+1,-x.f);
}
for(auto x:qq[i]){
ans[x.s]=query(x.f);
}
}
REP(i,q) fout<<ans[i],fout.pc('\n');
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 6ms
memory: 17992kb
input:
100 100 2 1 2 5 2 5 2 1 2 5 1 3 2 1 1 5 2 6 2 2 1 6 2 1 1 1 2 1 2 6 1 5 2 3 2 6 1 7 2 4 2 4 1 7 2 4 2 6 2 6 2 5 1 2 1 5 2 3 2 3 2 1 2 2 1 4 1 2 2 3 2 3 2 1 1 7 2 3 2 2 2 6 2 3 1 3 2 6 2 3 2 2 2 5 2 5 2 5 2 5 1 5 2 1 2 5 2 4 2 4 2 4 2 1 1 2 2 3 1 3 2 4 2 2 2 1 2 1 2 3 1 4 1 4 1 1 1 2 2 3 2 2 2 3 2 4 ...
output:
0 0 0 2 0 0 1 0 0 0 0 1 2 0 0 0 0 0 0 1 1 1 2 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 2 0 1 1 0 0 2 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 2 0 1 0 0 0 1 0 0 0 0 1 0 0 2 0 0 0 0 1 0 0 1 0 0 3 0 0 0 0 0 0 1
result:
ok 100 numbers
Test #2:
score: 10
Accepted
time: 8ms
memory: 21468kb
input:
5000 5000 1 24 2 102 1 215 2 24 2 141 2 109 2 252 1 235 2 77 2 278 2 292 1 12 2 201 2 227 2 238 1 152 1 116 2 204 2 122 1 149 2 284 2 254 2 115 2 234 2 203 2 238 2 291 2 58 1 289 2 105 1 33 2 236 2 184 2 57 2 121 2 17 2 245 2 134 1 245 2 73 1 26 2 240 2 15 1 129 2 196 1 23 2 279 2 168 2 48 2 206 2 3...
output:
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 0 0 1 0 1 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 ...
result:
ok 5000 numbers
Test #3:
score: 10
Accepted
time: 7ms
memory: 17788kb
input:
5000 5000 2 230 2 243 2 77 2 149 2 298 2 176 1 103 2 131 1 127 2 110 1 115 1 220 1 23 2 259 2 290 2 77 2 211 2 249 2 232 2 163 2 55 2 277 2 148 2 171 2 14 2 226 2 70 1 194 1 269 2 277 2 4 2 107 1 246 2 226 2 79 2 219 1 21 2 203 2 4 2 129 2 87 1 114 2 180 2 37 2 202 2 5 2 39 1 28 2 168 2 35 1 184 2 1...
output:
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 5000 numbers
Test #4:
score: 10
Accepted
time: 124ms
memory: 52964kb
input:
100000 100000 2 56 1 2499 2 5148 2 2178 2 106 2 5422 2 2276 1 1085 2 1681 1 528 1 4743 2 3591 2 4902 2 5943 1 2664 2 5377 2 1923 2 195 2 1765 2 3177 2 3947 2 649 2 4772 2 3331 2 547 2 2908 2 4684 2 2741 2 5343 2 2494 2 3140 2 3823 1 1817 1 5225 2 3689 2 2768 2 6070 1 3584 1 2328 1 1506 2 4544 2 3323...
output:
5205 2591 208 3767 2284 3926 1985 438 1821 416 2614 3977 96 1397 2536 322 1682 100 396 1550 3809 1219 0 594 2058 169 860 2089 2549 5999 1837 2585 23 424 3309 371 383 4510 446 1975 967 547 1136 5210 2279 3656 0 126 1893 311 7977 454 4762 258 2000 2440 3498 2989 7478 2691 608 2620 5296 4403 4699 1925 ...
result:
ok 100000 numbers
Test #5:
score: 10
Accepted
time: 118ms
memory: 52668kb
input:
100000 100000 1 771 2 5185 2 1153 1 1611 2 2438 2 3680 2 5272 2 13 2 3069 2 3468 2 2209 2 6050 2 3287 2 1728 2 4981 2 1757 2 3630 2 1746 2 3057 1 726 2 2900 2 2033 2 2601 1 1478 2 214 1 5337 2 3948 2 1703 2 5112 2 5533 2 3558 2 1891 2 3887 2 2952 1 3425 1 177 1 1297 1 1916 2 1754 2 710 2 5212 1 1117...
output:
2149 1638 699 2224 2602 2596 401 1004 4 800 557 2105 3555 2186 2732 3767 1474 1972 3163 4410 2246 1899 1484 143 2349 388 1692 574 691 1414 3048 3147 1865 1487 1970 973 2104 1430 3776 618 750 887 3095 131 2560 3480 1730 3088 1026 3257 1164 122 3208 4370 3212 997 335 524 628 5280 1407 749 321 116 315 ...
result:
ok 100000 numbers
Test #6:
score: 10
Accepted
time: 865ms
memory: 200448kb
input:
500000 1000000 1 64861 1 119062 1 65144 1 143894 1 73264 2 33253 2 173191 2 52204 1 40950 2 71504 1 94677 2 28492 2 212348 2 155025 1 189945 2 71636 2 137081 2 129336 1 150409 2 200735 1 127414 2 155968 1 83751 2 29575 1 25125 1 111882 2 83939 1 64246 1 195870 1 14834 2 10887 2 98830 1 197278 2 1160...
output:
16 1 15 4 9 21 36 70 11 24 53 18 2 62 16 30 24 51 32 10 59 122 16 8 5 55 53 22 11 12 17 12 42 70 37 26 13 34 6 101 11 22 18 21 84 30 35 39 67 24 11 8 0 25 40 1 30 42 38 121 23 37 9 9 37 45 30 33 24 5 22 43 43 20 20 14 41 6 10 20 54 36 19 97 32 19 6 48 14 48 48 80 33 2 3 12 18 11 33 1 47 27 54 17 93 ...
result:
ok 1000000 numbers
Test #7:
score: 10
Accepted
time: 852ms
memory: 200432kb
input:
500000 1000000 2 99254 2 207080 2 40178 2 203616 2 143664 1 172563 1 235135 1 39267 2 34014 2 240014 2 122830 2 65547 1 190038 1 133892 1 195039 2 242508 1 16681 1 171326 1 179195 1 28872 2 91496 2 196836 1 12722 1 133176 1 49610 1 160238 2 137275 2 82747 2 22809 2 148101 2 18675 1 120676 1 86886 1 ...
output:
90 11 22 19 119 155 49 44 37 66 62 24 11 62 89 56 28 60 10 22 177 10 95 67 57 52 84 126 93 5 106 34 53 37 8 47 53 33 135 30 105 50 51 57 4 101 91 168 11 70 1 46 142 3 131 70 49 98 121 13 61 1 68 61 38 94 20 112 31 159 23 47 47 4 155 135 109 78 142 35 36 9 24 49 0 34 58 95 98 187 91 126 6 32 31 58 63...
result:
ok 1000000 numbers
Test #8:
score: 10
Accepted
time: 844ms
memory: 200152kb
input:
500000 1000000 1 89685 2 77808 2 60421 1 3478 1 174801 2 79073 1 171086 2 189510 2 141242 2 42469 2 197806 1 17953 2 165030 1 200212 2 65004 2 67818 2 86070 1 111422 1 229646 2 68851 1 14764 1 230949 2 71728 1 45456 1 5844 1 191082 1 54319 2 135488 2 24463 1 2976 2 226836 1 57817 2 45114 2 58082 1 3...
output:
21 33 31 22 54 59 25 2 38 39 7 42 45 132 98 12 18 24 5 34 38 36 4 11 41 14 36 36 18 2 33 8 35 42 28 8 13 64 18 41 12 29 67 32 6 34 26 22 33 80 39 66 8 11 84 52 45 120 74 53 9 37 28 59 65 36 29 11 4 38 18 10 6 66 20 22 38 32 21 27 12 37 35 22 29 37 31 6 39 33 38 13 36 68 39 21 77 28 46 38 33 30 12 7 ...
result:
ok 1000000 numbers
Test #9:
score: 10
Accepted
time: 1303ms
memory: 215296kb
input:
500000 1000000 1 6055 2 7862 2 1392 2 1440 2 15064 2 460 1 29587 2 3993 2 29185 2 29543 2 583 1 19479 2 28783 2 26722 1 17041 2 10894 2 3234 2 25053 1 15206 1 6122 2 25838 1 28948 2 25858 2 27787 2 19146 2 27381 2 20541 2 24645 1 16937 2 3163 2 3194 2 25925 2 11649 2 20379 1 7937 2 1913 2 18048 1 94...
output:
411 2 789 108 281 204 76 509 45 647 9 16 279 164 433 63 173 217 324 86 107 43 202 535 464 94 64 227 267 46 13 350 216 218 575 73 128 230 365 285 131 200 75 364 9 46 8 35 271 81 100 563 436 225 545 129 255 115 150 854 115 133 119 108 517 108 726 255 32 147 292 190 78 319 79 64 176 100 360 100 289 349...
result:
ok 1000000 numbers
Test #10:
score: 10
Accepted
time: 1325ms
memory: 214832kb
input:
500000 1000000 1 21782 1 23178 2 3615 1 22855 2 689 2 18959 2 17609 2 21923 1 28277 2 16406 2 7256 1 10175 2 25925 2 15485 2 25959 2 25908 1 20117 2 14563 1 25592 2 15719 1 9408 2 9539 1 28533 2 14576 2 2884 2 2701 1 13792 1 4222 2 21169 1 9207 2 17859 2 21128 1 3543 1 25941 2 4987 2 2529 2 20743 2 ...
output:
22 96 178 186 24 410 355 316 152 349 7 628 247 152 573 216 101 30 172 189 76 8 127 95 351 69 181 139 204 175 261 6 195 96 211 39 159 550 126 40 259 324 252 48 126 342 143 513 235 331 142 173 153 257 133 120 252 318 178 775 161 216 146 18 221 78 214 248 180 172 196 180 262 501 155 149 149 228 127 187...
result:
ok 1000000 numbers
Extra Test:
score: 0
Extra Test Passed