Submission #52492


ソースコード

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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define swap(i,j) { int tmp = i; i = j; j = tmp; }
int k;
struct Data{
int num;
int cost;
int d;
bool x;
Data(int a, int b, bool xx){
num = a;
cost = 0;
d = b;
x = xx;
}
Data(){
num = 0;
cost = 0;
d = 0;
x = false;
}
};
template <class type> class SegmentTree{
public:
int size;
vector<type> tree;
type def;
type (*marge)(type, type);
SegmentTree(){
return;
}
void init(int n, type d, type (*m)(type, type)){
size = 1;
def = d;
marge = m;
n++;
while(size < n){
size *= 2;
}
tree.resize(2*size);
for(int i = 0;i < size;i++){
tree[size-1+i] = def;
}
for(int i = size-2;i >= 0;i--){
tree[i] = marge(tree[i*2+1], tree[i*2+2]);
}
return;
}
void update(int i, type val){
i = size-1+i;
tree[i].num += val.num;
tree[i].d += val.d;
tree[i].cost = 0;
tree[i].x |= val.x;
while(i > 0){
i = (i-1)/2;
tree[i] = marge(tree[i*2+1], tree[i*2+2]);
}
return; }
type get(int a, int b, int k, int l, int r){
if(r <= a || b <= l) return def;
if(a <= l && r <= b) return tree[k];
type vl = get(a, b, 2*k+1, l, (l+r)/2);
type vr = get(a, b, 2*k+2, (l+r)/2, r);
return marge(vl, vr);
}
type get(int a, int b){
return get(a, b, 0, 0, size);
}
};
Data marge(Data a, Data b){
Data d;
if(!b.x) return a;
if(!a.x) return b;
d.cost = a.cost + b.cost;
if((a.d + b.num) % k != 0){
d.cost += (a.d + b.num);
}
d.num = a.num;
d.d = b.d;
d.x = true;
return d;
}
vector<pair<int, int> > treeGraph[100010];
vector<int> tree[100010];
vector<int> subtree[100010];
int par[100010] = {};
int num[100010] = {};
int vid[100010];
int nid[100010];
int head[100010];
int team[100010];
int size[100010] = {};
int b = 0;
bool used[100010] = {};
int dist[100010];
int oya[100010];
SegmentTree<Data> segTree;
void maketree(int i){
used[i] = true;
for(int x = 0;x < treeGraph[i].size();x++){
if(!used[treeGraph[i][x].first]){
par[treeGraph[i][x].first] = i;
dist[treeGraph[i][x].first] = treeGraph[i][x].second;
tree[i].push_back(treeGraph[i][x].first);
maketree(treeGraph[i][x].first);
}
}
}
int setnum(int i){
int ret = 1;
for(int x = 0;x < tree[i].size();x++){
ret += setnum(tree[i][x]);
}
return num[i] = ret;
}
void dis(int n){
memset(team,-1,sizeof(team));
priority_queue<pair<int, int> > que;
for(int i = 0;i < n;i++){
que.push(make_pair(num[i], i));
}
int gnum = 0;
int next;
int pos = 0;
int i;
while(!que.empty()){
i = que.top().second; que.pop();
if(team[i] != -1) continue;
team[i] = gnum;
size[gnum]++;
subtree[gnum].push_back(i);
nid[i] = 0;
int nn = 1;
head[i] = i;
vid[i] = pos;
pos++;
next = i;
while(tree[next].size() != 0){
int maxi = 0;
for(int j = 1;j < tree[next].size();j++){
if(num[tree[next][maxi]] < num[tree[next][j]]){
maxi = j;
}
}
subtree[gnum].push_back(tree[next][maxi]);
team[tree[next][maxi]] = gnum;
head[tree[next][maxi]] = i;
vid[tree[next][maxi]] = pos;
nid[tree[next][maxi]] = nn;
size[gnum]++;
pos++;
nn++;
next = tree[next][maxi];
}
gnum++;
}
return;
}
long long subDist[100010];
SegmentTree<Data> seg;
void build(int n){
maketree(0);
setnum(0);
dis(n);
seg.init(n, Data(0, 0, false), marge);
for(int i = 0;i < n;i++){
seg.update(vid[i], Data(dist[i], 0, true));
}
return;
}
int so(int i, int j){
if(vid[i] >vid[j]) swap(i, j);
if(head[i] == head[j]) return i;
return so(i, par[head[j]]);
}
Data get(int i, int j){
//if(i == j) return Data();
if(vid[i] > vid[j]) swap(i, j);
if(head[i] == head[j]){
return seg.get(vid[i], vid[j]+1);
}
return marge(get(i, par[head[j]]), seg.get(vid[head[j]], vid[j]+1));
}
signed main(){
int n, m;
cin >> n >> k;
int a, b, c;
for(int i = 0;i < n-1;i++){
cin >> a >> b >> c;
treeGraph[a].push_back(make_pair(b, c));
treeGraph[b].push_back(make_pair(a, c));
}
build(n);
cin >> m;
string s;
for(int p = 0;p < m;p++){
cin >> s >> a >> b;
if(s == "send"){
int ss = so(a, b);
cout << get(a, ss).cost + get(b, ss).cost << endl;
}else{
seg.update(vid[a], Data(b, b, true));
}
}
return 0;
}

ステータス

項目 データ
問題 0965 - ネットワークの課金システム
ユーザー名 ei1719
投稿日時 2019-08-05 21:51:08
言語 C++14
状態 Accepted
得点 17
ソースコード長 5322 Byte
最大実行時間 813 ms
最大メモリ使用量 40776 KB

セット

セット 得点 Cases
1 ALL 17 / 17 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in1.txt AC 26 ms 13404 KB
1
in2.txt AC 23 ms 16224 KB
1
in3.txt AC 30 ms 16092 KB
1
in4.txt AC 22 ms 16064 KB
1
in5.txt AC 20 ms 16164 KB
1
in6.txt AC 24 ms 16260 KB
1
in7.txt AC 20 ms 16348 KB
1
in8.txt AC 27 ms 16296 KB
1
in9.txt AC 71 ms 18124 KB
1
in10.txt AC 71 ms 15624 KB
1
in11.txt AC 103 ms 20892 KB
1
in12.txt AC 167 ms 29924 KB
1
in13.txt AC 192 ms 30980 KB
1
in14.txt AC 205 ms 32460 KB
1
in15.txt AC 184 ms 30868 KB
1
in16.txt AC 481 ms 33252 KB
1
in17.txt AC 444 ms 36016 KB
1
in18.txt AC 532 ms 36560 KB
1
in19.txt AC 813 ms 33812 KB
1
in20.txt AC 222 ms 33692 KB
1
in21.txt AC 221 ms 40776 KB
1