QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#66503 | #5015. 树 | le0n | 0 | 29ms | 7892kb | C++14 | 5.6kb | 2022-12-08 20:18:51 | 2022-12-08 20:18:53 |
Judging History
answer
#include<bits/stdc++.h>
#include<random>
#include "tree.h"
int ask(int u,std::vector<int>v);
void answer(int u,int v);
const int N=1010;
std::mt19937 rnd(1);
int d[N];
inline void Shuffle(std::vector<int> &x){for(int i=0;i<int(x.size());++i) std::swap(x[i],x[rnd()%int(x.size())]);}
inline int randint(int l,int r){return l+rnd()%(r-l+1);}
std::map<std::pair<int,int>,int> mp;
inline int Query(int x,std::vector<int> &y)
{
std::vector<std::pair<int,int> > tmp;
for(auto c:y) tmp.push_back(std::make_pair(std::min(x,c),std::max(x,c)));
int cnt=0,ans=0;
std::vector<int> v;
for(auto c:tmp) cnt+=int(mp.count(c))^1;
if(!cnt)
{
for(auto c:tmp) ans+=mp[c];
return ans;
}
else if(cnt==1)
{
for(auto c:tmp)
if(mp.count(c)) ans+=mp[c];
else v.push_back(c.first^c.second^x),ans+=mp[c]=ask(x,v);
return ans;
}
else
{
for(auto c:tmp)
if(mp.count(c)) ans+=mp[c];
else v.push_back(c.first^c.second^x);
return ans+ask(x,v);
}
}
void dfs(std::vector<int> A,std::vector<int> B)
{
if(A.empty()||B.empty()) return ;
if(int(A.size())==1)
{
for(auto x:B) answer(A[0],x);
return ;
}
const int SZ=22;
if(int(A.size())==2&&int(B.size())>=SZ)
{
std::vector<int> tmp;
std::vector<bool> b;
tmp.push_back(A[1]);
int D=Query(A[0],tmp);
b.resize(B.size());
for(int i=0;i<int(B.size());++i) b[i]=false;
for(int i=0;i+SZ<int(B.size());i+=SZ)
{
tmp.clear();
for(int j=i;j<i+SZ;++j) tmp.emplace_back(B[j]);
int cur=Query(A[0],tmp);
if(cur==SZ)
for(int j=i;j<i+SZ;++j) answer(A[0],B[j]),b[j]=true;
else if(cur==(D+1)*SZ)
for(int j=i;j<i+SZ;++j) answer(A[1],B[j]),b[j]=true;
}
tmp.clear();
for(int i=0;i<int(B.size());++i) if(!b[i]) tmp.emplace_back(B[i]);
B=tmp;
if(B.empty()) return ;
}
Shuffle(A);
std::vector<int> q;
int c=randint(0.4*A.size(),0.6*A.size())+1;
if(c>int(A.size())) c=A.size()-1;
for(int i=0;i<int(A.size())/2;++i) q.emplace_back(A[i]);
std::unordered_map<int,std::vector<int> > mp1,mp2;
for(auto x:A) mp1[Query(x,q)].emplace_back(x);
for(auto x:B) mp2[Query(x,q)].emplace_back(x);
for(auto x:mp1)
{
std::vector<int> nxt1,nxt2;
for(auto y:x.second) nxt1.emplace_back(y);
if(mp2.count(x.first+int(q.size()))) for(auto y:mp2[x.first+int(q.size())]) nxt2.emplace_back(y);
dfs(nxt1,nxt2);
}
}
int R;
namespace TEST
{
using namespace std;
#define macro_expand(x) #x
#define print_macro(x) printf("%s\n",macro_expand(x))
#define FOR(i,l,r) for(int i=(l),i##ADJK=(r);i<=i##ADJK;++i)
#define ROF(i,r,l) for(int i=(r),i##ADJK=(l);i>=i##ADJK;--i)
const int MN=1e3+5;
int N,wes[MN],siz[MN],dep[MN];
vector<int> son[MN];
bool tag[MN],t2[MN];
void dfs(int u,int ff){
siz[u]=1;
for(int v:son[u]){
dfs(v,u);
siz[u]+=siz[v];
if((!wes[u])||siz[v]>siz[wes[u]])wes[u]=v;
}
for(int v:son[u]){
if(v!=wes[u])tag[u]|=t2[v];
t2[u]|=t2[v];
}
}
int jg(int u,vector<int> s){
int ret=0;
ret+=dep[u]*((int)s.size());
for(int v:s)ret+=dep[v];
return (ret-ask(u,s))/2;
}
int get(vector<int> s,int u){ // 可以试图加个随机化减小常数?
if(s.size()==1)return s[0];
vector<int> ls,rs;
FOR(i,1,s.size()/2)ls.push_back(s[i-1]);
FOR(i,s.size()/2+1,s.size())rs.push_back(s[i-1]);
int t=ask(u,ls);
if(t<((int)ls.size())*(dep[u]-dep[ls[0]]+2))return get(ls,u);
// 注意这里不是什么 dep[ls[0]]+2 是 dep[u]-dep[ls[0]]+2
else return get(rs,u);
}
int calc(int l,int r){return (l+r)*(r-l+1)/2;}
bool debug=0;
int find(int u,int nr){ // u 是要找的,nr 是当前的根
// if(debug)cerr<<"u:"<<u<<" nr:"<<nr<<endl;
if(dep[nr]==dep[u]-1)return nr;
vector<int> chain,pre;
int now=nr;
while(now){
// 注意,t2[now] && (!wes[now]) 也可能成为答案
if(tag[now]||((!wes[now])&&t2[now]))chain.push_back(now);
now=wes[now];
}
pre.resize(chain.size());
assert(chain.size()>=1);
pre[0]=dep[chain[0]];
FOR(i,1,chain.size()-1)pre[i]=pre[i-1]+dep[chain[i]];
int l=0,r=(int)chain.size()-2,sz=chain.size()-1,p=chain.size()-1;
if(chain.size()>1){
int t=jg(u,chain);
while(l<=r){
int mid=(l+r)>>1;
if((sz-mid+1)*dep[chain[mid]]+((mid-1>=0)?pre[mid-1]:0)>=t){
p=mid;
r=mid-1;
}else l=mid+1;
}
}else p=0;
vector<int> ls;
for(int v:son[chain[p]])
if(v!=wes[chain[p]]&&t2[v])ls.push_back(v);
if(ls.size()==0)return chain[p];
int nxt=get(ls,u);
return find(u,nxt);
}
bool cmp(const int &x,const int &y){return dep[x]<dep[y];}
void solver(int n,int A,int B)
{
// TODO: your solver
N=n;
int rt=R;
static int ord[MN],fa[MN];
FOR(i,1,N){
if(i!=rt)
{
// dep[i]=ask(rt,vector<int>({i}))+1;
dep[i]=d[i];
}
ord[i]=i;
}
dep[rt]=1;
sort(ord+1,ord+N+1,cmp); // 记得排序
int last=1;
FOR(i,2,N){
if(dep[ord[i]]!=dep[ord[i-1]]){
FOR(j,1,N)tag[j]=t2[j]=0;
FOR(j,last,i-1){ // 这个循环变量别重名……
t2[ord[j]]=1;
if(fa[ord[j]])
son[fa[ord[j]]].push_back(ord[j]);
}
dfs(rt,0);
last=i;
}
fa[ord[i]]=find(ord[i],rt);
}
FOR(i,1,N)if(fa[i])answer(i,fa[i]);
}
}
void solver(int n,int A,int B)
{
R=1+rnd()%n;
std::vector<int> tmp;
int mx=0;
for(int i=1;i<=n;++i) tmp.clear(),tmp.emplace_back(R),d[i]=ask(i,tmp),mx=std::max(mx,d[i]);
if(mx<=6){TEST::solver(n,A,B);return ;}
for(int i=1;i<=n;++i) if(d[i]==1) answer(R,i);
for(int i=1;i<n;++i)
{
std::vector<int> p,q;
for(int j=1;j<=n;++j)
{
if(d[j]==i) p.emplace_back(j);
else if(d[j]==i+1) q.emplace_back(j);
}
dfs(p,q);
}
}
详细
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 3
Accepted
time: 29ms
memory: 7768kb
input:
1000 500000 500000 1 2 2 3 2 4 2 5 2 6 3 7 2 8 5 9 5 10 9 11 2 12 9 13 4 14 5 15 12 16 5 17 4 18 4 19 13 20 9 21 19 22 7 23 6 24 14 25 2 26 10 27 14 28 21 29 17 30 8 31 15 32 9 33 22 34 24 35 20 36 6 37 12 38 19 39 31 40 35 41 25 42 11 43 8 44 9 45 12 46 26 47 10 48 6 49 27 50 39 51 33 52 6 53 43 54...
output:
areawavesuitbannerresortfatplasterdeclarationthesearejustrandomwords
result:
ok Orz..Orz..Orz..Orz..Orz
Test #2:
score: 0
Accepted
time: 22ms
memory: 7764kb
input:
1000 500000 500000 1 2 1 3 1 4 4 5 1 6 2 7 1 8 2 9 3 10 4 11 5 12 11 13 9 14 13 15 10 16 10 17 8 18 9 19 13 20 19 21 17 22 19 23 23 24 24 25 22 26 18 27 21 28 22 29 26 30 24 31 30 32 23 33 28 34 29 35 32 36 36 37 32 38 35 39 34 40 40 41 40 42 42 43 42 44 40 45 40 46 40 47 46 48 39 49 49 50 48 51 50 ...
output:
areawavesuitbannerresortfatplasterdeclarationthesearejustrandomwords
result:
ok Orz..Orz..Orz..Orz..Orz
Test #3:
score: -3
Dangerous Syscalls
input:
1000 500000 500000 498 209 498 647 498 776 498 8 498 382 498 181 498 644 498 331 498 516 498 197 498 630 498 693 498 577 498 572 498 393 498 638 498 94 498 847 498 273 498 535 498 703 498 176 498 605 498 214 498 610 498 416 498 928 498 470 498 753 498 182 498 294 498 514 498 831 498 386 498 935 498 ...
output:
Unauthorized output
result:
Subtask #2:
score: 0
Dangerous Syscalls
Test #11:
score: 0
Dangerous Syscalls
input:
100 3000 40000 66 95 66 60 66 93 66 69 66 82 66 24 66 64 66 84 66 42 66 22 66 67 66 54 66 90 66 26 66 41 66 18 66 43 66 68 66 36 66 88 66 33 66 29 66 79 66 6 66 48 66 47 66 8 66 38 66 61 69 97 64 30 38 86 88 14 18 10 54 81 88 25 29 2 18 21 95 46 42 80 93 91 61 62 68 35 47 23 69 17 93 28 18 31 61 70 ...
output:
Unauthorized output
result:
Subtask #3:
score: 0
Dangerous Syscalls
Test #111:
score: 20
Accepted
time: 13ms
memory: 7800kb
input:
1000 50000 3000000 126 207 937 126 615 937 837 615 500 837 588 500 505 588 353 505 60 353 904 60 656 904 685 656 460 685 614 460 551 614 537 551 858 537 596 858 9 596 738 9 918 738 322 918 940 322 859 940 113 859 110 113 312 110 995 312 443 995 246 443 257 246 238 257 999 238 885 999 976 885 330 976...
output:
areawavesuitbannerresortfatplasterdeclarationthesearejustrandomwords
result:
ok Orz..Orz..Orz..Orz..Orz
Test #112:
score: 0
Accepted
time: 11ms
memory: 7680kb
input:
1000 50000 3000000 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 8 16 8 17 9 18 9 19 10 20 10 21 11 22 11 23 12 24 12 25 13 26 13 27 14 28 14 29 15 30 15 31 16 32 16 33 17 34 17 35 18 36 18 37 19 38 19 39 20 40 20 41 21 42 21 43 22 44 22 45 23 46 23 47 24 48 24 49 25 50 25 51 26 52 2...
output:
areawavesuitbannerresortfatplasterdeclarationthesearejustrandomwords
result:
ok Orz..Orz..Orz..Orz..Orz
Test #113:
score: 0
Accepted
time: 14ms
memory: 7652kb
input:
1000 50000 3000000 1 2 2 3 2 4 4 5 5 6 6 7 6 8 8 9 8 10 10 11 10 12 12 13 12 14 13 15 14 16 15 17 16 18 18 19 18 20 19 21 20 22 21 23 22 24 24 25 24 26 26 27 27 28 27 29 28 30 29 31 30 32 31 33 32 34 34 35 35 36 36 37 36 38 37 39 39 40 39 41 41 42 41 43 42 44 43 45 45 46 45 47 47 48 48 49 48 50 50 5...
output:
areawavesuitbannerresortfatplasterdeclarationthesearejustrandomwords
result:
ok Orz..Orz..Orz..Orz..Orz
Test #114:
score: 0
Accepted
time: 16ms
memory: 7672kb
input:
1000 50000 3000000 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 8 16 8 17 9 18 9 19 10 20 10 21 11 22 11 23 12 24 12 25 13 26 13 27 14 28 14 29 15 30 15 31 16 32 16 33 17 34 17 35 18 36 18 37 19 38 19 39 20 40 20 41 21 42 21 43 22 44 22 45 23 46 23 47 24 48 24 49 25 50 25 51 26 52 2...
output:
areawavesuitbannerresortfatplasterdeclarationthesearejustrandomwords
result:
ok Orz..Orz..Orz..Orz..Orz
Test #115:
score: -20
Dangerous Syscalls
input:
1000 50000 3000000 31 688 31 684 31 63 31 564 31 34 31 288 31 808 31 356 31 327 31 458 31 993 31 344 31 902 31 407 31 37 31 150 31 969 31 323 31 790 31 464 31 230 31 999 31 936 31 106 31 965 31 771 31 663 31 476 31 652 31 991 31 475 31 258 31 395 31 664 31 762 31 934 31 951 31 419 31 84 31 70 31 167...
output:
Unauthorized output
result:
Subtask #4:
score: 0
Dangerous Syscalls
Test #211:
score: 60
Accepted
time: 19ms
memory: 7636kb
input:
990 8500 300000 1 2 1 3 1 4 1 5 2 6 2 7 2 8 3 9 3 10 3 11 4 12 4 13 4 14 5 15 5 16 5 17 6 18 6 19 6 20 7 21 7 22 7 23 8 24 8 25 8 26 9 27 9 28 9 29 10 30 10 31 10 32 11 33 11 34 11 35 12 36 12 37 12 38 13 39 13 40 13 41 14 42 14 43 14 44 15 45 15 46 15 47 16 48 16 49 16 50 17 51 17 52 17 53 18 54 18...
output:
areawavesuitbannerresortfatplasterdeclarationthesearejustrandomwords
result:
ok Orz..Orz..Orz..Orz..Orz
Test #212:
score: 0
Accepted
time: 23ms
memory: 7668kb
input:
992 8500 300000 1 2 2 3 3 4 4 5 3 6 5 7 7 8 3 9 3 10 2 11 8 12 4 13 4 14 9 15 11 16 5 17 5 18 7 19 12 20 5 21 5 22 10 23 9 24 23 25 22 26 11 27 21 28 28 29 23 30 19 31 5 32 12 33 9 34 11 35 3 36 19 37 10 38 33 39 12 40 12 41 38 42 31 43 25 44 6 45 5 46 36 47 23 48 28 49 31 50 28 51 25 52 5 53 25 54 ...
output:
areawavesuitbannerresortfatplasterdeclarationthesearejustrandomwords
result:
ok Orz..Orz..Orz..Orz..Orz
Test #213:
score: 0
Accepted
time: 12ms
memory: 7748kb
input:
999 8500 300000 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 8 16 8 17 9 18 9 19 10 20 10 21 11 22 11 23 12 24 12 25 13 26 13 27 14 28 14 29 15 30 15 31 16 32 16 33 17 34 17 35 18 36 18 37 19 38 19 39 20 40 20 41 21 42 21 43 22 44 22 45 23 46 23 47 24 48 24 49 25 50 25 51 26 52 26 5...
output:
areawavesuitbannerresortfatplasterdeclarationthesearejustrandomwords
result:
ok Orz..Orz..Orz..Orz..Orz
Test #214:
score: 0
Accepted
time: 24ms
memory: 7892kb
input:
995 8500 300000 1 2 1 3 2 4 1 5 1 6 2 7 7 8 3 9 6 10 9 11 2 12 1 13 9 14 4 15 9 16 13 17 13 18 14 19 6 20 18 21 21 22 14 23 12 24 19 25 9 26 26 27 16 28 28 29 7 30 14 31 1 32 25 33 32 34 5 35 8 36 22 37 19 38 15 39 13 40 27 41 25 42 18 43 12 44 14 45 8 46 36 47 33 48 45 49 46 50 44 51 47 52 15 53 2 ...
output:
areawavesuitbannerresortfatplasterdeclarationthesearejustrandomwords
result:
ok Orz..Orz..Orz..Orz..Orz
Test #215:
score: -60
Dangerous Syscalls
input:
999 8500 300000 1 2 2 3 3 4 4 5 2 6 5 7 4 8 8 9 6 10 1 11 7 12 6 13 2 14 2 15 10 16 10 17 7 18 9 19 4 20 4 21 7 22 1 23 4 24 8 25 1 26 8 27 5 28 6 29 3 30 8 31 6 32 8 33 3 34 10 35 9 36 5 37 9 38 3 39 10 40 7 41 6 42 8 43 7 44 2 45 3 46 10 47 2 48 2 49 5 50 6 51 1 52 2 53 3 54 1 55 7 56 5 57 3 58 10...
output:
Unauthorized output