#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;
typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements
#define DEBUG
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__);
#else
#define debug(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
int n,c[100005],a,b,p[100005],ft[100005];
vii v;
void up(int x,int v){
while(x<=n)ft[x]+=v,x+=x&-x;
}
int qry(int x){
int res=0;
while(x)res+=ft[x],x-=x&-x;
return res;
}
int main(){
sf("%d",&n);
for(int i=1;i<=n;++i){
sf("%d",&c[i]);
v.pb(c[i],i);
}
sort(all(v));
int cnt=0,pv=-1;
for(int i=0;i<n;++i){
if(v[i].fi!=pv)++cnt,pv=v[i].fi;
c[v[i].se]=cnt;
}
p[1]=-1;
for(int i=1;i<n;++i){
sf("%d%d",&a,&b);
memset(ft,0,sizeof ft);
p[b]=a;
ll ans=0;
while(a!=-1){
ans+=qry(c[a]);
up(c[a]+1,1);
c[a]=c[b];
a=p[a];
}
pf("%lld\n",ans);
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:54:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | sf("%d",&n);
| ^
construction.cpp:56:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | sf("%d",&c[i]);
| ^
construction.cpp:67:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | sf("%d%d",&a,&b);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
6 ms |
720 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
8 ms |
724 KB |
Output is correct |
7 |
Correct |
8 ms |
720 KB |
Output is correct |
8 |
Correct |
10 ms |
700 KB |
Output is correct |
9 |
Correct |
12 ms |
840 KB |
Output is correct |
10 |
Correct |
11 ms |
724 KB |
Output is correct |
11 |
Correct |
9 ms |
716 KB |
Output is correct |
12 |
Correct |
9 ms |
716 KB |
Output is correct |
13 |
Correct |
9 ms |
724 KB |
Output is correct |
14 |
Correct |
9 ms |
716 KB |
Output is correct |
15 |
Correct |
8 ms |
720 KB |
Output is correct |
16 |
Correct |
8 ms |
716 KB |
Output is correct |
17 |
Correct |
8 ms |
720 KB |
Output is correct |
18 |
Correct |
8 ms |
720 KB |
Output is correct |
19 |
Correct |
9 ms |
716 KB |
Output is correct |
20 |
Correct |
10 ms |
716 KB |
Output is correct |
21 |
Correct |
9 ms |
696 KB |
Output is correct |
22 |
Correct |
8 ms |
716 KB |
Output is correct |
23 |
Correct |
8 ms |
716 KB |
Output is correct |
24 |
Correct |
9 ms |
696 KB |
Output is correct |
25 |
Correct |
8 ms |
720 KB |
Output is correct |
26 |
Correct |
11 ms |
716 KB |
Output is correct |
27 |
Correct |
9 ms |
692 KB |
Output is correct |
28 |
Correct |
9 ms |
720 KB |
Output is correct |
29 |
Correct |
9 ms |
716 KB |
Output is correct |
30 |
Correct |
8 ms |
692 KB |
Output is correct |
31 |
Correct |
8 ms |
588 KB |
Output is correct |
32 |
Correct |
9 ms |
716 KB |
Output is correct |
33 |
Correct |
9 ms |
692 KB |
Output is correct |
34 |
Correct |
8 ms |
716 KB |
Output is correct |
35 |
Correct |
8 ms |
716 KB |
Output is correct |
36 |
Correct |
8 ms |
716 KB |
Output is correct |
37 |
Correct |
9 ms |
716 KB |
Output is correct |
38 |
Correct |
8 ms |
716 KB |
Output is correct |
39 |
Correct |
9 ms |
716 KB |
Output is correct |
40 |
Correct |
8 ms |
716 KB |
Output is correct |
41 |
Correct |
8 ms |
588 KB |
Output is correct |
42 |
Correct |
9 ms |
716 KB |
Output is correct |
43 |
Correct |
8 ms |
716 KB |
Output is correct |
44 |
Correct |
10 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
6 ms |
720 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
8 ms |
724 KB |
Output is correct |
7 |
Correct |
8 ms |
720 KB |
Output is correct |
8 |
Correct |
10 ms |
700 KB |
Output is correct |
9 |
Correct |
12 ms |
840 KB |
Output is correct |
10 |
Correct |
11 ms |
724 KB |
Output is correct |
11 |
Correct |
9 ms |
716 KB |
Output is correct |
12 |
Correct |
9 ms |
716 KB |
Output is correct |
13 |
Correct |
9 ms |
724 KB |
Output is correct |
14 |
Correct |
9 ms |
716 KB |
Output is correct |
15 |
Correct |
8 ms |
720 KB |
Output is correct |
16 |
Correct |
8 ms |
716 KB |
Output is correct |
17 |
Correct |
8 ms |
720 KB |
Output is correct |
18 |
Correct |
8 ms |
720 KB |
Output is correct |
19 |
Correct |
9 ms |
716 KB |
Output is correct |
20 |
Correct |
10 ms |
716 KB |
Output is correct |
21 |
Correct |
9 ms |
696 KB |
Output is correct |
22 |
Correct |
8 ms |
716 KB |
Output is correct |
23 |
Correct |
8 ms |
716 KB |
Output is correct |
24 |
Correct |
9 ms |
696 KB |
Output is correct |
25 |
Correct |
8 ms |
720 KB |
Output is correct |
26 |
Correct |
11 ms |
716 KB |
Output is correct |
27 |
Correct |
9 ms |
692 KB |
Output is correct |
28 |
Correct |
9 ms |
720 KB |
Output is correct |
29 |
Correct |
9 ms |
716 KB |
Output is correct |
30 |
Correct |
8 ms |
692 KB |
Output is correct |
31 |
Correct |
8 ms |
588 KB |
Output is correct |
32 |
Correct |
9 ms |
716 KB |
Output is correct |
33 |
Correct |
9 ms |
692 KB |
Output is correct |
34 |
Correct |
8 ms |
716 KB |
Output is correct |
35 |
Correct |
8 ms |
716 KB |
Output is correct |
36 |
Correct |
8 ms |
716 KB |
Output is correct |
37 |
Correct |
9 ms |
716 KB |
Output is correct |
38 |
Correct |
8 ms |
716 KB |
Output is correct |
39 |
Correct |
9 ms |
716 KB |
Output is correct |
40 |
Correct |
8 ms |
716 KB |
Output is correct |
41 |
Correct |
8 ms |
588 KB |
Output is correct |
42 |
Correct |
9 ms |
716 KB |
Output is correct |
43 |
Correct |
8 ms |
716 KB |
Output is correct |
44 |
Correct |
10 ms |
720 KB |
Output is correct |
45 |
Correct |
15 ms |
716 KB |
Output is correct |
46 |
Correct |
58 ms |
844 KB |
Output is correct |
47 |
Correct |
58 ms |
844 KB |
Output is correct |
48 |
Correct |
61 ms |
868 KB |
Output is correct |
49 |
Correct |
201 ms |
844 KB |
Output is correct |
50 |
Correct |
201 ms |
844 KB |
Output is correct |
51 |
Correct |
206 ms |
844 KB |
Output is correct |
52 |
Correct |
137 ms |
840 KB |
Output is correct |
53 |
Correct |
131 ms |
844 KB |
Output is correct |
54 |
Correct |
130 ms |
844 KB |
Output is correct |
55 |
Correct |
147 ms |
844 KB |
Output is correct |
56 |
Correct |
139 ms |
844 KB |
Output is correct |
57 |
Correct |
59 ms |
968 KB |
Output is correct |
58 |
Correct |
58 ms |
828 KB |
Output is correct |
59 |
Correct |
59 ms |
844 KB |
Output is correct |
60 |
Correct |
58 ms |
860 KB |
Output is correct |
61 |
Correct |
129 ms |
844 KB |
Output is correct |
62 |
Correct |
132 ms |
844 KB |
Output is correct |
63 |
Correct |
131 ms |
844 KB |
Output is correct |
64 |
Correct |
58 ms |
716 KB |
Output is correct |
65 |
Correct |
57 ms |
716 KB |
Output is correct |
66 |
Correct |
58 ms |
840 KB |
Output is correct |
67 |
Correct |
59 ms |
820 KB |
Output is correct |
68 |
Correct |
235 ms |
816 KB |
Output is correct |
69 |
Correct |
135 ms |
836 KB |
Output is correct |
70 |
Correct |
142 ms |
812 KB |
Output is correct |
71 |
Correct |
152 ms |
828 KB |
Output is correct |
72 |
Correct |
58 ms |
716 KB |
Output is correct |
73 |
Correct |
59 ms |
840 KB |
Output is correct |
74 |
Correct |
143 ms |
716 KB |
Output is correct |
75 |
Correct |
98 ms |
844 KB |
Output is correct |
76 |
Correct |
83 ms |
844 KB |
Output is correct |
77 |
Correct |
84 ms |
844 KB |
Output is correct |
78 |
Correct |
103 ms |
936 KB |
Output is correct |
79 |
Correct |
88 ms |
716 KB |
Output is correct |
80 |
Correct |
81 ms |
812 KB |
Output is correct |
81 |
Correct |
98 ms |
844 KB |
Output is correct |
82 |
Correct |
84 ms |
844 KB |
Output is correct |
83 |
Correct |
79 ms |
844 KB |
Output is correct |
84 |
Correct |
101 ms |
824 KB |
Output is correct |
85 |
Correct |
87 ms |
716 KB |
Output is correct |
86 |
Correct |
81 ms |
812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
6 ms |
720 KB |
Output is correct |
5 |
Correct |
8 ms |
724 KB |
Output is correct |
6 |
Correct |
8 ms |
724 KB |
Output is correct |
7 |
Correct |
8 ms |
720 KB |
Output is correct |
8 |
Correct |
10 ms |
700 KB |
Output is correct |
9 |
Correct |
12 ms |
840 KB |
Output is correct |
10 |
Correct |
11 ms |
724 KB |
Output is correct |
11 |
Correct |
9 ms |
716 KB |
Output is correct |
12 |
Correct |
9 ms |
716 KB |
Output is correct |
13 |
Correct |
9 ms |
724 KB |
Output is correct |
14 |
Correct |
9 ms |
716 KB |
Output is correct |
15 |
Correct |
8 ms |
720 KB |
Output is correct |
16 |
Correct |
8 ms |
716 KB |
Output is correct |
17 |
Correct |
8 ms |
720 KB |
Output is correct |
18 |
Correct |
8 ms |
720 KB |
Output is correct |
19 |
Correct |
9 ms |
716 KB |
Output is correct |
20 |
Correct |
10 ms |
716 KB |
Output is correct |
21 |
Correct |
9 ms |
696 KB |
Output is correct |
22 |
Correct |
8 ms |
716 KB |
Output is correct |
23 |
Correct |
8 ms |
716 KB |
Output is correct |
24 |
Correct |
9 ms |
696 KB |
Output is correct |
25 |
Correct |
8 ms |
720 KB |
Output is correct |
26 |
Correct |
11 ms |
716 KB |
Output is correct |
27 |
Correct |
9 ms |
692 KB |
Output is correct |
28 |
Correct |
9 ms |
720 KB |
Output is correct |
29 |
Correct |
9 ms |
716 KB |
Output is correct |
30 |
Correct |
8 ms |
692 KB |
Output is correct |
31 |
Correct |
8 ms |
588 KB |
Output is correct |
32 |
Correct |
9 ms |
716 KB |
Output is correct |
33 |
Correct |
9 ms |
692 KB |
Output is correct |
34 |
Correct |
8 ms |
716 KB |
Output is correct |
35 |
Correct |
8 ms |
716 KB |
Output is correct |
36 |
Correct |
8 ms |
716 KB |
Output is correct |
37 |
Correct |
9 ms |
716 KB |
Output is correct |
38 |
Correct |
8 ms |
716 KB |
Output is correct |
39 |
Correct |
9 ms |
716 KB |
Output is correct |
40 |
Correct |
8 ms |
716 KB |
Output is correct |
41 |
Correct |
8 ms |
588 KB |
Output is correct |
42 |
Correct |
9 ms |
716 KB |
Output is correct |
43 |
Correct |
8 ms |
716 KB |
Output is correct |
44 |
Correct |
10 ms |
720 KB |
Output is correct |
45 |
Correct |
15 ms |
716 KB |
Output is correct |
46 |
Correct |
58 ms |
844 KB |
Output is correct |
47 |
Correct |
58 ms |
844 KB |
Output is correct |
48 |
Correct |
61 ms |
868 KB |
Output is correct |
49 |
Correct |
201 ms |
844 KB |
Output is correct |
50 |
Correct |
201 ms |
844 KB |
Output is correct |
51 |
Correct |
206 ms |
844 KB |
Output is correct |
52 |
Correct |
137 ms |
840 KB |
Output is correct |
53 |
Correct |
131 ms |
844 KB |
Output is correct |
54 |
Correct |
130 ms |
844 KB |
Output is correct |
55 |
Correct |
147 ms |
844 KB |
Output is correct |
56 |
Correct |
139 ms |
844 KB |
Output is correct |
57 |
Correct |
59 ms |
968 KB |
Output is correct |
58 |
Correct |
58 ms |
828 KB |
Output is correct |
59 |
Correct |
59 ms |
844 KB |
Output is correct |
60 |
Correct |
58 ms |
860 KB |
Output is correct |
61 |
Correct |
129 ms |
844 KB |
Output is correct |
62 |
Correct |
132 ms |
844 KB |
Output is correct |
63 |
Correct |
131 ms |
844 KB |
Output is correct |
64 |
Correct |
58 ms |
716 KB |
Output is correct |
65 |
Correct |
57 ms |
716 KB |
Output is correct |
66 |
Correct |
58 ms |
840 KB |
Output is correct |
67 |
Correct |
59 ms |
820 KB |
Output is correct |
68 |
Correct |
235 ms |
816 KB |
Output is correct |
69 |
Correct |
135 ms |
836 KB |
Output is correct |
70 |
Correct |
142 ms |
812 KB |
Output is correct |
71 |
Correct |
152 ms |
828 KB |
Output is correct |
72 |
Correct |
58 ms |
716 KB |
Output is correct |
73 |
Correct |
59 ms |
840 KB |
Output is correct |
74 |
Correct |
143 ms |
716 KB |
Output is correct |
75 |
Correct |
98 ms |
844 KB |
Output is correct |
76 |
Correct |
83 ms |
844 KB |
Output is correct |
77 |
Correct |
84 ms |
844 KB |
Output is correct |
78 |
Correct |
103 ms |
936 KB |
Output is correct |
79 |
Correct |
88 ms |
716 KB |
Output is correct |
80 |
Correct |
81 ms |
812 KB |
Output is correct |
81 |
Correct |
98 ms |
844 KB |
Output is correct |
82 |
Correct |
84 ms |
844 KB |
Output is correct |
83 |
Correct |
79 ms |
844 KB |
Output is correct |
84 |
Correct |
101 ms |
824 KB |
Output is correct |
85 |
Correct |
87 ms |
716 KB |
Output is correct |
86 |
Correct |
81 ms |
812 KB |
Output is correct |
87 |
Correct |
146 ms |
1172 KB |
Output is correct |
88 |
Correct |
446 ms |
1856 KB |
Output is correct |
89 |
Correct |
1522 ms |
4844 KB |
Output is correct |
90 |
Correct |
1515 ms |
4872 KB |
Output is correct |
91 |
Correct |
1473 ms |
4844 KB |
Output is correct |
92 |
Execution timed out |
2048 ms |
3772 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |