#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC optimize("conserve-stack")
#include<bits/stdc++.h>
//#define int long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define mask(i) (1<<i)
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
#define getchar_unlocked getchar
inline int scan()
{
char c = getchar_unlocked();
int x = 0;
while (c < '0' || c > '9')
{
c = getchar_unlocked();
}
while (c >= '0' && c <= '9')
{
x = (x << 1) + (x << 3) + c - '0';
c = getchar_unlocked();
}
return x;
};
void phongbeo();
const int inf = 1e18;
const int mod2 = 1e9+7;
const int mod1 = 998244353;
const int base=67;
int add(int x,int y)
{
if(x+y>=mod2) x-=mod2;
if(x+y<0) x+=mod2;
return x+y;
}
struct icd
{
long double a;
int b;
};
struct ib
{
int a;
int b;
};
struct ic
{
int a, b, c, d;
};
struct ie
{
int a, b, c, d, e;
};
int n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2, r2, center;
int i, s10, s12,k1,k2,k3,s11,limit,w,l,r,last,root,cost;
int kk,invf;
int el = 19;
main()
{
if(fopen(task".inp","r"))
{
fin(task);
//fou(task);
}
NHP
/// cin>>s1;
// modwwe
phongbeo(),down
// checktime
}
int dp[100001];
int st[17][100001];
int in[100001];
int ou[100001];
vector<int>v[100001];
vector<ic> v2[100001];
struct fen_wick
{
int bit[100001];
void upd(int x,int y,int z)
{
y++;
for(x; x<=n; x+=x&-x)
bit[x]+=z;
for(y; y<=n; y+=y&-y)
bit[y]-=z;
}
int get(int x)
{
int s=0;
for(x; x; x-=x&-x)
s+=bit[x];
return s;
}
} fen;
bool check(int x,int y)
{
if(in[x]<=in[y]&&ou[x]>=in[y]) return 0;
else return 1;
}
int lca(int x,int y)
{
if(!check(x,y)) return x;
for(int j=16; j>=0; --j)
if(check(st[j][x],y))
x=st[j][x];
return st[0][x];
}
void dfs(int x,int y)
{
st[0][x]=y;
in[x]=++dem;
for(auto f:v[x])
if(f!=y)
dfs(f,x);
ou[x]=dem;
}
void dfs2(int x,int y)
{
for(auto f:v[x])
if(f!=y)
dfs2(f,x),
dp[x]+=dp[f],
fen.upd(in[f],ou[f],-dp[f]);
s5=0;
for(auto f:v2[x])
s2=fen.get(in[f.a])+fen.get(in[f.b])+f.c,
s5=max(s5,s2);
dp[x]+=s5;
for(auto f:v[x])
if(f!=y)
fen.upd(in[x]+1,ou[x],dp[f]);
fen.upd(in[x],in[x],dp[x]-s5);
}
void phongbeo()
{
cin>>n;
for(int i=2; i<=n; i++)
{
cin>>l>>r;
v[l].pb(r);
v[r].pb(l);
}
dfs(1,0);
ou[0]=dem;
for(int i=1; i<17; i++)
for(int j=1; j<=n; j++)
st[i][j]=st[i-1][st[i-1][j]];
cin>>m;
for(int i=1; i<=m; i++)
cin>>l>>r>>s2,
v2[lca(l,r)].pb({l,r,s2});
dfs2(1,0);
cout<<dp[1];
}
Compilation message
election_campaign.cpp:37:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
37 | const int inf = 1e18;
| ^~~~
election_campaign.cpp:72:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
72 | main()
| ^~~~
election_campaign.cpp: In member function 'void fen_wick::upd(int, int, int)':
election_campaign.cpp:97:13: warning: statement has no effect [-Wunused-value]
97 | for(x; x<=n; x+=x&-x)
| ^
election_campaign.cpp:99:13: warning: statement has no effect [-Wunused-value]
99 | for(y; y<=n; y+=y&-y)
| ^
election_campaign.cpp: In member function 'int fen_wick::get(int)':
election_campaign.cpp:105:13: warning: statement has no effect [-Wunused-value]
105 | for(x; x; x-=x&-x)
| ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define fin(x) freopen(x".inp","r",stdin)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp:76:9: note: in expansion of macro 'fin'
76 | fin(task);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12624 KB |
Output is correct |
2 |
Correct |
2 ms |
12624 KB |
Output is correct |
3 |
Correct |
3 ms |
12792 KB |
Output is correct |
4 |
Correct |
3 ms |
12624 KB |
Output is correct |
5 |
Correct |
43 ms |
16456 KB |
Output is correct |
6 |
Correct |
34 ms |
27216 KB |
Output is correct |
7 |
Correct |
40 ms |
23632 KB |
Output is correct |
8 |
Correct |
30 ms |
16976 KB |
Output is correct |
9 |
Correct |
37 ms |
21320 KB |
Output is correct |
10 |
Correct |
31 ms |
16992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12792 KB |
Output is correct |
2 |
Correct |
2 ms |
12624 KB |
Output is correct |
3 |
Correct |
3 ms |
12880 KB |
Output is correct |
4 |
Correct |
77 ms |
30024 KB |
Output is correct |
5 |
Correct |
77 ms |
30036 KB |
Output is correct |
6 |
Correct |
64 ms |
30024 KB |
Output is correct |
7 |
Correct |
78 ms |
30024 KB |
Output is correct |
8 |
Correct |
77 ms |
30028 KB |
Output is correct |
9 |
Correct |
65 ms |
31564 KB |
Output is correct |
10 |
Correct |
81 ms |
29768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12792 KB |
Output is correct |
2 |
Correct |
2 ms |
12624 KB |
Output is correct |
3 |
Correct |
3 ms |
12880 KB |
Output is correct |
4 |
Correct |
77 ms |
30024 KB |
Output is correct |
5 |
Correct |
77 ms |
30036 KB |
Output is correct |
6 |
Correct |
64 ms |
30024 KB |
Output is correct |
7 |
Correct |
78 ms |
30024 KB |
Output is correct |
8 |
Correct |
77 ms |
30028 KB |
Output is correct |
9 |
Correct |
65 ms |
31564 KB |
Output is correct |
10 |
Correct |
81 ms |
29768 KB |
Output is correct |
11 |
Correct |
10 ms |
13660 KB |
Output is correct |
12 |
Correct |
80 ms |
29768 KB |
Output is correct |
13 |
Correct |
82 ms |
30028 KB |
Output is correct |
14 |
Correct |
65 ms |
30024 KB |
Output is correct |
15 |
Correct |
78 ms |
30028 KB |
Output is correct |
16 |
Correct |
65 ms |
31560 KB |
Output is correct |
17 |
Correct |
81 ms |
31304 KB |
Output is correct |
18 |
Correct |
88 ms |
30036 KB |
Output is correct |
19 |
Correct |
78 ms |
30032 KB |
Output is correct |
20 |
Correct |
89 ms |
29924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
20160 KB |
Output is correct |
2 |
Correct |
72 ms |
30032 KB |
Output is correct |
3 |
Correct |
105 ms |
27232 KB |
Output is correct |
4 |
Correct |
81 ms |
18876 KB |
Output is correct |
5 |
Correct |
98 ms |
24892 KB |
Output is correct |
6 |
Correct |
72 ms |
18740 KB |
Output is correct |
7 |
Correct |
99 ms |
24640 KB |
Output is correct |
8 |
Correct |
96 ms |
20288 KB |
Output is correct |
9 |
Correct |
67 ms |
31412 KB |
Output is correct |
10 |
Correct |
108 ms |
23228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12624 KB |
Output is correct |
2 |
Correct |
2 ms |
12624 KB |
Output is correct |
3 |
Correct |
3 ms |
12792 KB |
Output is correct |
4 |
Correct |
3 ms |
12624 KB |
Output is correct |
5 |
Correct |
43 ms |
16456 KB |
Output is correct |
6 |
Correct |
34 ms |
27216 KB |
Output is correct |
7 |
Correct |
40 ms |
23632 KB |
Output is correct |
8 |
Correct |
30 ms |
16976 KB |
Output is correct |
9 |
Correct |
37 ms |
21320 KB |
Output is correct |
10 |
Correct |
31 ms |
16992 KB |
Output is correct |
11 |
Correct |
3 ms |
12624 KB |
Output is correct |
12 |
Correct |
3 ms |
12852 KB |
Output is correct |
13 |
Correct |
3 ms |
12880 KB |
Output is correct |
14 |
Correct |
3 ms |
12872 KB |
Output is correct |
15 |
Correct |
3 ms |
12624 KB |
Output is correct |
16 |
Correct |
3 ms |
12880 KB |
Output is correct |
17 |
Correct |
3 ms |
12624 KB |
Output is correct |
18 |
Correct |
3 ms |
12880 KB |
Output is correct |
19 |
Correct |
4 ms |
12624 KB |
Output is correct |
20 |
Correct |
3 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12624 KB |
Output is correct |
2 |
Correct |
2 ms |
12624 KB |
Output is correct |
3 |
Correct |
3 ms |
12792 KB |
Output is correct |
4 |
Correct |
3 ms |
12624 KB |
Output is correct |
5 |
Correct |
43 ms |
16456 KB |
Output is correct |
6 |
Correct |
34 ms |
27216 KB |
Output is correct |
7 |
Correct |
40 ms |
23632 KB |
Output is correct |
8 |
Correct |
30 ms |
16976 KB |
Output is correct |
9 |
Correct |
37 ms |
21320 KB |
Output is correct |
10 |
Correct |
31 ms |
16992 KB |
Output is correct |
11 |
Correct |
3 ms |
12792 KB |
Output is correct |
12 |
Correct |
2 ms |
12624 KB |
Output is correct |
13 |
Correct |
3 ms |
12880 KB |
Output is correct |
14 |
Correct |
77 ms |
30024 KB |
Output is correct |
15 |
Correct |
77 ms |
30036 KB |
Output is correct |
16 |
Correct |
64 ms |
30024 KB |
Output is correct |
17 |
Correct |
78 ms |
30024 KB |
Output is correct |
18 |
Correct |
77 ms |
30028 KB |
Output is correct |
19 |
Correct |
65 ms |
31564 KB |
Output is correct |
20 |
Correct |
81 ms |
29768 KB |
Output is correct |
21 |
Correct |
10 ms |
13660 KB |
Output is correct |
22 |
Correct |
80 ms |
29768 KB |
Output is correct |
23 |
Correct |
82 ms |
30028 KB |
Output is correct |
24 |
Correct |
65 ms |
30024 KB |
Output is correct |
25 |
Correct |
78 ms |
30028 KB |
Output is correct |
26 |
Correct |
65 ms |
31560 KB |
Output is correct |
27 |
Correct |
81 ms |
31304 KB |
Output is correct |
28 |
Correct |
88 ms |
30036 KB |
Output is correct |
29 |
Correct |
78 ms |
30032 KB |
Output is correct |
30 |
Correct |
89 ms |
29924 KB |
Output is correct |
31 |
Correct |
101 ms |
20160 KB |
Output is correct |
32 |
Correct |
72 ms |
30032 KB |
Output is correct |
33 |
Correct |
105 ms |
27232 KB |
Output is correct |
34 |
Correct |
81 ms |
18876 KB |
Output is correct |
35 |
Correct |
98 ms |
24892 KB |
Output is correct |
36 |
Correct |
72 ms |
18740 KB |
Output is correct |
37 |
Correct |
99 ms |
24640 KB |
Output is correct |
38 |
Correct |
96 ms |
20288 KB |
Output is correct |
39 |
Correct |
67 ms |
31412 KB |
Output is correct |
40 |
Correct |
108 ms |
23228 KB |
Output is correct |
41 |
Correct |
3 ms |
12624 KB |
Output is correct |
42 |
Correct |
3 ms |
12852 KB |
Output is correct |
43 |
Correct |
3 ms |
12880 KB |
Output is correct |
44 |
Correct |
3 ms |
12872 KB |
Output is correct |
45 |
Correct |
3 ms |
12624 KB |
Output is correct |
46 |
Correct |
3 ms |
12880 KB |
Output is correct |
47 |
Correct |
3 ms |
12624 KB |
Output is correct |
48 |
Correct |
3 ms |
12880 KB |
Output is correct |
49 |
Correct |
4 ms |
12624 KB |
Output is correct |
50 |
Correct |
3 ms |
12880 KB |
Output is correct |
51 |
Correct |
90 ms |
18884 KB |
Output is correct |
52 |
Correct |
78 ms |
29768 KB |
Output is correct |
53 |
Correct |
123 ms |
23228 KB |
Output is correct |
54 |
Correct |
87 ms |
20300 KB |
Output is correct |
55 |
Correct |
101 ms |
18620 KB |
Output is correct |
56 |
Correct |
88 ms |
30024 KB |
Output is correct |
57 |
Correct |
88 ms |
24260 KB |
Output is correct |
58 |
Correct |
75 ms |
18876 KB |
Output is correct |
59 |
Correct |
85 ms |
18820 KB |
Output is correct |
60 |
Correct |
81 ms |
31304 KB |
Output is correct |
61 |
Correct |
98 ms |
24264 KB |
Output is correct |
62 |
Correct |
73 ms |
18680 KB |
Output is correct |
63 |
Correct |
81 ms |
18244 KB |
Output is correct |
64 |
Correct |
80 ms |
29848 KB |
Output is correct |
65 |
Correct |
116 ms |
25788 KB |
Output is correct |
66 |
Correct |
77 ms |
18728 KB |
Output is correct |
67 |
Correct |
102 ms |
19864 KB |
Output is correct |
68 |
Correct |
79 ms |
29772 KB |
Output is correct |
69 |
Correct |
87 ms |
22656 KB |
Output is correct |
70 |
Correct |
80 ms |
18628 KB |
Output is correct |