QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#342709 | #4909. 《关于因为与去年互测zjk撞题而不得不改题这回事》 | yuqihao | 0 | 1034ms | 290604kb | C++14 | 4.1kb | 2024-03-01 15:12:57 | 2024-03-01 15:12:59 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
void read(int &x){
x=0;char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch-'0');ch=getchar();}
}
//void read(ll &x){
// x=0;char ch=getchar();
// while(ch<'0'||ch>'9')ch=getchar();
// while(ch>='0'&&ch<='9'){x=(x<<1ll)+(x<<3ll)+(ch-'0');ch=getchar();}
//}
const int N=1e6+10,Q=1e5+10;
int n,m,q;ll a[N];
vector<int>E[N];
int siz[N],top[N],dep[N],father[N],son[N],id[N],rez[N],tot;
void dfs_sp1(int u,int fa){
father[u]=fa;dep[u]=dep[fa]+1;siz[u]=1;
for(int v:E[u]){
if(v==fa)continue;
dfs_sp1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]])son[u]=v;
}
}
void dfs_sp2(int u){
if(son[u]){
int v=son[u];
top[v]=top[u];
id[v]=++tot;rez[id[v]]=v;
dfs_sp2(v);
}
for(int v:E[u]){
if(id[v])continue;
top[v]=v;
id[v]=++tot;rez[id[v]]=v;
dfs_sp2(v);
}
}
namespace ST{
int st[N][20],Log[N];
void init(){
for(int i=1;i<=n;i++)st[i][0]=i;
Log[1]=0;for(int i=2;i<=n;i++)Log[i]=Log[i/2]+1;
for(int j=1;j<20;j++){
for(int i=1;i+(1<<j)-1<=n;i++){
st[i][j]=(a[rez[st[i][j-1]]]>a[rez[st[i+(1<<(j-1))][j-1]]])?st[i][j-1]:st[i+(1<<(j-1))][j-1];
}
}
}
int ask(int l,int r){
int lg=Log[r-l+1];
return (a[rez[st[l][lg]]]>a[rez[st[r-(1<<lg)+1][lg]]])?st[l][lg]:st[r-(1<<lg)+1][lg];
}
}
set<pair<int,int> >now;
int lca(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
x=father[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
return x;
}
void cx(int x,int y){
now.clear();
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
now.insert(make_pair(id[top[x]],id[x]));
x=father[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
now.insert(make_pair(id[x],id[y]));
}
int Get(){
// if(now.empty())cout<<"A";
// cout<<"now:";
// for(auto i=now.begin();i!=now.end();i++){
// cout<<(*i).first<<" "<<(*i).second<<" ";
// }
// cout<<endl;
int mx=ST::ask((*now.begin()).first,(*now.begin()).second);
set<pair<int,int> >::iterator is=now.begin();
for(auto i=now.begin();i!=now.end();i++){
int x=ST::ask((*i).first,(*i).second);
mx=(a[rez[mx]]>a[rez[x]])?mx:x;is=(mx==x)?i:is;
}
pair<int,int>p1={(*is).first,mx-1},p2={mx+1,(*is).second};
now.erase(is);
if(p1.first<=p1.second)now.insert(p1);
if(p2.first<=p2.second)now.insert(p2);
return a[rez[mx]];
}
vector<int>ve;
int get_sum(int lg){
int s=0ll;for(int v:ve)s+=(v>>lg)&1ll;
return s;
}
#undef int
int main(){
#define int long long
read(n);
for(int i=1;i<n;i++){
int x,y;read(x);read(y);
E[x].push_back(y);E[y].push_back(x);
}
for(int i=1;i<=n;i++)read(a[i]);
read(q);
dfs_sp1(1,0);top[1]=1;id[1]=rez[1]=++tot;dfs_sp2(1);
// cout<<"siz:";for(int i=1;i<=n;i++)cout<<siz[i]<<" ";cout<<endl;
// cout<<"son:";for(int i=1;i<=n;i++)cout<<son[i]<<" ";cout<<endl;
// cout<<"dep:";for(int i=1;i<=n;i++)cout<<dep[i]<<" ";cout<<endl;
// cout<<"father:";for(int i=1;i<=n;i++)cout<<father[i]<<" ";cout<<endl;
// cout<<"id:";for(int i=1;i<=n;i++)cout<<id[i]<<" ";cout<<endl;
// cout<<"rez:";for(int i=1;i<=n;i++)cout<<rez[i]<<" ";cout<<endl;
ST::init();
ll lastans=0;
for(int i=1;i<=q;i++){
ll x,y;read(x);read(y);read(m);
x=((x^lastans)%n)+1,y=((y^lastans)%n)+1;
cx(x,y);
ve.clear();ve.shrink_to_fit();
int lg=61;ll ans=0;int sum=0;
// cout<<x<<" "<<y<<" "<<m<<endl;
// cout<<dep[x]+dep[y]-2*dep[lca(x,y)]+1<<endl;
// for(int j=1;~lg&&j<=min(200,m<<6);j++){
for(int j=1;~lg&&j<=min({200ll,m<<6,dep[x]+dep[y]-2*dep[lca(x,y)]+1});j++){
ll x=Get();
// cout<<x<<" "<<lg<<" "<<(x<(1ll<<lg))<<" "<<(1ll<<lg)<<endl;
while(lg>=0ll&&x<(1ll<<lg)){
ans+=(1ll<<lg)*(sum>=m);
// cout<<"$ "<<lg<<" "<<sum<<endl;
// if(sum>=m){
// cout<<"! "<<lg<<" "<<sum<<" ";
// sum=get_sum(lg);
// cout<<sum<<" !\n";
// }
lg--;sum=get_sum(lg);
}
ve.push_back(x);
// cout<<"@\n";
sum+=(x>>lg)&1;
}
while(~lg){
ans+=(1ll<<lg)*(sum>=m);
lg--;sum=get_sum(lg);
}
// cout<<get_sum(4)<<" "<<lg<<endl;
// cout<<endl;
lastans=ans;
printf("%lld\n",ans);
}
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: 38800kb
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:
1873497444985798654
result:
wrong answer 1st numbers differ - expected: '1152921504606846976', found: '1873497444985798654'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #17:
score: 0
Wrong Answer
time: 34ms
memory: 68220kb
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:
4094
result:
wrong answer 1st numbers differ - expected: '2050', found: '4094'
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
Wrong Answer
Test #45:
score: 0
Wrong Answer
time: 1034ms
memory: 290604kb
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:
4 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 10 0 0 0 0 16 0 0 4834 8 0 0 0 4168 0 0 0 0 2 0 0 0 0 4 0 0 0 0 38 4096 0 0 0 516 2 4 4718 0 2 0 0 136006 0 0 0 0 0 0 0 0 2 0 0 0 2 0 4100 0 0 0 0 0 0 524 42 138 0 0 4934 17366 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 586 0 0 62 0 0 0 0 0 0 0 70 0 0 0 0 0 0 0 0 0 0 0 0...
result:
wrong answer 3rd numbers differ - expected: '4', found: '6'
Subtask #8:
score: 0
Skipped
Dependency #1:
0%