QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#784707 | #2580. 语言 | pwx2009 | 0 | 451ms | 143776kb | C++14 | 3.6kb | 2024-11-26 15:45:58 | 2024-11-26 15:45:59 |
Judging History
answer
#include<cstdio>
#include<vector>
#define ll long long
int read(){
int num=0;
char ch;ch=getchar();
while(ch<48||ch>57)ch=getchar();
while(ch>47&&ch<58){
num=(num<<1)+(num<<3)+(ch^48);
ch=getchar();
}return num;
}
void swap(int &x,int &y){x^=y;y^=x;x^=y;}
const int N=2e5+5;
const int M=N<<1;
int n,m;
int em,e[N<<1],nx[N<<1],ls[N];
void insert(int x,int y){
e[++em]=y;
nx[em]=ls[x];
ls[x]=em;
}
int fir[N],fa[N];
ll dep[N];
int dfn[M],sum;
void dg1(int x,int father){
fa[x]=father;
dep[x]=dep[father]+1;
dfn[++sum]=x,fir[x]=sum;
for(int i=ls[x];i;i=nx[i]){
if(e[i]==father)continue;
dg1(e[i],x);dfn[++sum]=x;
}
}
int f[M][25];
int lo[M],w[25];
int Q(int l,int r){
if(l>r)swap(l,r);
int x=lo[r-l+1];
if(dep[f[l][x]]<dep[f[r-w[x]+1][x]])return f[l][x];
return f[r-w[x]+1][x];
}
struct node{
int x,y;
ll val;
};
std::vector<node>E[N];
int rt[N];
ll ans;
struct Node{
int lson,rson;
int L,R;
ll sum;
ll s;
};
Node v[N<<7];int cnt;
void change(int &x,int l,int r,int p,int val){
if(!x)x=++cnt;
if(l==r){
v[x].s+=val;
if(v[x].s){
v[x].sum=dep[dfn[p]];
v[x].L=v[x].R=p;
}else v[x].sum=0;
return;
}int mid=(l+r)>>1;
if(mid>=p)change(v[x].lson,l,mid,p,val);
else change(v[x].rson,mid+1,r,p,val);
v[x].s=0;
if(!v[x].lson||!v[v[x].lson].s){
v[x].L=v[v[x].rson].L;
v[x].R=v[v[x].rson].R;
v[x].s=v[v[x].rson].s;
v[x].sum=v[v[x].rson].sum;
return;
}
if(!v[x].rson||!v[v[x].rson].s){
v[x].L=v[v[x].lson].L;
v[x].R=v[v[x].lson].R;
v[x].s=v[v[x].lson].s;
v[x].sum=v[v[x].lson].sum;
return;
}
v[x].L=v[v[x].lson].L,v[x].R=v[v[x].rson].R;
v[x].s=v[v[x].lson].s+v[v[x].rson].s;
v[x].sum=v[v[x].lson].sum+v[v[x].rson].sum;
v[x].sum-=2*dep[Q(v[v[x].lson].R,v[v[x].rson].L)];
}
int merge(int x,int y,int l,int r){
if(!x||!y)return x+y;
if(l==r){
v[x].s+=v[y].s;
v[x].sum+=v[y].sum;
if(v[x].s){
v[x].sum=dep[dfn[l]];
v[x].L=v[x].R=l;
}else v[x].sum=0;
return x;
}
int mid=(l+r)>>1;
v[x].lson=merge(v[x].lson,v[y].lson,l,mid);
v[x].rson=merge(v[x].rson,v[y].rson,mid+1,r);
if(!v[x].lson||!v[v[x].lson].s){
v[x].L=v[v[x].rson].L;
v[x].R=v[v[x].rson].R;
v[x].s=v[v[x].rson].s;
v[x].sum=v[v[x].rson].sum;
return x;
}
if(!v[x].rson||!v[v[x].rson].s){
v[x].L=v[v[x].lson].L;
v[x].R=v[v[x].lson].R;
v[x].s=v[v[x].lson].s;
v[x].sum=v[v[x].lson].sum;
return x;
}
v[x].L=v[v[x].lson].L,v[x].R=v[v[x].rson].R;
v[x].s=v[v[x].lson].s+v[v[x].rson].s;
v[x].sum=v[v[x].lson].sum+v[v[x].rson].sum;
v[x].sum-=dep[Q(v[v[x].lson].R,v[v[x].rson].L)];
return x;
}
void dg2(int x,int father){
for(int i=ls[x];i;i=nx[i]){
if(e[i]==father)continue;
dg2(e[i],x);
rt[x]=merge(rt[x],rt[e[i]],1,sum);
}
for(int i=0;i<E[x].size();i++){
change(rt[x],1,sum,fir[E[x][i].x],E[x][i].val);
change(rt[x],1,sum,fir[E[x][i].y],E[x][i].val);
}
if(v[rt[x]].s){
ans+=v[rt[x]].sum-dep[Q(v[rt[x]].L,v[rt[x]].R)];
}
}
int main(){
n=read(),m=read();
for(int i=1,p,q;i<n;i++){
p=read(),q=read();
insert(p,q);
insert(q,p);
}dg1(1,0);lo[0]=-1;
for(int i=1;i<=sum;i++){
f[i][0]=dfn[i];
lo[i]=lo[i>>1]+1;
}
for(int i=0;i<23;i++)w[i]=1<<i;
for(int i=1;i<20;i++)for(int j=1;j+w[i]-1<=sum;j++) {
if(dep[f[j][i-1]]<dep[f[j+w[i-1]][i-1]])f[j][i]=f[j][i-1];
else f[j][i]=f[j+w[i-1]][i-1];
}
for(int i=1,p,q,lca;i<=m;i++){
p=read(),q=read();
lca=Q(fir[p],fir[q]);
E[p].push_back(node{p,q,1});
E[q].push_back(node{p,q,1});
E[lca].push_back(node{p,q,-1});
E[fa[lca]].push_back(node{p,q,-1});
}
dg2(1,0);
printf("%lld",ans/4);
return 0;
}
详细
Pretests
Final Tests
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 16308kb
input:
297 297 281 46 39 260 193 121 50 162 18 15 43 104 154 293 131 121 70 112 247 121 17 59 240 102 277 185 90 121 134 38 46 116 27 127 97 7 33 49 24 297 31 286 12 18 57 121 133 150 183 286 186 194 153 121 117 189 256 121 266 192 106 230 80 249 164 73 211 197 264 4 53 47 203 43 286 69 221 146 214 167 142...
output:
-156364
result:
wrong answer 1st lines differ - expected: '15910', found: '-156364'
Test #2:
score: 0
Wrong Answer
time: 0ms
memory: 16296kb
input:
293 296 286 83 144 202 108 42 70 112 85 29 291 112 14 112 97 292 123 168 247 112 143 112 238 102 174 262 252 185 63 112 137 103 64 112 15 11 282 273 248 1 100 112 159 181 226 20 47 129 258 34 72 160 189 282 50 252 145 175 13 112 250 152 106 267 30 196 22 112 43 104 17 112 105 80 95 177 34 251 259 29...
output:
-146262
result:
wrong answer 1st lines differ - expected: '15234', found: '-146262'
Test #3:
score: 0
Wrong Answer
time: 15ms
memory: 22088kb
input:
4858 4888 3140 3181 1664 1770 4200 493 3671 59 3209 1842 2502 2173 681 2155 2654 3278 4174 2554 3282 587 3337 4086 4287 2326 779 3708 2048 2448 3634 3148 4214 59 2083 59 325 1216 351 878 1497 3495 4642 4242 115 4419 1588 2172 1054 4610 3007 59 4821 2816 2078 1195 782 2610 2559 3187 4576 292 4000 59 ...
output:
-527007090
result:
wrong answer 1st lines differ - expected: '3627298', found: '-527007090'
Test #4:
score: 0
Wrong Answer
time: 6ms
memory: 20328kb
input:
4983 4867 1535 1610 1899 1266 3238 2426 2405 1420 2578 4364 300 2296 4590 254 396 1242 3129 3631 324 3583 3748 1155 2771 1200 4246 4603 2331 1110 2824 1891 2544 1237 3487 3040 3687 2834 109 4636 4687 1200 1649 2420 2008 2926 2516 4852 3440 1551 765 3096 2754 1200 4879 4820 3073 4787 91 1200 2911 257...
output:
-568632265
result:
wrong answer 1st lines differ - expected: '3873876', found: '-568632265'
Test #5:
score: 0
Wrong Answer
time: 233ms
memory: 62088kb
input:
96808 99508 40695 40696 73595 73596 13615 13616 65545 65546 19391 19392 2353 2354 26730 26731 42541 42542 28008 28009 96282 96283 76530 76531 5872 5873 61286 61287 56072 56073 87216 87217 38177 38178 50372 50373 329 330 58557 58558 43629 43630 77425 77426 8017 8018 43507 43508 35913 35914 31123 3112...
output:
-8433546918480
result:
wrong answer 1st lines differ - expected: '907477207', found: '-8433546918480'
Test #6:
score: 0
Wrong Answer
time: 242ms
memory: 62920kb
input:
97092 99840 40695 40696 73595 73596 13615 13616 65545 65546 19391 19392 2353 2354 26730 26731 42541 42542 28008 28009 96282 96283 76530 76531 5872 5873 61286 61287 56072 56073 87216 87217 38177 38178 50372 50373 329 330 58557 58558 43629 43630 77425 77426 8017 8018 43507 43508 35913 35914 31123 3112...
output:
-8459414025869
result:
wrong answer 1st lines differ - expected: '910321604', found: '-8459414025869'
Test #7:
score: 0
Wrong Answer
time: 446ms
memory: 143516kb
input:
99924 97275 24723 56564 44193 73644 54591 22570 56965 55653 91120 48245 25702 41871 77524 67327 80722 7653 87907 54339 86418 76838 41215 78202 48534 59032 44710 32114 38879 34955 17434 99405 37456 39328 66649 35177 76911 54339 40612 7834 70065 54339 29689 38076 48485 60166 50641 57751 97629 94661 47...
output:
-4810287176806
result:
wrong answer 1st lines differ - expected: '1654600973', found: '-4810287176806'
Test #8:
score: 0
Wrong Answer
time: 425ms
memory: 143776kb
input:
99498 96777 19744 18289 4605 93580 43036 31212 42702 45122 16545 73127 70905 51611 27911 61773 48069 83562 95567 11283 66495 45122 65856 15502 26003 58273 46733 45122 7043 7247 80433 55125 67878 88881 20197 45122 39212 80482 25962 45122 62830 45122 40173 39170 14714 82850 24093 66693 50604 61001 866...
output:
-4405026390003
result:
wrong answer 1st lines differ - expected: '1503897488', found: '-4405026390003'
Test #9:
score: 0
Wrong Answer
time: 436ms
memory: 141760kb
input:
99214 98195 8234 36494 37451 75094 31731 33067 19984 36494 52041 11042 4970 37696 26871 43916 87010 25881 37092 8701 75030 89583 4706 75209 24536 4378 37412 69085 88883 17123 79895 36494 95090 79450 95892 73714 61952 77454 81044 8337 68757 11047 73613 36494 60137 16120 41322 47605 49142 24141 79221 ...
output:
-4712566590102
result:
wrong answer 1st lines differ - expected: '1595001829', found: '-4712566590102'
Test #10:
score: 0
Wrong Answer
time: 451ms
memory: 141276kb
input:
98930 97863 31939 63734 42943 69741 55957 86464 76683 63177 19844 89880 92738 35520 42184 7020 98581 89880 73665 19084 91904 89880 32101 31332 16923 89880 64777 72859 39842 59094 18714 87378 5513 21706 54019 89880 16688 80816 1260 35635 83182 52628 32044 61306 26440 39530 77024 69004 20199 64299 434...
output:
-4378505717065
result:
wrong answer 1st lines differ - expected: '1481951671', found: '-4378505717065'