Submission #6300843


Source Code Expand

#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <tuple>
using namespace std;

#include <sys/mman.h>
static char *_in_buf = 
(char*)mmap(NULL, 1 << 26, PROT_READ, MAP_PRIVATE, fileno(stdin), 0);
inline int read() {
  int p = 0, v = 0;
  while (*_in_buf < 48) (*_in_buf ++ == 45) && (v = 1);
  while (*_in_buf > 47 && *_in_buf < 58) p = p * 10 + *_in_buf ++ - 48;
  return v ? -p : p;
}

static char out_buf[100000];
static char *outf = out_buf;
inline void flush() {
  if (outf != out_buf)
    fwrite(out_buf, outf - out_buf, 1, stdout), outf = out_buf;
}
inline void Putchar(char x) {
  *outf ++ = x;
  (outf == out_buf + 100000) && (flush(), 0);
}

int n, q, ans[100005], dis[100005];
vector <tuple <int, int, int> > g[100005];

int par[100005], sz[100005];
int son[100005], dep[100005];
int ind[100005], top[100005], indx;
void dfs1(int u, int parent, int depth) {
  dep[u] = depth, par[u] = parent, sz[u] = 1;
  for (int i = 0; i < (int)g[u].size(); ++ i) {
    int v = get <0> (g[u][i]);
    if (v == parent) continue;
    dfs1(v, u, depth + 1), sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) son[u] = v;
  }
}
void dfs2(int u, int topv) {
  top[u] = topv, ind[u] = ++ indx;
  if (son[u]) dfs2(son[u], topv);
  for (int i = 0; i < (int)g[u].size(); ++ i) {
    int v = get <0> (g[u][i]);
    if (v == par[u] || v == son[u]) continue;
    dfs2(v, v);
  }
}
inline int get_lca(int u, int v) {
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]])
      swap(u, v);
    u = par[top[u]];
  }
  if (dep[u] < dep[v]) return u;
  return v;
}

int dist[100005];
void dfs3(int u, int parent) {
  for (int i = 0; i < (int)g[u].size(); ++ i) {
    int v = get <0>(g[u][i]), w = get <2>(g[u][i]);
    if (v == parent) continue;
    dist[v] = dist[u] + w, dfs3(v, u);
  }
}

vector <tuple <int, int, int> > qry[100005];
int d[100005], cnt[100005];
void dfs4(int u, int parent) {
  for (int i = 0; i < (int)g[u].size(); ++ i) {
    int v = get <0>(g[u][i]), c = get <1> (g[u][i]), w = get <2>(g[u][i]);
    if (v == parent) continue;
    d[c] += w, cnt[c] ++;
    dfs4(v, u);
    d[c] -= w, cnt[c] --;
  }
  for (int i = 0; i < (int)qry[u].size(); ++ i) {
    int id = get <0>(qry[u][i]), c = get <1>(qry[u][i]);
    int t = get <2>(qry[u][i]), nowd = dis[id];
    int x = d[c], y = nowd * cnt[c];
    ans[id] += (y - x) * t;
  }
}

int main() {
  n = read(), q = read();
  for (int i = 1; i < n; ++ i) {
    int u = read(), v = read();
    int c = read(), d = read();
    g[u].emplace_back(v, c, d);
    g[v].emplace_back(u, c, d);
  }
  dfs1(1, 0, 1);
  dfs2(1, 1);
  dfs3(1, 0);
  for (int i = 1; i <= q; ++ i) {
    int c = read(); dis[i] = read();
    int u = read(), v = read();
    int lca = get_lca(u, v);
    ans[i] = dist[u] + dist[v] - 2 * dist[lca];
    qry[u].emplace_back(i, c, 1);
    qry[v].emplace_back(i, c, 1);
    qry[lca].emplace_back(i, c, -2);
  }
  dfs4(1, 0);
  for (int i = 1; i <= q; ++ i) {
    register char stk[20], top = 0;
    while (ans[i]) stk[top ++] = ans[i] % 10, ans[i] /= 10;
    while (top --) Putchar(stk[top] + 48);
    Putchar('\n');
  }
  flush();
  return 0;
}

Submission Info

Submission Time
Task F - Colorful Tree
User libra9z
Language C++14 (GCC 5.4.1)
Score 600
Code Size 3257 Byte
Status AC
Exec Time 91 ms
Memory 25400 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 1
AC × 12
Set Name Test Cases
Sample a01
All a01, b02, b03, b04, b05, b06, b07, b08, b09, b10, b11, b12
Case Name Status Exec Time Memory
a01 AC 3 ms 7680 KB
b02 AC 3 ms 7680 KB
b03 AC 12 ms 14128 KB
b04 AC 78 ms 24632 KB
b05 AC 84 ms 25400 KB
b06 AC 52 ms 18612 KB
b07 AC 53 ms 19380 KB
b08 AC 71 ms 23988 KB
b09 AC 69 ms 22640 KB
b10 AC 91 ms 20088 KB
b11 AC 83 ms 19448 KB
b12 AC 81 ms 19512 KB