답안 #332169

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
332169 2020-12-01T15:13:53 Z Vladth11 Traffickers (RMI18_traffickers) C++14
15 / 100
878 ms 59500 KB
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debug_with_space(x) cerr << #x << " " << x << " "

using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <ll, pii> piii;

const ll NMAX = 30001;
const ll INF = (1 << 30);
const ll MOD = 1000000007;
const ll BLOCK = 101;
const ll nr_of_bits = 14;
const ll delta = 0.0000001;

int n;

vector <int> v[NMAX];
int dp[NMAX][nr_of_bits + 1];
int pz[NMAX];
int sf[NMAX];
int stamp;
int fnw[NMAX][21][21];

void Precalc(){
    for(int j = 1; j <= nr_of_bits; j++){
        for(int i = 1; i <= n; i++){
            dp[i][j] = dp[dp[i][j - 1]][j - 1];
        }
    }
}

void DFS(int node, int p){
    dp[node][0] = p;
    pz[node] = ++stamp;
    for(auto x : v[node]){
        if(x == p)
            continue;
        DFS(x, node);
    }
    sf[node] = stamp;
}

bool isParent(int a, int b){
    return (pz[a] <= pz[b] && sf[b] <= sf[a]);
}

int LCA(int a, int b){
    int r = a, pas = nr_of_bits;
    while(pas >= 0){
        if(dp[r][pas] != 0 && !isParent(dp[r][pas], b))
            r = dp[r][pas];
        pas--;
    }
    if(!isParent(r, b))
        r = dp[r][0];
    return r;
}

int query(int node, int t){
    int sum = 0;
    for(int i = 1; i <= 20; i++){
        int rest = t % i + 1;
        int cnt = 0;
        for(int j = pz[node]; j > 0; j -= j&(-j)){
            for(int k = rest; k > 0; k -= k&(-k)){
                cnt += fnw[j][i][k];
            }
        }
        sum += cnt * (t / i);
        int cnt2 = 0;
         for(int j = pz[node]; j > 0; j -= j&(-j)){
            for(int k = i; k > 0; k -= k&(-k)){
                cnt2 += fnw[j][i][k];
            }
        }
        cnt2 -= cnt;
        sum += (t / i - 1) * cnt2;
    }
    return sum;
}

void baga(int nod, int rest, int imp, int val){
    for(int i = nod; i <= n; i += i&(-i)){
        for(int j = rest; j <= imp; j += j&(-j))
            fnw[i][imp][j] += val;
    }
}

void adauga(int u, int v, int c){
    vector <int> up;
    while(!isParent(u, v)){
        up.push_back(u);
        u = dp[u][0];
    }
    vector <int> idk;
    while(v != u){
        idk.push_back(v);
        v = dp[v][0];
    }
    idk.push_back(v);
    reverse(idk.begin(), idk.end());
    for(auto x : idk)
        up.push_back(x);
    idk.clear();
    for(int i = 0; i < up.size(); i++){
        baga(pz[up[i]], i + 1, up.size(), c);
        baga(sf[up[i]] + 1, i + 1, up.size(), -c);
    }
}

int sol(int u, int v, int t){
    int lca = LCA(u, v);
    return query(u, t) + query(v, t) - query(lca, t) - query(dp[lca][0], t);
}

int main() {
    
    int i;
    cin >> n;
    for(i = 1; i <= n - 1; i++){
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    DFS(1, 0);
    Precalc();
    int k;
    cin >> k;
    while(k--){
        int a, b;
        cin >> a >> b;
        adauga(a, b, 1);
    }
    int q;
    cin >> q;
    while(q--){
        int t;
        cin >> t;
        if(t == 1){
            int a, b;
            cin >> a >> b;
            adauga(a, b, 1);
        }else if(t == 2){
            int a, b;
            cin >> a >> b;
            adauga(a, b, -1);
        }else{
            int a, b, t1, t2;
            cin >> a >> b >> t1 >> t2;
            cout << sol(a, b, t2) - sol(a, b, t1 - 1) << "\n";
        }
    }
    return 0;
}

Compilation message

traffickers.cpp: In function 'void adauga(int, int, int)':
traffickers.cpp:107:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |     for(int i = 0; i < up.size(); i++){
      |                    ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1132 KB Output is correct
2 Correct 7 ms 2924 KB Output is correct
3 Correct 7 ms 2924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 86 ms 19716 KB Output isn't correct
2 Incorrect 92 ms 17900 KB Output isn't correct
3 Incorrect 89 ms 19180 KB Output isn't correct
4 Incorrect 87 ms 19820 KB Output isn't correct
5 Incorrect 95 ms 19692 KB Output isn't correct
6 Incorrect 103 ms 19776 KB Output isn't correct
7 Incorrect 88 ms 19436 KB Output isn't correct
8 Incorrect 83 ms 19820 KB Output isn't correct
9 Incorrect 76 ms 19948 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 845 ms 58872 KB Output isn't correct
2 Incorrect 873 ms 59372 KB Output isn't correct
3 Incorrect 834 ms 59100 KB Output isn't correct
4 Incorrect 854 ms 58732 KB Output isn't correct
5 Incorrect 878 ms 58528 KB Output isn't correct
6 Incorrect 838 ms 59500 KB Output isn't correct
7 Incorrect 736 ms 59500 KB Output isn't correct
8 Incorrect 727 ms 59372 KB Output isn't correct