//https://www.instagram.com/_modwwe/
#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2")
#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 checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
void phongbeo();
const int inf=1e18;
const int mod2=998244353;
const int mod1=998244353;
struct icd
{
int a,b;
};
struct ib
{
int a;
int b;
};
struct ic
{
int a,b,c;
};
struct id
{
int a,b,c,d;
};
struct ie
{
int a,b,c,d,e,f;
};
int n,m,s1,s2,s4,s3,sf,k,r,mid,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,dem=0,l;
int i,s10,s12;
int kk;
int el=29;
main()
{
#ifndef ONLINE_JUDGE
// fin(task),fou(task);
#endif
NHP
/// cin>>s1;
// modwwe
phongbeo();
}
int st[100001];
int pos[100001];
int heavy[100001];
int head[100001];
int b[100001];
int a[100001];
vector<int> v[100001];
vector<ib> vv;
int t[400001];
int bit[100001];
int t2[400001];
int dfs(int x,int y)
{
st[x]=y;
int sz=1;
int mx_sz=0;
for(auto f:v[x])
{
if(f!=y)
{
int s=dfs(f,x);
sz+=s;
if(s>mx_sz)
mx_sz=s,heavy[x]=f;
}
}
return sz;
}
void descompose(int x,int y)
{
pos[x]=++dem;
head[x]=y;
if(heavy[x]!=0)
descompose(heavy[x],y);
for(auto f:v[x])
if(pos[f]==0)
descompose(f,f);
}
void upd_fenwick(int x,int y)
{
for(x;x<=n;x+=x&-x)
bit[x]+=y;
}
int get_fenwick(int x)
{
int s=0;
for(x;x;x-=x&-x)s+=bit[x];
return s;
}
void ff(int x)
{
for(int i=x*2; i<=x*2+1; i++)
t[i]=t2[x],
t2[i]=t2[x];
t2[x]=0;
return;
}
void upd(int node,int l,int r,int l1,int r1,int x)
{
if(l>r1||r<l1) return;
if(l>=l1&&r<=r1&&t[node])
{
s2+=get_fenwick(t[node]-1)*(r-l+1);
if(l!=1)
upd_fenwick(t[node],r-l+1), vv.pb({t[node],r-l+1});
t[node]=x;
t2[node]=x;
return;
}
else if(l==r)
{
t[node]=x;
t2[node]=x;
return;
}
int mid=l+r>>1;
if(t2[node]!=0)
ff(node);
upd(node<<1|1,mid+1,r,l1,r1,x);
upd(node<<1,l,mid,l1,r1,x);
t[node]=(t[node<<1]==t[node<<1|1])*t[node<<1];
}
void upd_hld(int x,int y,int c)
{
if(pos[head[x]]<=pos[y])
{
upd(1,1,n,pos[y],pos[x],c);
}
else
{
upd(1,1,n,pos[head[x]],pos[x],c);
upd_hld(st[head[x]],y,c);
}
}
vector<int> v2;
void phongbeo()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i],v2.pb(a[i]);
sort(v2.begin(),v2.end());
for(int i=1;i<=n;i++)
a[i]=lower_bound(v2.begin(),v2.end(),a[i])-v2.begin()+1;
for(int i=1;i<n;i++)
cin>>l>>r,v[l].pb(r),v[r].pb(l),b[i]=r;
dfs(1,0);
descompose(1,1);
upd(1,1,n,1,1,a[1]);
//for(int i=1;i<=n;i++)
//cout<<pos[i]<<" "<<i,down
for(int i=1;i<n;i++)
{
s2=0;
vv.clear();
upd_hld(b[i],1,a[b[i]]);
for(auto f:vv)
{
upd_fenwick(f.a,-f.b);
}
cout<<s2,down
}
}
Compilation message
construction.cpp:49:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
49 | main()
| ^~~~
construction.cpp: In function 'void upd_fenwick(long long int, long long int)':
construction.cpp:99:11: warning: statement has no effect [-Wunused-value]
99 | for(x;x<=n;x+=x&-x)
| ^
construction.cpp: In function 'long long int get_fenwick(long long int)':
construction.cpp:105:13: warning: statement has no effect [-Wunused-value]
105 | for(x;x;x-=x&-x)s+=bit[x];
| ^
construction.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int, long long int)':
construction.cpp:122:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
122 | if(l!=1)
| ^~
construction.cpp:124:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
124 | t[node]=x;
| ^
construction.cpp:134:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
134 | int mid=l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2696 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2828 KB |
Output is correct |
12 |
Correct |
2 ms |
2904 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
2904 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
3 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2744 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2824 KB |
Output is correct |
30 |
Correct |
2 ms |
2904 KB |
Output is correct |
31 |
Correct |
2 ms |
2932 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2912 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
3 ms |
2908 KB |
Output is correct |
36 |
Correct |
3 ms |
2892 KB |
Output is correct |
37 |
Correct |
2 ms |
2908 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2908 KB |
Output is correct |
41 |
Correct |
2 ms |
2908 KB |
Output is correct |
42 |
Correct |
2 ms |
2904 KB |
Output is correct |
43 |
Correct |
2 ms |
2908 KB |
Output is correct |
44 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2696 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2828 KB |
Output is correct |
12 |
Correct |
2 ms |
2904 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
2904 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
3 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2744 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2824 KB |
Output is correct |
30 |
Correct |
2 ms |
2904 KB |
Output is correct |
31 |
Correct |
2 ms |
2932 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2912 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
3 ms |
2908 KB |
Output is correct |
36 |
Correct |
3 ms |
2892 KB |
Output is correct |
37 |
Correct |
2 ms |
2908 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2908 KB |
Output is correct |
41 |
Correct |
2 ms |
2908 KB |
Output is correct |
42 |
Correct |
2 ms |
2904 KB |
Output is correct |
43 |
Correct |
2 ms |
2908 KB |
Output is correct |
44 |
Correct |
2 ms |
2908 KB |
Output is correct |
45 |
Correct |
3 ms |
2908 KB |
Output is correct |
46 |
Correct |
6 ms |
3420 KB |
Output is correct |
47 |
Correct |
7 ms |
3464 KB |
Output is correct |
48 |
Correct |
6 ms |
3464 KB |
Output is correct |
49 |
Correct |
6 ms |
3932 KB |
Output is correct |
50 |
Correct |
4 ms |
4188 KB |
Output is correct |
51 |
Correct |
5 ms |
4188 KB |
Output is correct |
52 |
Correct |
6 ms |
3732 KB |
Output is correct |
53 |
Correct |
6 ms |
3672 KB |
Output is correct |
54 |
Correct |
6 ms |
3836 KB |
Output is correct |
55 |
Correct |
5 ms |
3676 KB |
Output is correct |
56 |
Correct |
7 ms |
3756 KB |
Output is correct |
57 |
Correct |
7 ms |
3448 KB |
Output is correct |
58 |
Correct |
7 ms |
3420 KB |
Output is correct |
59 |
Correct |
7 ms |
3368 KB |
Output is correct |
60 |
Correct |
9 ms |
3464 KB |
Output is correct |
61 |
Correct |
6 ms |
3676 KB |
Output is correct |
62 |
Correct |
5 ms |
3704 KB |
Output is correct |
63 |
Correct |
5 ms |
3676 KB |
Output is correct |
64 |
Correct |
7 ms |
3232 KB |
Output is correct |
65 |
Correct |
9 ms |
3420 KB |
Output is correct |
66 |
Correct |
6 ms |
3420 KB |
Output is correct |
67 |
Correct |
8 ms |
3344 KB |
Output is correct |
68 |
Correct |
4 ms |
3932 KB |
Output is correct |
69 |
Correct |
4 ms |
3740 KB |
Output is correct |
70 |
Correct |
4 ms |
3688 KB |
Output is correct |
71 |
Correct |
6 ms |
3748 KB |
Output is correct |
72 |
Correct |
10 ms |
3420 KB |
Output is correct |
73 |
Correct |
8 ms |
3436 KB |
Output is correct |
74 |
Correct |
5 ms |
3676 KB |
Output is correct |
75 |
Correct |
6 ms |
3652 KB |
Output is correct |
76 |
Correct |
6 ms |
3420 KB |
Output is correct |
77 |
Correct |
6 ms |
3420 KB |
Output is correct |
78 |
Correct |
5 ms |
3420 KB |
Output is correct |
79 |
Correct |
4 ms |
3476 KB |
Output is correct |
80 |
Correct |
5 ms |
3524 KB |
Output is correct |
81 |
Correct |
7 ms |
3640 KB |
Output is correct |
82 |
Correct |
6 ms |
3340 KB |
Output is correct |
83 |
Correct |
8 ms |
3416 KB |
Output is correct |
84 |
Correct |
5 ms |
3420 KB |
Output is correct |
85 |
Correct |
5 ms |
3340 KB |
Output is correct |
86 |
Correct |
5 ms |
3416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2696 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2828 KB |
Output is correct |
12 |
Correct |
2 ms |
2904 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
2904 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
3 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2744 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2824 KB |
Output is correct |
30 |
Correct |
2 ms |
2904 KB |
Output is correct |
31 |
Correct |
2 ms |
2932 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2912 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
3 ms |
2908 KB |
Output is correct |
36 |
Correct |
3 ms |
2892 KB |
Output is correct |
37 |
Correct |
2 ms |
2908 KB |
Output is correct |
38 |
Correct |
2 ms |
2908 KB |
Output is correct |
39 |
Correct |
2 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2908 KB |
Output is correct |
41 |
Correct |
2 ms |
2908 KB |
Output is correct |
42 |
Correct |
2 ms |
2904 KB |
Output is correct |
43 |
Correct |
2 ms |
2908 KB |
Output is correct |
44 |
Correct |
2 ms |
2908 KB |
Output is correct |
45 |
Correct |
3 ms |
2908 KB |
Output is correct |
46 |
Correct |
6 ms |
3420 KB |
Output is correct |
47 |
Correct |
7 ms |
3464 KB |
Output is correct |
48 |
Correct |
6 ms |
3464 KB |
Output is correct |
49 |
Correct |
6 ms |
3932 KB |
Output is correct |
50 |
Correct |
4 ms |
4188 KB |
Output is correct |
51 |
Correct |
5 ms |
4188 KB |
Output is correct |
52 |
Correct |
6 ms |
3732 KB |
Output is correct |
53 |
Correct |
6 ms |
3672 KB |
Output is correct |
54 |
Correct |
6 ms |
3836 KB |
Output is correct |
55 |
Correct |
5 ms |
3676 KB |
Output is correct |
56 |
Correct |
7 ms |
3756 KB |
Output is correct |
57 |
Correct |
7 ms |
3448 KB |
Output is correct |
58 |
Correct |
7 ms |
3420 KB |
Output is correct |
59 |
Correct |
7 ms |
3368 KB |
Output is correct |
60 |
Correct |
9 ms |
3464 KB |
Output is correct |
61 |
Correct |
6 ms |
3676 KB |
Output is correct |
62 |
Correct |
5 ms |
3704 KB |
Output is correct |
63 |
Correct |
5 ms |
3676 KB |
Output is correct |
64 |
Correct |
7 ms |
3232 KB |
Output is correct |
65 |
Correct |
9 ms |
3420 KB |
Output is correct |
66 |
Correct |
6 ms |
3420 KB |
Output is correct |
67 |
Correct |
8 ms |
3344 KB |
Output is correct |
68 |
Correct |
4 ms |
3932 KB |
Output is correct |
69 |
Correct |
4 ms |
3740 KB |
Output is correct |
70 |
Correct |
4 ms |
3688 KB |
Output is correct |
71 |
Correct |
6 ms |
3748 KB |
Output is correct |
72 |
Correct |
10 ms |
3420 KB |
Output is correct |
73 |
Correct |
8 ms |
3436 KB |
Output is correct |
74 |
Correct |
5 ms |
3676 KB |
Output is correct |
75 |
Correct |
6 ms |
3652 KB |
Output is correct |
76 |
Correct |
6 ms |
3420 KB |
Output is correct |
77 |
Correct |
6 ms |
3420 KB |
Output is correct |
78 |
Correct |
5 ms |
3420 KB |
Output is correct |
79 |
Correct |
4 ms |
3476 KB |
Output is correct |
80 |
Correct |
5 ms |
3524 KB |
Output is correct |
81 |
Correct |
7 ms |
3640 KB |
Output is correct |
82 |
Correct |
6 ms |
3340 KB |
Output is correct |
83 |
Correct |
8 ms |
3416 KB |
Output is correct |
84 |
Correct |
5 ms |
3420 KB |
Output is correct |
85 |
Correct |
5 ms |
3340 KB |
Output is correct |
86 |
Correct |
5 ms |
3416 KB |
Output is correct |
87 |
Correct |
15 ms |
4444 KB |
Output is correct |
88 |
Correct |
49 ms |
7392 KB |
Output is correct |
89 |
Correct |
225 ms |
19148 KB |
Output is correct |
90 |
Correct |
192 ms |
19000 KB |
Output is correct |
91 |
Correct |
215 ms |
19080 KB |
Output is correct |
92 |
Correct |
97 ms |
37324 KB |
Output is correct |
93 |
Correct |
107 ms |
37324 KB |
Output is correct |
94 |
Correct |
92 ms |
37240 KB |
Output is correct |
95 |
Correct |
96 ms |
29484 KB |
Output is correct |
96 |
Correct |
106 ms |
30016 KB |
Output is correct |
97 |
Correct |
104 ms |
29900 KB |
Output is correct |
98 |
Correct |
106 ms |
29856 KB |
Output is correct |
99 |
Correct |
106 ms |
28620 KB |
Output is correct |
100 |
Correct |
243 ms |
18316 KB |
Output is correct |
101 |
Correct |
264 ms |
18624 KB |
Output is correct |
102 |
Correct |
275 ms |
18640 KB |
Output is correct |
103 |
Correct |
281 ms |
18720 KB |
Output is correct |
104 |
Correct |
130 ms |
28704 KB |
Output is correct |
105 |
Correct |
135 ms |
28616 KB |
Output is correct |
106 |
Correct |
138 ms |
28696 KB |
Output is correct |
107 |
Correct |
162 ms |
17616 KB |
Output is correct |
108 |
Correct |
186 ms |
18124 KB |
Output is correct |
109 |
Correct |
220 ms |
18640 KB |
Output is correct |
110 |
Correct |
91 ms |
36556 KB |
Output is correct |
111 |
Correct |
98 ms |
29644 KB |
Output is correct |
112 |
Correct |
102 ms |
29296 KB |
Output is correct |
113 |
Correct |
101 ms |
27932 KB |
Output is correct |
114 |
Correct |
257 ms |
18544 KB |
Output is correct |
115 |
Correct |
303 ms |
17912 KB |
Output is correct |
116 |
Correct |
127 ms |
28400 KB |
Output is correct |
117 |
Correct |
122 ms |
23692 KB |
Output is correct |
118 |
Correct |
115 ms |
21968 KB |
Output is correct |
119 |
Correct |
130 ms |
21284 KB |
Output is correct |
120 |
Correct |
111 ms |
23244 KB |
Output is correct |
121 |
Correct |
114 ms |
21808 KB |
Output is correct |
122 |
Correct |
139 ms |
20936 KB |
Output is correct |
123 |
Correct |
153 ms |
23720 KB |
Output is correct |
124 |
Correct |
142 ms |
21968 KB |
Output is correct |
125 |
Correct |
162 ms |
21192 KB |
Output is correct |
126 |
Correct |
154 ms |
23244 KB |
Output is correct |
127 |
Correct |
142 ms |
21708 KB |
Output is correct |
128 |
Correct |
154 ms |
20936 KB |
Output is correct |