QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#876052 | #8616. Fast Tree Queries | ucup-team6274 | TL | 892ms | 14504kb | C++26 | 3.1kb | 2025-01-30 16:37:54 | 2025-01-30 16:37:55 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
bool Mbg;
using namespace std;
#define vec vector
#define pb push_back
#define eb emplace_back
#define siz(vec) ((int)(vec).size())
#define all(vec) (vec).begin(),(vec).end()
template<class T>
void operator +=(vec<T> &a,T b){a.push_back(b);}
template<class T>
void operator --(vec<T> &a){a.pop_back();}
#define pii pair<int,int>
#define x first
#define y second
#define mp make_pair
#define exc(exp) if(exp)continue;
#define stop(exp) if(exp)break;
#define ret(exp) if(exp)return;
#define deb(var) cerr<<#var<<'='<<(var)<<"; "
#define debl(var) cerr<<#var<<'='<<(var)<<";\n"
#define ins insert
#define era erase
#define lb lower_bound
#define ub upper_bound
#define int long long
#define inf (long long)(1e18)
template<class T>
bool Min(T &x,T y){return x>y?x=y,1:0;}
template<class T>
bool Max(T &x,T y){return x<y?x=y,1:0;}
const int mod=998244353;
void Add(int &x,int y){x=x+y<mod?x+y:x+y-mod;}
void Dec(int &x,int y){x=x>=y?x-y:x-y+mod;}
int fpm(int x,int y){
int ans=1;for(;y;y>>=1,x=1ll*x*x%mod)if(y&1)ans=1ll*x*ans%mod;return ans;
}
int n,q;
vec<int> g[100010];
int fa[100010],hvy[100010],dep[100010],siz[100010],tim,dfn[100010],top[100010];
void dfs(int u,int f){
siz[u]=1;
dep[u]=dep[fa[u]=f]+1;
for(auto v:g[u]){
exc(v==f);
dfs(v,u);
siz[u]+=siz[v];
}
}
void dfs2(int u,int fa){
dfn[u]=++tim;
if(hvy[u]){
top[hvy[u]]=top[u];
dfs(hvy[u],u);
}
for(auto v:g[u]){
exc(v==fa||v==hvy[u]);
top[v]=v;
dfs2(v,u);
}
}
int a[100010];
void add(int l,int r,int x){
for(;l<=r;l++){
a[l]+=x;
}
}
int qry(int l,int r){
int x=0;
for(;l<=r;l++){
x^=a[l];
}
return x;
}
void work(){
cin>>n>>q;
for(int i=1,u,v;i<n;i++){
cin>>u>>v;
g[u]+=v,g[v]+=u;
}
dfs(1,0);
top[1]=1;
dfs2(1,0);
for(int i=1;i<=n;i++){
add(dfn[i],dfn[i],i);
}
while(q--){
char op;
int u,v,x;
cin>>op>>u>>v;
if(op=='?'){
int ans=0;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
ans^=qry(dfn[top[u]],dfn[u]);
u=fa[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
ans^=qry(dfn[u],dfn[v]);
cout<<ans<<'\n';
}else{
cin>>x;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
add(dfn[top[u]],dfn[u],x);
u=fa[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
add(dfn[u],dfn[v],x);
}
}
}
bool Med;
signed main(){
ios::sync_with_stdio(0),
cin.tie(0),cout.tie(0);
int T=1;while(T--)work();
// cerr<<"Time: "<<clock()<<" ms;\n";
// cerr<<"Memory: "<<abs(&Med-&Mbg)/1024.0/1024.0<<" MiB.\n";
}
/*
- CONTINUE, NON-STOPPING, FOR THE FAITH
- START TYPING IF YOU DON'T KNOW WHAT TO DO
- STOP TYPING IF YOU DON'T KNOW WHAT YOU'RE DOING
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3712kb
input:
5 6 1 2 1 3 3 4 3 5 ? 2 5 + 1 4 1 ? 2 5 + 4 5 2 ? 4 5 ? 1 1
output:
5 1 6 2
result:
ok 4 number(s): "5 1 6 2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
100 100 6 74 6 93 7 46 7 78 10 77 11 9 11 19 11 37 11 51 11 65 12 57 13 15 13 81 13 100 14 2 14 31 16 11 16 24 16 43 16 82 18 4 18 8 21 56 24 29 24 96 26 22 27 32 28 59 29 6 29 94 32 33 35 54 35 80 35 88 37 66 37 71 37 84 38 17 39 64 39 92 40 49 43 7 43 13 43 44 43 79 44 35 44 60 44 63 44 73 46 75 4...
output:
106 66 23766 20394 16388 3365 12076 7844 43127 56700 59 34700 24083 1164 24068 18776 3375 17495 21576 63126 11157 108177 63127 99162 105173 10715 110921 18320 19725 30958 120179 81226 107525 15669 21804 31071 63099 8313 191380 232240 84531 89146 173181 5447 160027 228651 98075 32595 109808 221822 11...
result:
ok 51 numbers
Test #3:
score: 0
Accepted
time: 7ms
memory: 6600kb
input:
10000 10000 1 8776 3 1597 3 8333 4 675 4 6993 4 7587 5 3379 5 6733 7 2440 7 6480 7 9506 10 4545 10 6664 12 1476 12 5343 14 1340 14 4167 14 5990 14 9910 15 5118 15 9423 16 8106 17 3856 20 5201 23 1138 24 3431 24 5578 25 251 26 3219 26 4156 26 6668 27 3795 28 6282 28 8196 34 9595 35 3919 36 5893 37 58...
output:
9911 12310 4793 31764 2730 42301 62944 16978 8306 25311 3011 1327 48224 8407 15662 38117 42837 59074 40600 39018 126441 21755 24519 51286 121187 4335 8349 10553 60726 86675 10797 3883 90069 95477 129342 37777 42339 124045 94323 16858 217612 79454 258856 118337 257945 196316 170121 216631 168616 1663...
result:
ok 5012 numbers
Test #4:
score: 0
Accepted
time: 30ms
memory: 9216kb
input:
50000 50000 1 1362 3 3371 3 13803 3 41794 3 47253 5 6227 5 42748 5 47841 6 28509 6 47395 7 8651 8 35909 10 24241 10 31205 10 33574 10 44109 10 48982 12 31361 12 44645 13 42041 15 20480 16 9467 16 11685 16 16024 16 28894 16 30759 19 3485 19 23470 19 24714 22 14239 25 44841 31 20447 35 5378 35 45983 3...
output:
5042 36803 61033 62216 64370 9744 33748 43519 25564 87892 120265 52351 36778 1507 81138 124599 19620 169852 82219 106112 38410 47506 214605 229380 175036 184353 71808 135637 195043 213707 60790 86453 31176 26740 107180 117349 64903 55397 245841 33362 73881 227391 227333 52027 217335 146795 51029 100...
result:
ok 24888 numbers
Test #5:
score: 0
Accepted
time: 71ms
memory: 14504kb
input:
100000 100000 4 6907 4 36895 4 37669 4 43936 4 99352 5 70501 10 29516 11 2844 11 77315 13 67782 13 72511 14 17273 14 52833 16 97869 19 29567 20 150 20 22843 20 40110 20 55549 20 70574 22 19544 25 43083 26 6781 28 16286 31 54186 33 6987 33 30861 33 98931 35 1140 35 21137 35 26681 35 59133 35 68440 35...
output:
81605 93578 30029 52473 57143 63629 77074 53264 71956 49059 16958 120352 93046 80080 67928 99557 182425 198595 36952 180370 156161 98094 56273 28969 34287 146511 31057 171228 128057 13930 81021 69266 100431 118091 249004 63451 147951 223183 255240 173677 30490 137681 135801 8441 205904 32855 66449 2...
result:
ok 50026 numbers
Test #6:
score: 0
Accepted
time: 892ms
memory: 14396kb
input:
100000 100000 1 484 1 23605 4 29115 5 78235 6 41049 7 17427 7 59118 7 73639 8 11499 8 21824 11 1488 11 34365 11 46659 15 1670 15 18862 16 88552 17 6551 17 18453 17 22090 18 90500 19 7369 19 40175 19 88031 19 89418 20 82312 22 36035 22 37511 22 90587 24 26545 25 99359 26 9713 26 19360 26 23939 27 145...
output:
118339 49557 8069 129295 8844 117076 73582 44568 123694 84080 220459 65210 20353 78112 178132 238977 76360 242837 177610 55964 146913 258846 46783 101598 210987 130886 215693 137264 91070 47636 222290 212175 70753 64509 143987 258750 63355 124572 249779 144712 237288 64472 32941 26055 220986 221734 ...
result:
ok 50004 numbers
Test #7:
score: -100
Time Limit Exceeded
input:
100000 100000 2 96325 3 2625 3 19305 3 23547 3 33880 4 34351 4 80895 6 81122 7 8449 8 33132 9 6805 10 66297 12 40279 15 97995 17 28489 17 58943 17 63155 18 16755 19 36111 19 48497 20 73429 21 58028 22 23782 23 23210 25 43059 25 98782 28 96774 29 13371 30 18348 30 33119 31 91098 32 20761 34 19579 34 ...
output:
127926 27191 52198 17494 136 89133 88302 171 53017 111240 104493 80525 30736 39514 30186 242564 127960 116595 77217 181066 78202 117647 65124 5801 23054 231346 224212 101596 208931 56567 153986 225153 98732 39206 196696 201593 49107 59019 227567 23907 240224 47177 110143 93337 12687 16281 91369 7419...