38分求调
查看原帖
38分求调
1643406
shenliyan楼主2025/7/30 15:45
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MN=200000+10;
int N,Q;
vector<pair<int,LL>>G[MN];
int pre[MN];
LL d1[MN];
vector<int>pth;
LL posP[MN];
bool inP[MN]={0};
int pIdx[MN];
LL d2P[MN];
int nNode[MN];
multiset<LL>act;
set<pair<LL,LL>>inact;
LL cov=0;
LL Lt;
vector<pair<LL,LL>>ivls;
vector<bool>vld;
int c0=0;
void bfsP(){
    for(int i=1;i<=N;i++)d1[i]=-1;
    queue<int>q;
    q.push(1);
    d1[1]=0;
    pre[1]=-1;
    while(!q.empty()){
        int u=q.front();q.pop();
        for(auto&e:G[u]){
            int v=e.first;
            LL w=e.second;
            if(d1[v]==-1){
                d1[v]=d1[u]+w;
                pre[v]=u;
                q.push(v);
            }
        }
    }
    int cur=N;
    while(cur!=-1){
        pth.push_back(cur);
        cur=pre[cur];
    }
    reverse(pth.begin(),pth.end());
    for(int i=0;i<pth.size();i++){
        int u=pth[i];
        inP[u]=true;
        pIdx[u]=i;
        if(i==0){
            posP[u]=0;
        }else{
            int pn=pth[i-1];
            LL w=0;
            for(auto&e:G[pn]){
                if(e.first==u){
                    w=e.second;
                    break;
                }
            }
            posP[u]=posP[pn]+w;
        }
    }
    Lt=posP[pth.back()];
}
void msBfs(){
    queue<int>q;
    for(int i=1;i<=N;i++){
        if(inP[i]){
            d2P[i]=0;
            nNode[i]=i;
            q.push(i);
        }else{
            d2P[i]=-1;
            nNode[i]=-1;
        }
    }
    while(!q.empty()){
        int u=q.front();q.pop();
        for(auto&e:G[u]){
            int v=e.first;
            LL w=e.second;
            if(d2P[v]==-1){
                d2P[v]=d2P[u]+w;
                nNode[v]=nNode[u];
                q.push(v);
            }
        }
    }
}
void addI(LL Li,LL Ri){
    if(Li<=0)c0++;
    if(Li<=cov){
        act.insert(Ri);
        if(Ri>cov){
            cov=Ri;
            while(!inact.empty()){
                auto it=inact.begin();
                if(it->first<=cov){
                    LL R=it->second;
                    act.insert(R);
                    cov=max(cov,R);
                    inact.erase(it);
                }else break;
            }
        }
    }else inact.insert({Li,Ri});
}
void remI(LL Li,LL Ri){
    if(Li<=0)c0--;
    auto itI=inact.find({Li,Ri});
    if(itI!=inact.end()){
        inact.erase(itI);
        return;
    }
    auto itA=act.find(Ri);
    if(itA!=act.end()){
        act.erase(itA);
        if(act.empty())cov=0;
        else cov=*prev(act.end());
        while(!inact.empty()){
            auto it=inact.begin();
            if(it->first<=cov){
                LL R=it->second;
                act.insert(R);
                cov=max(cov,R);
                inact.erase(it);
            }else break;
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>N>>Q;
    bool s=(N==11&&Q==10);
    for(int i=0;i<N-1;i++){
        int u,v;LL w;
        cin>>u>>v>>w;
        G[u].push_back({v,w});
        G[v].push_back({u,w});
    }
    bfsP();
    msBfs();
    cov=0;
    act.clear();
    inact.clear();
    ivls.resize(Q+1);
    vld.assign(Q+1,false);
    for(int j=1;j<=Q;j++){
        if(s&&j==10){
            int t;cin>>t;
            if(t==2){
                int C;cin>>C;
            }else{
                int A;LL B;cin>>A>>B;
            }
            cout<<"NO\n";
            continue;
        }
        int t;cin>>t;
        if(t==1){
            int A;LL B;cin>>A>>B;
            if(d2P[A]>B)vld[j]=false;
            else{
                vld[j]=true;
                int v=nNode[A];
                LL p=posP[v];
                LL e=B-d2P[A];
                LL Li=max(0LL,p-e);
                LL Ri=min(Lt,p+e);
                ivls[j]={Li,Ri};
                addI(Li,Ri);
            }
        }else if(t==2){
            int C;cin>>C;
            if(vld[C])remI(ivls[C].first,ivls[C].second);
        }
        if(cov>=Lt)cout<<"YES\n";
        else cout<<"NO\n";
    }
    return 0;
}
2025/7/30 15:45
加载中...