QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#339663 | #6142. 宝石 | LYT0122 | 100 ✓ | 312ms | 133232kb | C++14 | 3.3kb | 2024-02-27 19:24:52 | 2024-02-27 19:24:52 |
Judging History
answer
#include <iostream>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long int ll;
const int N=1e6+9,INF=1e9;
const double eps=1e-5;
typedef pair <int,int> PII;
inline int read()
{
int x=0,f=1;char c=getchar();
while(c<'0' || c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-48,c=getchar();}
return x*f;
}
inline ll readll()
{
ll x=0,f=1;char c=getchar();
while(c<'0' || c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-48,c=getchar();}
return x*f;
}
struct node{
int to,nxt;
} edge[N<<1];
int n,m,c,q,id[N],a[N],tot,head[N],pos1[N],pos[N],fa[N][20],lg[N],f[N][20],g[N][20],dep[N],ans[N];
vector <PII> qu[N];
void addedge(int u,int v)
{
edge[++tot].to=v,edge[tot].nxt=head[u],head[u]=tot;
}
void dfs(int u,int father)
{
if(a[u]==1) pos1[u]=u;
else pos1[u]=pos1[father];
int x=pos[a[u]];
pos[a[u]]=u;
f[u][0]=pos[a[u]+1],g[u][0]=pos[a[u]-1],dep[u]=dep[father]+1,fa[u][0]=father;
for(int i=head[u];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(v==father) continue;
dfs(v,u);
}
pos[a[u]]=x;
}
int LCA(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
while(dep[x]>dep[y]) x=fa[x][lg[dep[x]-dep[y]]];
if(x==y) return x;
for(int i=lg[dep[x]];i>=0;i--)
if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
bool check(int x,int p,int lim)
{
int u=pos[x];
if(dep[u]<dep[p]) return false;
int ans=x;
for(int i=lg[dep[u]];i>=0;i--)
if(dep[g[u][i]]>=dep[p]) ans-=(1<<i),u=g[u][i];
return ans<=lim;
}
void dfs2(int u,int father)
{
int x=pos[a[u]];
pos[a[u]]=u;
for(int i=0;i<qu[u].size();i++)
{
int s=qu[u][i].first,j=qu[u][i].second;
int p=LCA(s,u),tmp=0;
s=pos1[s];
if(dep[s]>=dep[p])
{
for(int i=lg[dep[s]];i>=0;i--)
if(dep[f[s][i]]>=dep[p]) s=f[s][i];
tmp=a[s];
}
int l=tmp+1,r=c,res=tmp;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid,p,tmp+1)) l=mid+1,res=mid;
else r=mid-1;
}
ans[j]=res;
}
for(int i=head[u];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(v!=father) dfs2(v,u);
}
pos[a[u]]=x;
}
int main()
{
// #define FILEIO
#ifdef FILEIO
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
#endif
n=read(),m=read(),c=read();
for(int i=1;i<=c;i++) id[read()]=i;
for(int i=1;i<=n;i++) a[i]=id[read()];
for(int i=1;i<n;i++)
{
int u=read(),v=read();
addedge(u,v),addedge(v,u);
}
dfs(1,0);
q=read();
for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1;
for(int i=1;i<=lg[n];i++)
for(int j=1;j<=n;j++)
fa[j][i]=fa[fa[j][i-1]][i-1],f[j][i]=f[f[j][i-1]][i-1],g[j][i]=g[g[j][i-1]][i-1];
for(int i=1;i<=q;i++)
{
int s=read(),t=read();
qu[t].push_back({s,i});
}
dfs2(1,0);
for(int i=1;i<=q;i++) cout<<ans[i]<<'\n';
cerr<<endl<<1e3*clock()/CLOCKS_PER_SEC<<"ms";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 0ms
memory: 42448kb
input:
10 4 4 2 1 4 3 2 4 4 1 1 1 1 1 4 4 2 1 3 2 4 1 5 1 6 5 7 1 8 5 9 5 10 3 10 5 1 1 7 6 8 5 10 7 6 9 3 3 9 2 7 9 7 2 4
output:
1 2 0 1 2 1 3 2 2 2
result:
ok 10 numbers
Test #2:
score: 5
Accepted
time: 0ms
memory: 39864kb
input:
10 4 4 1 3 2 4 3 4 4 3 1 3 1 2 4 2 2 1 3 1 4 2 5 2 6 4 7 6 8 5 9 2 10 4 10 2 8 6 9 1 6 9 8 4 9 2 8 4 6 3 10 7 8 1 10
output:
1 0 0 1 0 1 0 0 3 0
result:
ok 10 numbers
Test #3:
score: 5
Accepted
time: 0ms
memory: 40792kb
input:
1000 20 20 20 3 1 5 4 17 10 11 2 8 19 7 15 13 9 14 16 18 12 6 16 1 17 10 17 3 12 1 15 18 1 10 10 13 1 16 9 8 4 8 20 9 14 2 2 13 14 16 11 8 5 19 12 4 13 10 10 1 7 14 2 17 13 1 15 6 13 15 7 19 6 12 6 16 14 6 20 17 19 3 17 7 15 6 20 1 15 19 3 4 13 14 11 14 14 11 2 12 16 4 16 16 15 20 20 4 17 13 4 16 19...
output:
0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 2 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 2 0 0 0 1 0 0 1 0 2 0 0 0 0 1 2 0 0 0 0 3 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 1 1 0 2 0 1 0 0 1 0 0 0 0 0 0 2 0 0 1 1 0 0 0 1 0 1 0 2 0 1 1 0 0 0 2 0 1 0 1 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 1000 numbers
Test #4:
score: 5
Accepted
time: 3ms
memory: 40316kb
input:
1000 20 20 5 8 2 4 10 3 17 7 11 9 16 6 19 18 13 1 14 20 12 15 15 4 15 5 15 11 8 10 2 11 5 18 16 9 2 5 14 14 10 6 14 19 2 14 7 10 18 3 3 11 9 9 19 18 18 20 11 8 14 12 18 16 17 4 15 15 13 20 7 20 16 18 1 9 3 10 9 6 12 1 17 1 15 13 19 6 12 18 1 6 3 2 14 2 6 2 6 6 13 19 16 11 17 6 6 16 17 14 10 17 1 11 ...
output:
1 1 0 1 1 1 2 1 0 1 0 1 1 0 0 0 1 1 0 1 0 0 0 0 0 1 2 1 1 1 1 0 0 1 1 1 1 1 0 1 2 0 0 0 2 1 1 1 1 1 0 1 1 1 1 0 1 1 0 0 1 1 0 1 0 2 1 1 0 1 0 1 1 1 0 1 0 1 0 1 0 2 0 1 0 1 1 0 2 0 1 1 1 0 0 1 1 1 1 2 1 1 0 0 0 0 1 1 1 1 0 2 2 1 1 1 0 1 1 1 1 0 1 1 2 1 0 0 0 1 0 0 1 1 1 1 1 1 0 1 2 1 1 0 0 1 1 0 1 1 ...
result:
ok 1000 numbers
Test #5:
score: 5
Accepted
time: 0ms
memory: 41248kb
input:
1000 20 20 16 6 12 9 8 3 17 13 14 18 4 10 7 20 15 19 1 5 2 11 12 8 20 15 15 15 8 13 7 16 2 16 15 3 6 8 5 8 9 15 10 18 4 8 17 15 10 19 9 13 15 13 17 12 15 14 4 11 12 6 11 1 11 9 3 11 19 12 8 8 6 18 4 12 19 18 2 5 17 18 13 11 13 14 9 16 3 1 18 5 5 18 3 18 14 2 15 3 10 16 9 11 16 9 12 5 13 11 8 1 16 19...
output:
0 1 2 3 0 2 0 2 3 1 0 0 0 2 0 1 0 2 1 3 2 0 1 1 1 0 0 0 1 0 1 0 0 0 1 0 2 2 0 1 0 1 0 0 1 2 0 0 2 0 1 0 0 0 3 0 0 0 1 2 2 1 0 3 0 2 2 0 0 1 2 2 0 0 2 0 0 0 0 1 3 1 0 0 1 0 0 0 0 0 1 1 2 0 0 0 3 1 1 2 0 0 0 0 0 3 1 0 1 0 1 0 1 0 0 2 0 0 2 1 1 1 1 1 0 0 3 3 2 2 0 1 0 2 1 0 1 1 1 1 0 0 0 1 1 1 1 0 0 1 ...
result:
ok 1000 numbers
Test #6:
score: 5
Accepted
time: 211ms
memory: 110728kb
input:
200000 300 300 196 108 179 192 60 223 53 267 231 41 90 297 123 193 81 234 151 287 167 107 86 163 200 112 182 181 17 217 230 237 249 202 127 61 80 286 34 279 52 118 14 58 85 255 262 268 105 257 194 160 190 211 110 106 71 206 283 141 154 145 245 67 216 271 274 31 146 131 189 82 29 73 282 228 5 201 187...
output:
206 221 27 174 170 15 93 159 200 248 188 235 210 76 115 227 50 148 152 214 157 72 2 202 278 200 219 24 201 137 238 144 109 176 18 176 231 5 203 107 176 35 202 223 34 173 293 271 224 212 63 139 258 149 213 74 199 204 185 269 193 236 4 248 199 86 195 268 212 23 22 17 58 258 214 116 25 172 190 237 42 2...
result:
ok 200000 numbers
Test #7:
score: 5
Accepted
time: 201ms
memory: 121052kb
input:
200000 300 300 295 274 12 146 252 147 282 178 187 13 35 278 199 11 134 165 229 172 261 244 238 202 118 245 124 209 26 228 190 5 149 141 132 107 184 175 121 283 114 15 101 60 173 158 243 289 91 263 160 50 240 31 194 49 36 102 34 96 130 98 248 55 6 223 46 272 213 182 1 164 144 224 279 14 82 256 106 23...
output:
237 1 196 224 175 178 134 77 112 126 167 261 171 239 242 15 88 300 173 300 208 293 38 186 245 171 300 209 18 46 244 138 300 58 200 137 267 172 175 218 60 47 147 181 254 147 195 236 38 133 150 186 95 217 181 90 181 190 243 209 39 284 109 212 201 300 122 300 63 242 201 244 249 45 217 174 6 288 203 172...
result:
ok 200000 numbers
Test #8:
score: 5
Accepted
time: 203ms
memory: 116160kb
input:
200000 300 300 10 14 170 41 80 89 291 95 48 135 157 230 249 110 186 87 111 93 299 84 73 35 127 259 33 30 120 252 2 205 178 166 142 52 297 62 254 124 224 72 168 167 67 147 4 85 160 189 136 213 37 123 28 211 300 203 46 285 13 104 103 286 198 241 55 20 98 39 29 1 287 226 112 208 260 32 47 266 75 82 204...
output:
159 2 300 74 185 203 113 199 181 94 74 37 300 229 52 248 181 213 113 225 137 185 28 222 185 225 157 168 12 57 300 217 41 18 184 216 300 44 61 121 7 185 300 27 26 121 187 300 300 81 44 105 177 241 25 231 300 48 246 300 230 206 21 201 176 300 204 98 166 194 201 141 196 299 49 62 122 35 293 190 177 141...
result:
ok 200000 numbers
Test #9:
score: 5
Accepted
time: 211ms
memory: 120552kb
input:
200000 300 300 135 142 98 181 82 176 67 7 257 203 299 27 102 265 215 178 12 147 31 218 36 253 138 290 70 159 78 20 95 96 54 58 40 293 240 150 13 143 97 275 37 4 187 146 89 126 112 127 38 144 157 15 109 206 124 33 177 23 233 6 268 270 101 274 50 191 61 287 99 87 259 115 196 118 229 3 25 48 134 24 28 ...
output:
233 87 11 219 46 100 75 251 141 300 177 239 251 3 181 173 98 229 197 300 79 191 234 300 236 190 201 40 266 261 300 181 163 182 157 80 248 174 237 30 218 272 260 192 112 4 190 86 13 261 134 190 238 64 81 102 182 207 271 92 220 231 212 126 257 6 231 62 287 181 9 23 124 179 300 209 279 194 155 83 182 6...
result:
ok 200000 numbers
Test #10:
score: 5
Accepted
time: 195ms
memory: 119716kb
input:
200000 300 300 274 49 162 254 89 166 94 176 152 82 270 204 218 150 126 106 14 252 194 64 103 71 39 79 70 81 132 102 18 223 217 47 171 142 196 99 186 73 231 178 80 258 293 192 221 238 40 68 37 101 46 172 190 241 52 153 197 266 220 159 17 173 38 299 263 95 48 151 76 255 191 112 207 226 57 135 61 275 1...
output:
211 112 300 200 76 147 142 104 153 29 210 22 25 208 217 190 20 181 173 140 94 35 211 300 148 101 184 191 206 205 150 138 16 191 218 182 270 278 210 197 282 255 126 221 270 245 201 136 23 27 180 192 157 89 221 1 119 6 259 117 231 10 72 16 200 56 68 181 72 208 97 263 113 259 91 1 44 193 281 42 181 190...
result:
ok 200000 numbers
Test #11:
score: 5
Accepted
time: 276ms
memory: 133232kb
input:
200000 20127 20127 18040 5063 1949 3575 2887 4151 1159 1395 9647 1376 16097 1422 9694 637 14090 9230 14318 13072 18802 7526 10464 4802 5645 5290 5964 12137 6076 13499 1318 13431 19198 9334 1530 14120 11039 15879 13900 16154 4761 9216 10424 9009 17705 9859 5950 3661 12576 7727 12666 3694 12659 11255 ...
output:
12538 18047 3941 18045 18047 0 14163 18045 18047 14805 18046 5 18047 18047 18045 14805 4023 18047 18045 18047 18047 14163 14475 18047 18047 14806 18047 4894 18047 18047 18045 18047 5231 18047 17134 5417 18045 10875 14163 7805 0 13240 18047 14297 3345 1 0 18047 12472 18047 14805 18047 14297 18047 146...
result:
ok 200000 numbers
Test #12:
score: 5
Accepted
time: 262ms
memory: 122552kb
input:
200000 23282 23282 21983 22037 20506 5857 14044 14669 3005 18638 11846 2997 11539 553 14415 19572 5331 4476 14759 12040 20064 7250 274 17286 11831 10626 7477 10762 6845 15046 12107 14685 18249 11525 2784 8792 17696 1632 23013 18350 2080 16516 16837 8455 17780 11515 18030 3775 18845 17393 21102 13274...
output:
18817 19036 19036 10381 18817 1 19036 13120 18671 1 19036 19036 19036 6451 18817 18817 19036 19036 1 8081 6361 18817 19036 16525 19036 12418 19036 18671 18817 18817 8729 12382 19036 18817 7658 19036 18817 12418 18817 18817 18817 19036 19036 18673 18817 18673 0 15257 19036 19036 18817 5726 18672 0 13...
result:
ok 200000 numbers
Test #13:
score: 5
Accepted
time: 279ms
memory: 123292kb
input:
200000 28544 28544 7244 5125 13001 24487 2244 14598 18685 18357 10046 22220 19562 8608 19790 17520 6216 14370 5433 26605 16988 9140 16226 26081 22437 5566 8245 14813 24444 28163 18528 2207 24794 12264 10635 5127 18902 19900 27203 26164 6846 6635 27305 1337 12224 16953 27055 12658 13986 1768 1176 192...
output:
1269 10708 17066 5467 21638 19074 7529 0 0 21638 19074 21638 19074 3844 661 19074 21638 21638 7932 0 21638 21178 21178 4516 18882 21638 21638 21638 21638 19074 1 19074 19074 21638 0 21638 17892 16810 21638 19074 0 8684 0 0 21638 21638 21638 19074 21638 21638 19074 0 21638 21638 0 21638 21638 0 21638...
result:
ok 200000 numbers
Test #14:
score: 5
Accepted
time: 312ms
memory: 119692kb
input:
200000 25120 25120 19718 16977 5459 6261 226 9358 19674 9257 20784 6657 3912 8494 14777 6941 9848 610 2913 20118 21634 14408 14166 22368 12192 12973 13467 4618 7443 8836 14545 14406 3814 24795 22415 13472 11011 16568 17540 8422 5328 13712 21752 18592 7878 24559 16275 21553 7342 11995 3001 16377 2430...
output:
7621 20397 16121 16464 10949 14903 15962 16123 16464 15962 15964 16123 0 20397 3887 20397 16464 16464 20397 16464 16400 16123 15690 0 15962 15963 16123 16464 16464 0 14902 15915 16464 15964 16120 15963 14765 16124 16464 12962 13556 16123 16121 12762 0 16039 15962 1 15633 20397 15853 16123 1 16124 0 ...
result:
ok 200000 numbers
Test #15:
score: 5
Accepted
time: 233ms
memory: 101560kb
input:
200000 24328 24328 15195 1593 23225 20912 7763 20719 9202 1254 3290 17138 4223 14496 15571 18186 1033 8842 8522 20814 14920 18798 2047 6512 1982 4197 11481 23482 16371 17861 6280 6624 8117 10033 4398 23867 3528 6 2981 6944 7481 20962 21517 17989 23203 22361 1038 1246 13657 22894 2348 6730 7110 8880 ...
output:
2 18071 8696 3 2058 3 6440 3921 5 2 282 17562 18305 7011 3449 3379 7787 6608 1087 18832 2 14196 3 1091 1089 3 1089 7604 4 12837 3 4981 0 4 1091 1091 13774 13080 3 282 3576 3 19952 2 8312 3 1 3 1 2 4 8033 2 3 1207 12526 4 2398 8975 2769 0 2058 2059 1091 9359 1087 1090 1 8676 7186 6434 2 16109 9263 15...
result:
ok 200000 numbers
Test #16:
score: 5
Accepted
time: 233ms
memory: 100216kb
input:
200000 23535 23535 22853 557 15383 107 16780 17397 19383 23406 1069 14 9230 480 5190 10487 9042 19046 471 4135 19888 850 6067 62 14804 4650 2920 13459 14544 17662 16078 7574 21213 5405 1919 15140 7146 2925 4778 14951 15553 8580 6064 3892 11013 11012 2097 18893 7494 5195 12354 2651 11175 12606 12664 ...
output:
13758 13237 4 2074 2 9193 2 0 1721 13648 1 4567 2083 16544 1720 458 458 11655 1719 2074 5347 459 460 2 4 3 3 461 458 3 458 1824 9402 38 2083 37 2074 15253 1720 2074 4 1309 2083 5 1 36 4 459 15530 1801 459 489 460 16343 14468 17020 459 1722 11411 2074 6534 457 6577 2074 2 7666 460 37 1 16043 4 1786 1...
result:
ok 200000 numbers
Test #17:
score: 5
Accepted
time: 222ms
memory: 104244kb
input:
200000 22743 22743 26 4272 21333 3815 310 870 19582 21253 2000 1381 11577 19266 2640 22550 17985 5590 7197 6217 5104 4265 21673 3978 16073 21248 21987 13590 5409 19015 1944 21469 21370 6780 20746 13576 8195 11758 14166 15845 8397 10421 14501 22171 21406 10972 7967 14255 7357 5492 14937 17706 20760 1...
output:
531 532 659 661 531 660 4 600 9466 3 8901 5870 661 138 659 659 662 3 527 660 529 527 15043 17119 530 659 4 660 527 530 3 5 2 8086 528 602 600 2392 531 0 1 661 529 1832 662 0 661 661 3 659 0 528 5 531 660 661 1835 4 1545 602 661 134 660 661 11389 2254 0 106 662 601 4 11165 1961 661 662 601 600 0 601 ...
result:
ok 200000 numbers
Test #18:
score: 5
Accepted
time: 242ms
memory: 114304kb
input:
200000 25897 25897 9229 3418 18627 21598 21258 18733 11907 14782 18543 17294 4540 18700 24910 13430 9782 9506 7390 2006 20953 3780 19408 18626 21687 19405 19781 12991 158 7068 23862 2769 22136 25319 13968 13880 24740 21463 21488 13464 15681 12433 24507 9218 20806 18559 6736 20446 13834 23123 24256 2...
output:
2428 3 10817 3 2 6184 2011 15695 590 587 1 709 7010 2 588 1 587 8920 3832 4748 710 5841 20179 4 10314 1 2 1 3 2224 589 2 14219 708 15074 2010 711 1 14397 2009 2 13360 710 3 15 588 10961 16241 2945 20280 11187 711 15696 15882 1 710 3589 20826 0 8671 9475 709 591 18956 10936 4 16186 10717 19118 708 7 ...
result:
ok 200000 numbers
Test #19:
score: 5
Accepted
time: 243ms
memory: 109124kb
input:
200000 23789 23789 17563 21148 9167 3895 16973 16113 9911 2518 4703 218 22930 284 16799 19632 16346 8224 2871 19891 2911 16600 6291 9116 21363 20129 5027 14959 3788 14003 15535 3755 22226 10930 13781 21835 12820 9609 18032 16716 503 10116 16382 10138 11039 21423 5390 8456 17993 7383 4331 4347 14717 ...
output:
563 1 2 1230 2 18355 4735 2128 1230 2150 2265 1231 1231 12921 5 1231 1229 8447 1 2 1229 1232 3 2168 1 2345 2262 5 1229 1229 1229 2 19441 4 13103 1230 560 1230 3 560 1 1230 5 0 2 2 1903 3 2265 1753 1230 1 11490 3579 2152 1229 10272 4820 2150 1231 2 3 1 1232 10256 1546 140 3 1231 1232 1 1 2128 1229 13...
result:
ok 200000 numbers
Test #20:
score: 5
Accepted
time: 238ms
memory: 110492kb
input:
200000 20366 20366 15031 14077 7945 19727 9442 11037 3781 14028 13750 13686 3985 17531 13107 605 9577 17785 10007 5565 2954 8868 3804 17501 1912 9811 5964 17853 16747 9194 14398 10970 8926 4950 5892 3292 18378 10549 8905 575 1906 6708 3449 1776 1494 9237 9873 17896 18498 15097 15870 6150 6842 18640 ...
output:
1 4 2381 1623 689 1 1053 11510 1628 1 1 1 0 0 5234 3 7572 1051 7051 4 15618 2435 1052 6278 6 1051 5708 1050 3 3 5495 13112 14498 1627 3 1620 0 1 1 2 0 1 1621 2 1621 1 12704 1 1 2801 10283 1052 6319 3 1 69 15951 1 2275 732 1 691 6447 1052 1050 3 2 690 2788 4 1 4 10129 1628 4 2 3 1 1052 1051 0 1 7137 ...
result:
ok 200000 numbers
Extra Test:
score: 0
Extra Test Passed