Submission #974908

#TimeUsernameProblemLanguageResultExecution timeMemory
974908vivkostovBridges (APIO19_bridges)C++14
100 / 100
2008 ms36188 KiB
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    /*#ifdef ONLINE_JUDGE
    freopen("taxi.in", "r", stdin);
    freopen("taxi.out", "w", stdout);
    #endif
    */
}
struct cell
{
    int a,b,c,use,ti,old;
};
bool cmp(cell a1,cell a2)
{
    if(a1.c==a2.c)return a1.a>a2.a;
    return a1.c>a2.c;
}
int n,m,q,num[1000005],b[1000005],c[1000005],lead[1000005],sz[1000005],otg[1000005],used[1000005];
cell a[1000005],f[1000005],lis[1000005];
stack<pair<int,int>>s;
int get_lead(int a)
{
    while(lead[a]!=a)a=lead[a];
    return a;
}
void prec()
{
    for(int i=1; i<=n; i++)
    {
        lead[i]=i;
        sz[i]=1;
    }
}
int bg;
void add(int a,int b)
{
    a=get_lead(a);
    b=get_lead(b);
    if(a==b)return;
    bg++;
    if(sz[a]<b[sz])swap(a,b);
    sz[a]+=sz[b];
    lead[b]=a;
    pair<int,int>p;
    p.first=a;
    p.second=b;
    s.push(p);
}
void del()
{
    int a=s.top().first,b=s.top().second;
    sz[a]-=sz[b];
    lead[b]=s.top().second;
    s.pop();
}
void read()
{
    cin>>n>>m;
    for(int i=1; i<=m; i++)
    {
        cin>>a[i].a>>a[i].b>>a[i].c;
        a[i].ti=a[i].c;
    }
    cin>>q;
    for(int i=1; i<=q; i++)
    {
        cin>>num[i]>>b[i]>>c[i];
    }
    for(int i=1; i<=q; i+=1000)
    {
        int br=0,br1=0;
        prec();
        for(int j=i; j<=min(q,i+1000); j++)
        {
            if(num[j]==1)
            {
                int o=a[b[j]].ti;
                a[b[j]].use=1;
                a[b[j]].c=c[j];
                br1++;
                lis[br1]=a[b[j]];
                lis[br1].use=b[j];
                lis[br1].ti=j;
                lis[br1].old=o;
            }
            else
            {
                br++;
                f[br].a=-j;
                f[br].b=b[j];
                f[br].c=c[j];
                f[br].ti=j;
            }
        }
        for(int j=1; j<=m; j++)
        {
            if(!a[j].use)
            {
                br++;
                f[br]=a[j];
            }
        }
        sort(f+1,f+br+1,cmp);
        /*cout<<endl;
        for(int j=1; j<=br1; j++)
        {
            cout<<lis[j].a<<" "<<lis[j].b<<" "<<lis[j].c<<" "<<lis[j].use<<" "<<lis[j].ti<<" "<<lis[j].old<<endl;
        }
        for(int i=1; i<=n; i++)
        {
            cout<<lead[i]<<" "<<sz[i]<<" "<<i<<endl;
        }
        cout<<endl;
        */
        for(int j=1; j<=br; j++)
        {
            if(f[j].a>0)
            {
                add(f[j].a,f[j].b);
                /*for(int i=1; i<=n; i++)
                {
                    cout<<lead[i]<<" "<<sz[i]<<" "<<i<<endl;
                }
                cout<<endl;
                */
            }
            else
            {
                //cout<<f[j].c<<endl;
                //cout<<endl;
                for(int i=1; i<=n; i++)
                {
                    //cout<<lead[i]<<" "<<sz[i]<<" "<<i<<endl;
                }
                //cout<<endl;
                for(int h=1;h<=br1;h++)used[lis[h].use]=-1;
                for(int h=1;h<=br1;h++)
                {
                    if(lis[h].c>=f[j].c&&lis[h].ti<f[j].ti)
                    {
                        used[lis[h].use]=1;
                    }
                    else if(lis[h].ti<f[j].ti)
                    {
                        used[lis[h].use]=0;
                    }
                    if(lis[h].ti>f[j].ti&&lis[h].old>=f[j].c&&used[lis[h].use]==-1)
                    {
                        used[lis[h].use]=1;
                    }
                }
                for(int i=1;i<=q;i++)
                {
                    //cout<<used[i]<<" ";
                }
                //cout<<endl;
                bg=0;
                for(int h=1; h<=br1; h++)
                {
                    if(used[lis[h].use]==1)
                    {
                        add(lis[h].a,lis[h].b);
                    }
                }
                otg[f[j].ti]=sz[get_lead(f[j].b)];
                //cout<<get_lead(f[j].b)<<endl;
                //cout<<-f[j].a<<" "<<otg[-f[j].a]<<" "<<f[j].b<<" "<<bg<<" "<<f[j].c<<" "<<f[j].ti<<endl;
                for(int z=1; z<=bg; z++)del();
            }
        }
        for(int i=1; i<=m; i++)
        {
            a[i].use=0;
            lis[i].a=0;
            lis[i].b=0;
            lis[i].c=0;
            lis[i].use=0;
            lis[i].ti=0;
            lis[i].old=0;
            f[i].a=0;
            f[i].b=0;
            f[i].c=0;
            f[i].use=0;
            f[i].ti=0;
            f[i].old=0;
            used[i]=0;
            a[i].ti=a[i].c;
        }
        while(!s.empty())
        {
            del();
        }
    }
    for(int i=1; i<=q; i++)
    {
        if(num[i]==2)cout<<otg[i]<<endl;
    }
}
int main()
{
    speed();
    read();
    return 0;
}
/*
10 9
1 2 1
2 3 2
3 4 3
4 5 4
5 6 5
6 7 6
7 8 7
8 9 8
9 10 9
5
1 3 3
1 3 2
1 3 4
1 3 5
2 5 3
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...