QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#594124 | #5439. Meet in the Middle | pengpeng_fudan | WA | 74ms | 43188kb | C++14 | 5.7kb | 2024-09-27 19:31:12 | 2024-09-27 19:31:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll =long long;
const int N=100010;
vector<pair<int,int>> va[N],vb[N];
pair<int,int> st[20][2*N];
int dfn[N],ed[N],cnt=0;int rnk[N];
int depa[N],depb[N];
int tot=0;
int st_pz[N];
int lcab(int u,int v){
if(st_pz[u]>st_pz[v]) swap(u,v);
int l=__lg(st_pz[v]-st_pz[u]+1);
return min(st[l][st_pz[u]],st[l][st_pz[v]-(1<<l)+1]).second;
}
int get_len_b(int u,int v){
return depb[u]+depb[v]-2*depb[lcab(u,v)];
}
struct seg{
int n;
#define az(x) tr[x].az
#define bz(x) tr[x].bz
#define tag(x) tr[x].tag
struct node{
pair<int,int> az;pair<int,int> bz;
int tag;
node operator+ (const node x)const {
node res;res.tag=0;
int len1=(az.first==bz.first?0:az.second+bz.second+get_len_b(rnk[az.first],rnk[bz.first]));
int len2=(x.az.first==x.bz.first?0:x.az.second+x.bz.second+get_len_b(rnk[x.az.first],rnk[x.bz.first]));
int len3=(az.second+x.az.second+get_len_b(rnk[az.first],rnk[x.az.first]));
int len4=(az.second+x.bz.second+get_len_b(rnk[az.first],rnk[x.bz.first]));
int len5=(bz.second+x.az.second+get_len_b(rnk[bz.first],rnk[x.az.first]));
int len6=(bz.second+x.bz.second+get_len_b(rnk[bz.first],rnk[x.bz.first]));
int maxn=max(max(len1,len2),max(max(len3,len4),max(len5,len6)));
if(maxn==len1) {res.az=az,res.bz=bz;}
else if(maxn==len2) {res.az=x.az,res.bz=x.bz;}
else if(maxn==len3) {res.az=az,res.bz=x.az;}
else if(maxn==len4) {res.az=az,res.bz=x.bz;}
else if(maxn==len5) {res.az=bz,res.bz=x.az;}
else if(maxn==len6) {res.az=bz,res.bz=x.bz;}
return res;
}
}tr[4*N];
void spread(int p){
az(p<<1).second+=tag(p);
az(p<<1|1).second+=tag(p);
bz(p<<1).second+=tag(p);
bz(p<<1|1).second+=tag(p);
tag(p<<1)+=tag(p),tag(p<<1|1)+=tag(p);
tag(p)=0;
}
void pushup(int p){
tr[p]=tr[p<<1]+tr[p<<1|1];
}
void build(int p,int l,int r){
tag(p)=0;
if(l==r){tr[p].az=tr[p].bz={l,depa[rnk[l]]};return ;}
int mid=(l+r)>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r);
pushup(p);
}
void intt(int sz){
n=sz;
build(1,1,n);
}
void modify(int p,int l,int r,int L,int R,int num){
if(L<=l&&r<=R){
az(p).second+=num,bz(p).second+=num;
tag(p)+=num;
return ;
}
spread(p);
int mid=(l+r)>>1;
if(mid>=L) modify(p<<1,l,mid,L,R,num);
if(mid<R) modify(p<<1|1,mid+1,r,L,R,num);
pushup(p);
}
void modify(int l,int r,int num){
// cerr<<"!!!"<<tr[4].az.second<<'\n';
if(l>r) return ;
modify(1,1,n,l,r,num);
}
node query(int p,int l,int r,int L,int R){
if(L<=l&&r<=R) return tr[p];
int mid=(l+r)>>1;
if(mid>=L&&mid<R) return query(p<<1,l,mid,L,R)+query(p<<1|1,mid+1,r,L,R);
if(mid>=L) return query(p<<1,l,mid,L,R);
if(mid<R) return query(p<<1|1,mid+1,r,L,R);
return node();
}
node query(int l,int r){
return query(1,1,n,l,r);
}
int ask(int x){
int len1=get_len_b(x,rnk[tr[1].az.first])+tr[1].az.second;
int len2=get_len_b(x,rnk[tr[1].bz.first])+tr[1].bz.second;
// cerr<<x<<' '<<rnk[tr[1].az.first]<<' '<<rnk[tr[1].bz.first]<<' '<<tr[1].az.second<<' '<<tr[1].bz.second<<'\n';
return max(len1,len2);
}
};
seg sg;
void solve(){
int n,q;
cin>>n>>q;
for(int i=1;i<=n-1;i++){
int u,v,w;
cin>>u>>v>>w;
va[u].push_back({v,w});
va[v].push_back({u,w});
}
for(int i=1;i<=n-1;i++){
int u,v,w;
cin>>u>>v>>w;
vb[u].push_back({v,w});
vb[v].push_back({u,w});
}
auto dfsa=[&](auto self,int u,int v,int dp)->void {
dfn[u]=++cnt;rnk[cnt]=u;
depa[u]=dp;
for(auto [i,w]:va[u]){
if(i==v) continue;
self(self,i,u,dp+w);
}
ed[u]=cnt;
};
dfsa(dfsa,1,1,0);
auto dfsb=[&](auto self,int u,int v,int dp)->void {
st[0][++tot]={dp,u};
depb[u]=dp;
st_pz[u]=tot;
for(auto [i,w]:vb[u]){
if(i==v) continue;
self(self,i,u,dp+w);
st[0][++tot]={dp,u};
}
};
dfsb(dfsb,1,1,0);
for(int j=1;j<20;j++){
for(int i=1;i+(1<<j)-1<=tot;i++){
st[j][i]=min(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
sg.intt(n);
vector<vector<pair<int,int>>> qry(n+1);
for(int i=1;i<=q;i++){
int s,e;
cin>>s>>e;
qry[s].push_back({e,i});
}
vector<int> ans(q+1);
auto get_res=[&](auto self,int u,int v)->void {
for(auto [x,num]:qry[u]){
ans[num]=sg.ask(x);
}
for(auto [i,len]:va[u]){
if(i==v) continue;
sg.modify(dfn[i],ed[i],-len);
sg.modify(1,dfn[i]-1,len);
sg.modify(ed[i]+1,n,len);
self(self,i,u);
sg.modify(dfn[i],ed[i],len);
sg.modify(1,dfn[i]-1,-len);
sg.modify(ed[i]+1,n,-len);
}
};
get_res(get_res,1,1);
for(int i=1;i<=q;i++) cout<<ans[i]<<'\n';
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int _ = 1;
while (_--) solve();
return 0;
}
/*
3 0
1 2 1
2 3 2
1 2 2
2 3 1
*/
/*
3 4
1 2 1
2 3 2
1 2 2
2 3 1
1 1
1 2
2 1
2 2
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 23360kb
input:
3 4 1 2 1 2 3 2 1 2 2 2 3 1 1 1 1 2 2 1 2 2
output:
6 4 5 3
result:
ok 4 number(s): "6 4 5 3"
Test #2:
score: 0
Accepted
time: 3ms
memory: 21300kb
input:
2 1 1 2 1 1 2 1 1 1
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 0ms
memory: 19208kb
input:
2 1 1 2 1 1 2 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Wrong Answer
time: 74ms
memory: 43188kb
input:
10000 50000 8101 5996 108427744 5996 7605 870838849 5996 5599 603303696 8101 3006 339377417 8101 6463 442516687 6463 5560 109174079 5560 4063 127596224 3006 1682 947915262 5996 1986 130416311 6463 5455 279771516 6463 2634 516688796 4063 3034 217886863 7605 5092 742375061 5599 2266 193804402 5092 140...
output:
1887098747 910392842 -412069831 1406079888 1607344640 -1857896817 -313112880 1905005455 1279985485 1452327506 -538760081 -660088752 782774064 -890041203 2059683721 -861617418 491233992 1968782617 -1474374947 1103132115 878871384 1564046754 70733138 1610216528 1373762406 995915500 -1263323703 -933273...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '1887098747'