Submission #52517
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | #include <bits/stdc++.h> using namespace std; template < typename G > struct DoublingLowestCommonAncestor { const int LOG; vector< int > dep; const G &g; vector< vector< int > > table; DoublingLowestCommonAncestor( const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) { table.assign(LOG, vector< int >(g.size(), -1)); } void dfs( int idx, int par, int d) { table[0][idx] = par; dep[idx] = d; for (auto &to : g[idx]) { if (to != par) dfs(to, idx, d + 1); } } void build() { dfs(0, -1, 0); for ( int k = 0; k + 1 < LOG; k++) { for ( int i = 0; i < table[k].size(); i++) { if (table[k][i] == -1) table[k + 1][i] = -1; else table[k + 1][i] = table[k][table[k][i]]; } } } int query( int u, int v) { if (dep[u] > dep[v]) swap(u, v); for ( int i = LOG - 1; i >= 0; i--) { if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v]; } if (u == v) return u; for ( int i = LOG - 1; i >= 0; i--) { if (table[i][u] != table[i][v]) { u = table[i][u]; v = table[i][v]; } } return table[0][u]; } }; #define BOUND 300 #define int long long int k; vector<pair< int , int > > graph[100010]; vector<vector< int > > tree(100010); vector<pair< int , pair< int , int > > > query[2000]; int dist[100010] = {}; int kdist[100010] = {}; bool used[100010] = {}; int parent[100010] = {}; void makeTree( int idx){ used[idx] = true ; for ( int i = 0;i < graph[idx].size();i++){ int ko = graph[idx][i].first; if (!used[ko]){ dist[ko] = graph[idx][i].second; tree[idx].push_back(ko); parent[ko] = idx; makeTree(ko); } } return ; } vector<pair< int , pair< int , int > > > subTree[2000]; int ditbl[2000][2000] = {}; int ddtbl[2000][2000] = {}; int cidx[100010]; int cp = 1; int uidx[100010]; void makeSubTree( int idx, int par, int d, int o, int di){ if ((dist[idx] + o) % k != 0){ d += dist[idx] + o; } bool flag = false ; if (uidx[idx]){ if (par != idx){ cidx[idx] = cp; subTree[cidx[idx]].clear(); if (parent[idx] != par){ subTree[cidx[idx]].push_back(make_pair(cidx[par], make_pair(-1, dist[idx]+o))); subTree[cidx[par]].push_back(make_pair(cidx[idx], make_pair(-1, di))); } else { int ki, oi; ki = subTree[cidx[par]].size(); oi = subTree[cidx[idx]].size(); subTree[cidx[idx]].push_back(make_pair(cidx[par], make_pair(ki, dist[idx]+o))); subTree[cidx[par]].push_back(make_pair(cidx[idx], make_pair(oi, di))); } ditbl[cidx[idx]][cidx[par]] = d; ditbl[cidx[par]][cidx[idx]] = d; cp++; d = 0; par = idx; } di = kdist[idx]; flag = true ; } for ( int i = 0;i < tree[idx].size();i++){ makeSubTree(tree[idx][i], par, d, kdist[idx], flag ? di+dist[tree[idx][i]] : di); } } int bfs( int s, int t){ s = cidx[s]; t = cidx[t]; bool used[2000] = {}; queue<pair< int , int > > que; que.push(make_pair(s, 0)); used[s] = 1; while (!que.empty()){ int idx = que.front().first; int cost = que.front().second; que.pop(); if (idx == t){ return cost; } used[idx] = 1; for ( int i = 0;i < subTree[idx].size();i++){ int next = subTree[idx][i].first; int ncost = ditbl[idx][next]; if (!used[next]){ que.push(make_pair(next, ncost+cost)); } } } return 0; } signed main(){ ios::sync_with_stdio( false ); cin.tie(0); int n; cin >> n >> k; int a, b, c; for ( int i = 0;i < n-1;i++){ cin >> a >> b >> c; graph[a].push_back(make_pair(b, c)); graph[b].push_back(make_pair(a, c)); } int Q, q; cin >> Q; string str; for ( int i = 0;i < Q;i++){ cin >> str >> b >> c; //send : 1 //add : 2 query[i/BOUND].push_back(make_pair(str[0] == 's' ? 1 : 2, make_pair(b, c))); } q = (Q+BOUND-1)/BOUND; makeTree(0); DoublingLowestCommonAncestor<vector<vector< int > > > d(tree); d.build(); for ( int p = 0;p < q;p++){ subTree[0].clear(); cidx[0] = 0; memset (uidx, false , sizeof (uidx)); uidx[0] = true ; cp = 1; for ( int i = 0;i < query[p].size();i++){ if (query[p][i].first == 1){ uidx[query[p][i].second.first] = true ; uidx[query[p][i].second.second] = true ; uidx[d.query(query[p][i].second.first, query[p][i].second.second)] = true ; } else { uidx[query[p][i].second.first] = true ; } } makeSubTree(0, 0, 0, 0, 0); for ( int i = 0;i < query[p].size();i++){ a = query[p][i].second.first; b = query[p][i].second.second; if (query[p][i].first == 1){ cout << bfs(a, b) << endl; } else { if (a != 0) dist[a] += b; kdist[a] += b; a = cidx[a]; for ( int j = 0;j < subTree[a].size();j++){ int old = subTree[a][j].second.second; if (old % k == 0) old = 0; ditbl[a][subTree[a][j].first] -= old; ditbl[subTree[a][j].first][a] -= old; subTree[a][j].second.second += b; if (subTree[a][j].second.first != -1){ subTree[subTree[a][j].first][subTree[a][j].second.first].second.second += b; } int now = subTree[a][j].second.second; if (now % k == 0) now = 0; ditbl[a][subTree[a][j].first] += now; ditbl[subTree[a][j].first][a] += now; } } } } return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 0965 - ネットワークの課金システム |
ユーザー名 | ei1719 |
投稿日時 | 2019-08-09 01:16:44 |
言語 | C++14 |
状態 | Accepted |
得点 | 17 |
ソースコード長 | 6457 Byte |
最大実行時間 | 2035 ms |
最大メモリ使用量 | 48416 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 17 / 17 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in1.txt | AC | 37 ms | 18260 KB |
1
|
in2.txt | AC | 35 ms | 21028 KB |
1
|
in3.txt | AC | 34 ms | 21056 KB |
1
|
in4.txt | AC | 26 ms | 21092 KB |
1
|
in5.txt | AC | 26 ms | 21124 KB |
1
|
in6.txt | AC | 26 ms | 21148 KB |
1
|
in7.txt | AC | 34 ms | 21164 KB |
1
|
in8.txt | AC | 37 ms | 23208 KB |
1
|
in9.txt | AC | 70 ms | 28688 KB |
1
|
in10.txt | AC | 79 ms | 25976 KB |
1
|
in11.txt | AC | 107 ms | 27560 KB |
1
|
in12.txt | AC | 196 ms | 31956 KB |
1
|
in13.txt | AC | 273 ms | 32908 KB |
1
|
in14.txt | AC | 479 ms | 32484 KB |
1
|
in15.txt | AC | 419 ms | 28488 KB |
1
|
in16.txt | AC | 1084 ms | 41608 KB |
1
|
in17.txt | AC | 838 ms | 37360 KB |
1
|
in18.txt | AC | 2035 ms | 47672 KB |
1
|
in19.txt | AC | 1163 ms | 41648 KB |
1
|
in20.txt | AC | 481 ms | 36552 KB |
1
|
in21.txt | AC | 795 ms | 48416 KB |
1
|