ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#196584 | #2765. 幽幽子与妖梦 | snow_trace | 100 | 1546ms | 265936kb | C++11 | 1.8kb | 2023-10-28 11:56:16 | 2023-10-28 12:07:08 |
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){
int x =0 ;char c= getchar();
while(c<'0' or c>'9')c = getchar();
while('0'<=c and c<='9'){
x = x*10+(c^48),c =getchar();
}return x;
}int n;
int fa[500005],sz[500005];
int now =0,ans =0 ;
vector<int>lf;
inline int find(int x){
if(x == fa[x])return x;
return find(fa[x]);
}void merge(int a,int b){
int x= find(a),y = find(b);
if(x == y)return;
if(sz[x]>sz[y])swap(x,y);
fa[x] = y;
now+=sz[y]*sz[x];
sz[y]+=sz[x];
lf.push_back(x);return;
}inline void del(){
if(lf.empty())return;
int x = lf.back();lf.pop_back();
int tt = fa[x];
sz[fa[x]]-=sz[x],fa[x] = x;
now-=sz[tt]*sz[x];
return;
}struct node{
int l,r;
vector<pair<int,int> >q;
}tree[2000005];
inline void build(int k,int l,int r){
tree[k].l = l,tree[k].r = r;
if(l+1 == r)return;
build(k<<1,l,l+r>>1),build(k<<1|1,l+r>>1,r);
}inline void upd(int k,int l,int r,int x,int y){
int ll = tree[k].l,rr = tree[k].r;
if(l>=rr or ll>=r)return;
if(l<=ll and rr<=r){
tree[k].q.push_back({x,y});return;
}upd(k<<1,l,r,x,y),upd(k<<1|1,l,r,x,y);
return;
}inline void dfs(int k){
int l = tree[k].l,r = tree[k].r,o = lf.size();
//cout << l << " " << r << endl;
for(int i =0;i<tree[k].q.size();i++){
// cout << " " << tree[k].q[i].first << " " << tree[k].q[i].second << endl;
merge(tree[k].q[i].first,tree[k].q[i].second);
}if(l+1 == r){
// cout << l << " "<< now << endl;
ans+=n*(n-1)/2-now;
}else dfs(k<<1),dfs(k<<1|1);
while(lf.size()>o)del();return;
}
signed main(){
n = read();
for(int i = 1;i<=n;i++)fa[i] = i,sz[i] = 1;
build(1,1,n+1);
for(int i = 1;i<n;i++){
int a = read(),b = read(),c = read();
if(c!=1)upd(1,1,c,a,b);
if(c!=n)upd(1,c+1,n+1,a,b);
}dfs(1);
cout << ans*2 << endl;
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 8ms
memory: 79428kb
input:
300 2 1 15 3 1 10 4 1 1 5 3 2 6 1 8 7 1 16 8 6 17 9 5 16 10 6 3 11 2 16 12 10 7 13 12 8 14 9 2 15 7 ...
output:
995128
result:
ok single line: '995128'
Test #2:
score: 10
Accepted
time: 8ms
memory: 79432kb
input:
300 2 1 8 3 2 16 4 2 8 5 3 1 6 2 16 7 5 12 8 5 10 9 1 8 10 4 5 11 8 6 12 8 17 13 1 3 14 2 16 15 7 2 ...
output:
1003386
result:
ok single line: '1003386'
Test #3:
score: 10
Accepted
time: 12ms
memory: 79432kb
input:
300 2 1 15 3 1 15 4 2 8 5 4 4 6 3 5 7 6 1 8 3 4 9 2 5 10 1 4 11 2 1 12 9 7 13 10 3 14 7 4 15 10 6 16...
output:
979308
result:
ok single line: '979308'
Test #4:
score: 10
Accepted
time: 57ms
memory: 94924kb
input:
50000 1 2 142 2 3 135 3 4 103 4 5 17 5 6 13 6 7 149 7 8 4 8 9 206 9 10 40 10 11 133 11 12 173 12 13 ...
output:
552565399476
result:
ok single line: '552565399476'
Test #5:
score: 10
Accepted
time: 49ms
memory: 95148kb
input:
50000 1 2 211 2 3 135 3 4 121 4 5 90 5 6 154 6 7 37 7 8 190 8 9 126 9 10 106 10 11 126 11 12 174 12 ...
output:
552552414980
result:
ok single line: '552552414980'
Test #6:
score: 10
Accepted
time: 48ms
memory: 94996kb
input:
50000 1 2 56 1 3 200 1 4 109 1 5 132 1 6 178 1 7 132 1 8 46 1 9 27 1 10 29 1 11 118 1 12 217 1 13 10...
output:
4988587626
result:
ok single line: '4988587626'
Test #7:
score: 10
Accepted
time: 47ms
memory: 95020kb
input:
50000 1 2 15 1 3 164 1 4 210 1 5 3 1 6 192 1 7 134 1 8 76 1 9 118 1 10 148 1 11 204 1 12 220 1 13 16...
output:
4988587498
result:
ok single line: '4988587498'
Test #8:
score: 10
Accepted
time: 52ms
memory: 95084kb
input:
50000 2 1 75 3 2 141 4 1 111 5 1 133 6 2 146 7 5 201 8 7 32 9 6 46 10 6 130 11 9 27 12 5 51 13 1 133...
output:
509207879442
result:
ok single line: '509207879442'
Test #9:
score: 10
Accepted
time: 629ms
memory: 265884kb
input:
500000 2 1 692 3 1 414 4 3 348 5 3 162 6 3 334 7 6 589 8 6 314 9 3 309 10 2 380 11 3 620 12 2 418 13...
output:
171527774112372
result:
ok single line: '171527774112372'
Test #10:
score: 10
Accepted
time: 636ms
memory: 265936kb
input:
500000 2 1 625 3 1 678 4 1 636 5 4 289 6 2 328 7 5 683 8 3 116 9 4 281 10 8 58 11 9 378 12 8 279 13 ...
output:
171458440760230
result:
ok single line: '171458440760230'