Submission #279716

#TimeUsernameProblemLanguageResultExecution timeMemory
279716infinite_iqDynamic Diameter (CEOI19_diameter)C++14
42 / 100
1231 ms9720 KiB
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 340
#define mp make_pair
#define mid (l+r)/2
#define le node * 2 
#define ri node * 2 + 1 
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
#define all(x) x.begin(), x.end()
#define gc getchar_unlocked
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef pair<double,ll>pdi;
const ll inf=1e18;
const ll Mod=1e9+7;
const ld Pi=acos(-1) ;
int n , q , MX , Last ;
vpi v [100009] ;
pi edges [100009] ;
//
void input () {
        cin >> n >> q >> MX ;
        for ( int i = 0 ; i < n - 1 ; i ++ ) {
                int a , b , c ;
                scanf ("%d%d%d", &a , &b , &c ) ;
                a -- , b -- ;
                edges [i] = { a , b } ;
                v [a] .pb ( { b , c } ) ;
                v [b] .pb ( { a , c } ) ;
        }
}
pair < pi , int >  Query ( int t ) {
        int id , cost ;
        scanf ("%d%d",&id,&cost) ;
        id   = ( id   + Last ) % ( n - 1 ) ;
        cost = ( cost + Last ) % ( MX    ) ;
        int a = edges [id] .fi , b = edges [id] .se ;
        if ( t ) {
                for ( auto &u : v [a] ) {
                        if ( u .fi == b ) {
                                u .se = cost ;
                                break ;
                        }
                }
                for ( auto &u : v [b] ) {
                        if ( u .fi == a ) {
                                u .se = cost ;
                                break ;
                        }
                }
        }
        return { { a , b } , cost } ;
}
// Sub1_2
pi Far ;
int Sub1_2 () {
        return ( n <= 5009 && q <= 5009 ) ;
}
void Dfs1 ( int node , int p , int Crnt ) {
        Far = max ( Far , { Crnt , node } ) ;
        for ( auto u : v [node] ) {
                if ( u .fi == p ) C ;
                Dfs1 ( u .fi , node , Crnt + u .se ) ;
        }
}
int BruteForce () {
        Far = { 0 , 0 } ;
        Dfs1 ( 0 , 0 , 0 ) ;
        Dfs1 ( Far .se , Far .se , 0 ) ;
        return Far .fi ;
}
void Sol1_2 () {
        while ( q -- ) {
                Query (1) ;
                Last = BruteForce () ;
                printf ("%d\n",Last) ;
        }
        exit (0) ;
}
// Sub4 
int Dp [100009] , Mx [100009] , P [100009] ;
int Sub4 () {
        for ( int i = 0 ; i < n - 1 ; i ++ ) {
                int a = edges [i] .fi , b = edges [i] .se ;
                if ( a > b ) {
                        swap ( edges [i] .fi , edges [i] .se ) ;
                }
                a ++ , b ++ ;
                if ( a * 2 != b && a * 2 + 1 != b ) return 0 ;
        }
        return 1 ;
}
void Fill ( int node , int p ) {
        P [node] = p ;
        for ( auto u : v [node] ) {
                if ( u .fi == p ) C ;
                Fill ( u .fi , node ) ;
                Dp [node] = max ( Dp [node] , Dp [u.fi] ) ;
                Dp [node] = max ( Dp [node] , Mx [u.fi] + u .se + Mx [node] ) ;
                Mx [node] = max ( Mx [node] , Mx [u.fi] + u .se ) ;
        }
        Dp [node] = max ( Dp [node] , Mx [node] ) ;
}
void Up ( int node ) {
        Dp [node] = 0 , Mx [node] = 0 ;
        for ( auto u : v [node] ) {
                if ( u .fi == P [node] ) C ;
                Dp [node] = max ( Dp [node] , Dp [u.fi] ) ;
                Dp [node] = max ( Dp [node] , Mx [u.fi] + u .se + Mx [node] ) ;
                Mx [node] = max ( Mx [node] , Mx [u.fi] + u .se ) ;
        }
        if ( P [node] != node )
                Up ( P  [node] ) ;
}
void Sol4 () {
        Fill ( 0 , 0 ) ;
        while ( q -- ) {
                pair < pi , int > Ret  = Query (1) ;
                int a = Ret .fi.fi , b = Ret .fi.se , cost = Ret .se ;
                Up ( a ) ;
                Last =  Dp [0] ;
                printf ("%d\n",Dp [0]);
        }
}
int main () {
        input () ;
        if ( Sub1_2 () ) Sol1_2 () ;
        if ( Sub4   () ) Sol4   () ;
//      if ( Sub3_5 () ) Sol3_5 () ; 
}

Compilation message (stderr)

diameter.cpp: In function 'void Sol4()':
diameter.cpp:137:38: warning: unused variable 'b' [-Wunused-variable]
  137 |                 int a = Ret .fi.fi , b = Ret .fi.se , cost = Ret .se ;
      |                                      ^
diameter.cpp:137:55: warning: unused variable 'cost' [-Wunused-variable]
  137 |                 int a = Ret .fi.fi , b = Ret .fi.se , cost = Ret .se ;
      |                                                       ^~~~
diameter.cpp: In function 'void input()':
diameter.cpp:43:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   43 |                 scanf ("%d%d%d", &a , &b , &c ) ;
      |                 ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp: In function 'std::pair<std::pair<int, int>, int> Query(int)':
diameter.cpp:52:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   52 |         scanf ("%d%d",&id,&cost) ;
      |         ~~~~~~^~~~~~~~~~~~~~~~~~
#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...