Submission #857059

# Submission time Handle Problem Language Result Execution time Memory
857059 2023-10-05T10:40:27 Z Tudy006 Traffickers (RMI18_traffickers) C++14
0 / 100
1629 ms 14916 KB
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 3e4;
const int LMAX = 20;
const int QMAX = 5e4;

vector <int> edges[NMAX + 1];

int aib[LMAX + 1][2 * NMAX + 1];

struct node {
    int start;
    int finish;
    int jump;
    int p;
    int d;
} info[NMAX + 1];

void update( int l, int c, int x ) {
    for ( int i = l; i <= LMAX; i += ( i & -i ) ) {
        for ( int j = c; j <= NMAX; j += ( j & -j ) ) {
            aib[i][j] += x;
        }
    }
}

int sum( int l, int c ) {
    int s = 0;
    for ( int i = l; i > 0; i -= ( i & -i ) ) {
        for ( int j = c; j > 0; j -= ( j & -j ) ) {
            s += aib[i][j];
        }
    }
    return s;
}

int t = 0;
void dfs( int node, int p = 1 ) {
    info[node].p = p;
    info[node].d = info[p].d + 1;
    info[node].start = ++t;
    for ( auto vec : edges[node] ) {
        if ( vec == p ) continue;
        dfs( vec, node );
        if ( info[p].d - info[info[p].jump].d == info[info[p].jump].d - info[info[info[p].jump].jump].d ) {
            info[vec].jump = info[info[p].jump].jump;
        } else {
            info[vec].jump = p;
        }
    }
    info[node].finish = ++t;
}

bool isAncestor( int u, int v ) {
    return ( info[u].start <= info[v].start && info[v].finish <= info[u].finish );
}
int LCA( int u, int v ) {
    while ( !isAncestor( u, v ) ) {
        if ( !isAncestor( info[u].jump, v ) ) {
             u = info[u].jump;
        } else {
            u = info[u].p;
        }
    }
    return u;
}
int dist( int u, int v, int lca ) {
    return info[u].d + info[v].d - 2 * info[lca].d + 1;
}
struct query {
    int tip;
    int u;
    int v;
    int t1;
    int t2;
    int lca;
};

int sumPath( int u, int v, int l, int lca ) {
    int s = sum( l, info[u].start ) + sum( l, info[v].start ) - sum( l, info[lca].start );
    if ( lca == 1 ) {
        return s;
    }
    return s - sum( l, info[info[lca].p].start );
}
long long queryPath( int u, int v, int l, int lca, int t ) {
//    cerr << "For the query: " << u << ' ' << v << ' ' << t << ' ' << l << ": " << sumPath( u, v, l, lca ) << ' ' << sumPath( u, v, t % l, lca ) << endl;
    return (long long)(t / l) * sumPath( u, v, l, lca ) + sumPath( u, v, t % l, lca );
}
void updatePath( int u, int v, int lca ) {
    int d = dist( u, v, lca ), tLca = dist( u, lca, lca );
    for ( int t = 1; u != lca; u = info[u].p ) {
        update( t, info[u].start, +1 );
        update( t, info[u].finish, -1 );
//        cerr << u << ": " << t << endl;
        t ++;
    }
    for ( int t = d; v != lca; v = info[v].p ) {
        update( t, info[v].start, +1 );
        update( t, info[v].finish, -1 );
//        cerr << v << ": " << t << endl;
        t --;
    }
//    cerr << lca << ": " << tLca << endl;
    update( tLca, info[lca].start, +1 );
    update( tLca, info[lca].finish, -1 );
}

long long answer[QMAX + 1];

int main() {
    ios_base::sync_with_stdio( false );
    cin.tie( NULL );
    cout.tie( NULL );
//    ifstream cin( "Traffickers.in");
//    ofstream cout( "traffickers.out" );
    int n, k, q;
    cin >> n;
    for ( int i = 1, a, b; i < n; i ++ ) {
        cin >> a >> b;
        edges[a].push_back( b );
        edges[b].push_back( a );
    }
    info[1].jump = 1;
    dfs( 1 );
    vector <query> v;
    cin >> k;
    for ( int i = 1, a, b; i <= k; i ++ ) {
        cin >> a >> b;
        v.push_back( (query){1, a, b, 0, 0, LCA(a, b)} );
    }
    cin >> q;
    for ( int i = 1, a, b, tip; i <= q; i ++ ) {
        cin >> tip >> a >> b;
        if ( tip == 3 ) {
            int t1, t2;
            cin >> t1 >> t2;
            v.push_back( (query){tip, a, b, t1, t2, LCA(a, b)} );
        } else {
            v.push_back( (query){tip, a, b, 0, 0, LCA(a, b)} );
        }
    }

    int cnt;
    for ( int l = 3; l <= 3; l ++ ) {
        for ( int i = 1; i <= LMAX; i ++ ) {
            for ( int j = 1; j <= 2 * NMAX; j ++ ) {
                aib[i][j] = 0;
            }
        }
        cnt = 0;
        for ( auto p : v ) {
            if ( p.tip == 3 ) {
                answer[++cnt] += queryPath( p.u, p.v, l, p.lca, p.t2 + 1 ) - queryPath( p.u, p.v, l, p.lca, p.t1 );
            } else {
                if ( dist( p.u, p.v, p.lca ) == l ) {
                    updatePath( p.u, p.v, p.lca );
                }
            }
        }
    }
    for ( int i = 1; i <= cnt; i ++ ) {
        cout << answer[i] << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 6748 KB Output isn't correct
2 Incorrect 2 ms 7004 KB Output isn't correct
3 Incorrect 2 ms 7028 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 8028 KB Output isn't correct
2 Incorrect 11 ms 7956 KB Output isn't correct
3 Incorrect 20 ms 8532 KB Output isn't correct
4 Incorrect 16 ms 8280 KB Output isn't correct
5 Incorrect 13 ms 8080 KB Output isn't correct
6 Incorrect 15 ms 8024 KB Output isn't correct
7 Incorrect 16 ms 8204 KB Output isn't correct
8 Incorrect 21 ms 8292 KB Output isn't correct
9 Incorrect 47 ms 8408 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 333 ms 13852 KB Output isn't correct
2 Incorrect 483 ms 14780 KB Output isn't correct
3 Incorrect 593 ms 14756 KB Output isn't correct
4 Incorrect 116 ms 13240 KB Output isn't correct
5 Incorrect 48 ms 13504 KB Output isn't correct
6 Incorrect 515 ms 14812 KB Output isn't correct
7 Incorrect 1629 ms 14916 KB Output isn't correct
8 Incorrect 1553 ms 14452 KB Output isn't correct