QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#305340 | #4909. 《关于因为与去年互测zjk撞题而不得不改题这回事》 | fireince | 0 | 0ms | 79516kb | C++14 | 4.6kb | 2024-01-15 07:22:02 | 2024-01-15 07:22:02 |
Judging History
answer
#include<iostream>
#include<cassert>
#include<queue>
#include<array>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<ll,int> pii;
const int N=1e6+10,K=63;
int n;
vector<int> G[N];
ll val[N];
typedef vector<ll> Dat;
namespace IO{
#define BUF_SIZE (1<<16)
#define OUT_SIZE (1<<16)
bool IOerror=0;
inline char nc(){static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;if(p1==pend){p1=buf;pend=buf+fread(buf,1,BUF_SIZE,stdin);if(pend==p1)return IOerror=1,-1;}return *p1++;}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline void read(int &x){bool sign=0;char ch=nc();x=0;for(;blank(ch);ch=nc());if(IOerror)return;if(ch=='-')sign=1,ch=nc();for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';if(sign)x=-x;}
inline void read(ll &x){bool sign=0;char ch=nc();x=0;for(;blank(ch);ch=nc());if(IOerror)return;if(ch=='-')sign=1,ch=nc();for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';if(sign)x=-x;}
struct Ostream_fwrite{
char *buf,*p1,*pend;
Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;}
inline void out(char ch){if(p1==pend){fwrite(buf,1,BUF_SIZE,stdout);p1=buf;}*p1++=ch;}
inline void print(ll x){static char s[25],*s1;s1=s;if(!x)*s1++='0';if(x<0)out('-'),x=-x;while(x)*s1++=x%10+'0',x/=10;while(s1--!=s)out(*s1);}
inline void println(ll x){print(x);out('\n');}
inline void flush(){if(p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}}
~Ostream_fwrite(){flush();}
}Ostream;
inline void print(ll x){Ostream.print(x);}
inline void println(ll x){Ostream.println(x);}
inline void println(){Ostream.out('\n');}
inline void flush(){Ostream.flush();}
#undef OUT_SIZE
#undef BUF_SIZE
}using namespace IO;
const int H=20
;
ll f[H][N];
int fp[H][N];
struct DS{
void build(int n,ll* a){
for(int i=1;i<=n;i++)f[0][i]=a[i],fp[0][i]=i;
for(int i=1;i<H;i++)
for(int j=1;j+(1<<i)-1<=n;j++)
f[i][j]=max(f[i-1][j],f[i-1][j+1<<i-1]),
fp[i][j]=f[i-1][j]>f[i-1][j+(1<<i-1)]?fp[i-1][j]:fp[i-1][j+(1<<i-1)];
}
pii query(int l,int r){
if(l>r)return {0,0};
int len=r-l+1,h=__lg(len);
ll v=max(f[h][l],f[h][r-(1<<h)+1]);
int p=v==f[h][l]?fp[h][l]:fp[h][r];
return {v,p};
}
} ds;
int dfn[N],top[N],dcnt,siz[N],son[N],fa[N],dep[N];
ll arr[N];
void dfs1(int u,int f){
fa[u]=f,dep[u]=dep[f]+1,siz[u]=1;
for(int v:G[u]){
if(v==f)continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[son[u]]<siz[v])son[u]=v;
}
}
void dfs2(int u,int tp){
top[u]=tp,dfn[u]=++dcnt,arr[dfn[u]]=val[u];
if(son[u])dfs2(son[u],tp);
for(int v:G[u]){
if(v==son[u]||v==fa[u])continue;
dfs2(v,v);
}
}
typedef array<ll,4> rg;
struct cmp{
bool operator()(const rg& a,const rg& b) const{
return a[0]<b[0];
}
};
typedef priority_queue<rg,vector<rg>,cmp> pqt;
void insertRange(int l,int r,pqt& pq){
auto res=ds.query(l,r);
pq.push({res.first,res.second,l,r});
};
Dat queryPoints(int u,int v,int m){
Dat res;
pqt pq;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
insertRange(dfn[top[u]],dfn[u],pq);
u=fa[top[u]];
}
if(dep[u]<dep[v])swap(u,v);
insertRange(dfn[v],dfn[u],pq);
while(pq.size()&&res.size()<m){
auto a=pq.top();
pq.pop();
res.push_back(a[0]);
if(a[1]!=a[2])insertRange(a[2],a[1]-1,pq);
if(a[1]!=a[3])insertRange(a[1]+1,a[3],pq);
}
return res;
}
ll queryVal(Dat& dt,int m){
ll ans=0;
for(int i=K;i>=0;i--){
int cnt=0;
Dat ndt;
for(ll j:dt)if(j>>i&1)cnt++,ndt.push_back(j);
if(cnt>=m){
ans|=(1ll<<i);
dt=ndt;
}
}
return ans;
}
signed main(){
read(n);
for(int i=1;i<n;i++){
int u,v;
read(u),read(v);
G[u].push_back(v);
G[v].push_back(u);
}
for(int i=1;i<=n;i++)read(val[i]);
dfs1(1,0);
dfs2(1,1);
ds.build(n,arr);
int q;
read(q);
ll lastans=0;
for(int i=1;i<=q;i++){
int u,v,m;
read(u),read(v),read(m);
u=(u^lastans)%n+1,v=(v^lastans)%n+1;
Dat dt=queryPoints(u,v,K*min(3,m-1));
if(dt.size()<m){
lastans=0;
}else lastans=queryVal(dt,m);
println(lastans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 79516kb
input:
931 184 700 485 184 419 485 386 419 308 386 114 308 301 114 598 301 120 598 144 120 595 144 812 595 236 812 7 236 543 7 327 543 858 327 68 858 177 68 398 177 899 398 408 899 848 408 202 848 269 202 304 269 540 304 647 540 672 647 314 672 157 314 241 157 745 241 300 745 343 300 92 343 117 92 30 117 2...
output:
1152921504606846992
result:
wrong answer 1st numbers differ - expected: '1152921504606846976', found: '1152921504606846992'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Runtime Error
Test #17:
score: 0
Runtime Error
input:
99115 98506 98914 1961 98506 45808 1961 23027 45808 16655 23027 66393 16655 77250 66393 68284 77250 53684 68284 21189 53684 84955 21189 73464 84955 47574 73464 40651 47574 21101 40651 6589 21101 59680 6589 6185 59680 25529 6185 207 25529 33286 207 98459 33286 92565 98459 85446 92565 97388 85446 1630...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
0%
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Runtime Error
Test #45:
score: 0
Runtime Error
input:
996678 2 1 3 1 4 1 5 1 6 3 7 5 8 5 9 5 10 7 11 8 12 9 13 1 14 2 15 7 16 4 17 5 18 17 19 16 20 2 21 1 22 1 23 9 24 17 25 19 26 10 27 9 28 7 29 25 30 25 31 4 32 11 33 31 34 21 35 13 36 19 37 25 38 10 39 11 40 20 41 35 42 1 43 19 44 20 45 41 46 1 47 19 48 5 49 28 50 21 51 33 52 7 53 14 54 21 55 20 56 1...
output:
result:
Subtask #8:
score: 0
Skipped
Dependency #1:
0%