#include <bits/stdc++.h>
#define ericxiao cin.tie(0); ios_base::sync_with_stdio(false);
#define endl '\n'
#define int long long
using namespace std;
typedef pair<int,int> pii;
const int maxn=2e5+10;
deque<int> posl[maxn],posr[maxn];
struct BIT{
int v[maxn],n;
int lb(int x){return x&(-x);}
void init(int x){n=x; for(int i=1;i<=n;i++) v[i]=lb(i);}
void modify(int x,int k){while(x<=n) v[x]+=k,x+=lb(x);}
int query(int x){int t=0; while(x>0) t+=v[x],x-=lb(x); return t;}
int get(int l,int r){return query(r+1)-query(l);}
void del(int x){modify(x+1,-1);}
void print(){for(int i=0;i<n;i++) cout<<get(i,i)<<' '; cout<<endl;}
} tree;
int count_swaps(vector<signed> v){
int n=v.size(),ans=0; tree.init(n);
for(int i=0;i<n;i++){
if(v[i]>0) posr[v[i]].push_back(i);
else posl[-v[i]].push_back(i);
}
// for(int i=1;i<=n;i++){
// cout<<i<<": "<<endl;
// for(auto &j:posl[i]) cout<<j<<' '; cout<<endl;
// for(auto &j:posr[i]) cout<<j<<' '; cout<<endl;
// }
// tree.print();
for(int i=0;i<n;i++){
int p=v[i];
if(p==0) continue;
if(p>0)
ans+=tree.get(i,posl[p].front())-1;
else
ans+=tree.get(i+1,posr[-p].front())-1;
tree.del(posl[abs(p)].front()); tree.del(posr[abs(p)].front());
v[posl[abs(p)].front()]=0; v[posr[abs(p)].front()]=0;
posl[abs(p)].pop_front(); posr[abs(p)].pop_front();
// tree.print();
// for(int j=0;j<n;j++) cout<<v[j]<<' '; cout<<endl;
// cout<<"ans: "<<ans<<endl;
}
return ans;
}
// signed main(){
// int n; cin>>n;
// vector<int> v(n);
// for(int i=0;i<n;i++) cin>>v[i];
// cout<<count_swaps(v)<<endl;
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
269624 KB |
Output is correct |
2 |
Correct |
184 ms |
269564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
269624 KB |
Output is correct |
2 |
Correct |
184 ms |
269564 KB |
Output is correct |
3 |
Correct |
186 ms |
269688 KB |
Output is correct |
4 |
Correct |
184 ms |
269564 KB |
Output is correct |
5 |
Correct |
187 ms |
269560 KB |
Output is correct |
6 |
Correct |
182 ms |
269560 KB |
Output is correct |
7 |
Correct |
187 ms |
269560 KB |
Output is correct |
8 |
Correct |
179 ms |
269560 KB |
Output is correct |
9 |
Correct |
180 ms |
269612 KB |
Output is correct |
10 |
Correct |
181 ms |
269560 KB |
Output is correct |
11 |
Correct |
182 ms |
269664 KB |
Output is correct |
12 |
Correct |
181 ms |
269560 KB |
Output is correct |
13 |
Correct |
179 ms |
269560 KB |
Output is correct |
14 |
Correct |
190 ms |
269560 KB |
Output is correct |
15 |
Correct |
186 ms |
269560 KB |
Output is correct |
16 |
Correct |
179 ms |
269560 KB |
Output is correct |
17 |
Correct |
189 ms |
269560 KB |
Output is correct |
18 |
Correct |
184 ms |
269560 KB |
Output is correct |
19 |
Correct |
191 ms |
269560 KB |
Output is correct |
20 |
Correct |
180 ms |
269560 KB |
Output is correct |
21 |
Correct |
193 ms |
269560 KB |
Output is correct |
22 |
Correct |
183 ms |
269560 KB |
Output is correct |
23 |
Correct |
188 ms |
269560 KB |
Output is correct |
24 |
Correct |
181 ms |
269572 KB |
Output is correct |
25 |
Correct |
183 ms |
269560 KB |
Output is correct |
26 |
Correct |
180 ms |
269560 KB |
Output is correct |
27 |
Correct |
187 ms |
269588 KB |
Output is correct |
28 |
Correct |
182 ms |
269560 KB |
Output is correct |
29 |
Correct |
183 ms |
269560 KB |
Output is correct |
30 |
Correct |
186 ms |
269560 KB |
Output is correct |
31 |
Correct |
182 ms |
269560 KB |
Output is correct |
32 |
Correct |
180 ms |
269564 KB |
Output is correct |
33 |
Correct |
181 ms |
269560 KB |
Output is correct |
34 |
Correct |
183 ms |
269536 KB |
Output is correct |
35 |
Correct |
181 ms |
269688 KB |
Output is correct |
36 |
Correct |
187 ms |
269560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
269624 KB |
Output is correct |
2 |
Correct |
184 ms |
269564 KB |
Output is correct |
3 |
Correct |
187 ms |
269712 KB |
Output is correct |
4 |
Correct |
185 ms |
269560 KB |
Output is correct |
5 |
Correct |
183 ms |
269688 KB |
Output is correct |
6 |
Correct |
185 ms |
269560 KB |
Output is correct |
7 |
Correct |
193 ms |
269688 KB |
Output is correct |
8 |
Correct |
181 ms |
269560 KB |
Output is correct |
9 |
Correct |
186 ms |
269564 KB |
Output is correct |
10 |
Correct |
183 ms |
269560 KB |
Output is correct |
11 |
Correct |
186 ms |
269560 KB |
Output is correct |
12 |
Correct |
179 ms |
269560 KB |
Output is correct |
13 |
Correct |
203 ms |
269560 KB |
Output is correct |
14 |
Correct |
184 ms |
269560 KB |
Output is correct |
15 |
Correct |
181 ms |
269688 KB |
Output is correct |
16 |
Correct |
183 ms |
269560 KB |
Output is correct |
17 |
Correct |
179 ms |
269560 KB |
Output is correct |
18 |
Correct |
194 ms |
269688 KB |
Output is correct |
19 |
Correct |
198 ms |
269688 KB |
Output is correct |
20 |
Correct |
203 ms |
270072 KB |
Output is correct |
21 |
Correct |
203 ms |
270072 KB |
Output is correct |
22 |
Correct |
237 ms |
274428 KB |
Output is correct |
23 |
Correct |
237 ms |
274424 KB |
Output is correct |
24 |
Correct |
241 ms |
274424 KB |
Output is correct |
25 |
Correct |
236 ms |
274424 KB |
Output is correct |
26 |
Correct |
242 ms |
275772 KB |
Output is correct |
27 |
Correct |
237 ms |
275572 KB |
Output is correct |
28 |
Correct |
234 ms |
275704 KB |
Output is correct |
29 |
Correct |
233 ms |
275704 KB |
Output is correct |
30 |
Correct |
232 ms |
275704 KB |
Output is correct |
31 |
Correct |
238 ms |
275704 KB |
Output is correct |
32 |
Correct |
233 ms |
275812 KB |
Output is correct |
33 |
Correct |
237 ms |
275448 KB |
Output is correct |
34 |
Correct |
241 ms |
275704 KB |
Output is correct |
35 |
Correct |
187 ms |
269560 KB |
Output is correct |
36 |
Correct |
184 ms |
269560 KB |
Output is correct |
37 |
Correct |
237 ms |
275704 KB |
Output is correct |
38 |
Correct |
236 ms |
275704 KB |
Output is correct |
39 |
Correct |
240 ms |
275708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
269564 KB |
Output is correct |
2 |
Correct |
185 ms |
269560 KB |
Output is correct |
3 |
Correct |
184 ms |
269560 KB |
Output is correct |
4 |
Correct |
187 ms |
269564 KB |
Output is correct |
5 |
Correct |
283 ms |
272736 KB |
Output is correct |
6 |
Correct |
261 ms |
274040 KB |
Output is correct |
7 |
Correct |
277 ms |
273912 KB |
Output is correct |
8 |
Correct |
233 ms |
275704 KB |
Output is correct |
9 |
Correct |
279 ms |
274040 KB |
Output is correct |
10 |
Correct |
278 ms |
274044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
269624 KB |
Output is correct |
2 |
Correct |
184 ms |
269564 KB |
Output is correct |
3 |
Correct |
186 ms |
269688 KB |
Output is correct |
4 |
Correct |
184 ms |
269564 KB |
Output is correct |
5 |
Correct |
187 ms |
269560 KB |
Output is correct |
6 |
Correct |
182 ms |
269560 KB |
Output is correct |
7 |
Correct |
187 ms |
269560 KB |
Output is correct |
8 |
Correct |
179 ms |
269560 KB |
Output is correct |
9 |
Correct |
180 ms |
269612 KB |
Output is correct |
10 |
Correct |
181 ms |
269560 KB |
Output is correct |
11 |
Correct |
182 ms |
269664 KB |
Output is correct |
12 |
Correct |
181 ms |
269560 KB |
Output is correct |
13 |
Correct |
179 ms |
269560 KB |
Output is correct |
14 |
Correct |
190 ms |
269560 KB |
Output is correct |
15 |
Correct |
186 ms |
269560 KB |
Output is correct |
16 |
Correct |
179 ms |
269560 KB |
Output is correct |
17 |
Correct |
189 ms |
269560 KB |
Output is correct |
18 |
Correct |
184 ms |
269560 KB |
Output is correct |
19 |
Correct |
191 ms |
269560 KB |
Output is correct |
20 |
Correct |
180 ms |
269560 KB |
Output is correct |
21 |
Correct |
193 ms |
269560 KB |
Output is correct |
22 |
Correct |
183 ms |
269560 KB |
Output is correct |
23 |
Correct |
188 ms |
269560 KB |
Output is correct |
24 |
Correct |
181 ms |
269572 KB |
Output is correct |
25 |
Correct |
183 ms |
269560 KB |
Output is correct |
26 |
Correct |
180 ms |
269560 KB |
Output is correct |
27 |
Correct |
187 ms |
269588 KB |
Output is correct |
28 |
Correct |
182 ms |
269560 KB |
Output is correct |
29 |
Correct |
183 ms |
269560 KB |
Output is correct |
30 |
Correct |
186 ms |
269560 KB |
Output is correct |
31 |
Correct |
182 ms |
269560 KB |
Output is correct |
32 |
Correct |
180 ms |
269564 KB |
Output is correct |
33 |
Correct |
181 ms |
269560 KB |
Output is correct |
34 |
Correct |
183 ms |
269536 KB |
Output is correct |
35 |
Correct |
181 ms |
269688 KB |
Output is correct |
36 |
Correct |
187 ms |
269560 KB |
Output is correct |
37 |
Correct |
182 ms |
269688 KB |
Output is correct |
38 |
Correct |
181 ms |
269688 KB |
Output is correct |
39 |
Correct |
186 ms |
269588 KB |
Output is correct |
40 |
Correct |
183 ms |
269560 KB |
Output is correct |
41 |
Correct |
179 ms |
269560 KB |
Output is correct |
42 |
Correct |
183 ms |
269688 KB |
Output is correct |
43 |
Correct |
180 ms |
269688 KB |
Output is correct |
44 |
Correct |
185 ms |
269560 KB |
Output is correct |
45 |
Correct |
182 ms |
269560 KB |
Output is correct |
46 |
Correct |
190 ms |
269688 KB |
Output is correct |
47 |
Correct |
186 ms |
269688 KB |
Output is correct |
48 |
Correct |
186 ms |
269688 KB |
Output is correct |
49 |
Correct |
183 ms |
269560 KB |
Output is correct |
50 |
Correct |
204 ms |
269688 KB |
Output is correct |
51 |
Correct |
200 ms |
269816 KB |
Output is correct |
52 |
Correct |
182 ms |
269688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
269624 KB |
Output is correct |
2 |
Correct |
184 ms |
269564 KB |
Output is correct |
3 |
Correct |
186 ms |
269688 KB |
Output is correct |
4 |
Correct |
184 ms |
269564 KB |
Output is correct |
5 |
Correct |
187 ms |
269560 KB |
Output is correct |
6 |
Correct |
182 ms |
269560 KB |
Output is correct |
7 |
Correct |
187 ms |
269560 KB |
Output is correct |
8 |
Correct |
179 ms |
269560 KB |
Output is correct |
9 |
Correct |
180 ms |
269612 KB |
Output is correct |
10 |
Correct |
181 ms |
269560 KB |
Output is correct |
11 |
Correct |
182 ms |
269664 KB |
Output is correct |
12 |
Correct |
181 ms |
269560 KB |
Output is correct |
13 |
Correct |
179 ms |
269560 KB |
Output is correct |
14 |
Correct |
190 ms |
269560 KB |
Output is correct |
15 |
Correct |
186 ms |
269560 KB |
Output is correct |
16 |
Correct |
179 ms |
269560 KB |
Output is correct |
17 |
Correct |
189 ms |
269560 KB |
Output is correct |
18 |
Correct |
184 ms |
269560 KB |
Output is correct |
19 |
Correct |
191 ms |
269560 KB |
Output is correct |
20 |
Correct |
180 ms |
269560 KB |
Output is correct |
21 |
Correct |
193 ms |
269560 KB |
Output is correct |
22 |
Correct |
183 ms |
269560 KB |
Output is correct |
23 |
Correct |
188 ms |
269560 KB |
Output is correct |
24 |
Correct |
181 ms |
269572 KB |
Output is correct |
25 |
Correct |
183 ms |
269560 KB |
Output is correct |
26 |
Correct |
180 ms |
269560 KB |
Output is correct |
27 |
Correct |
187 ms |
269588 KB |
Output is correct |
28 |
Correct |
182 ms |
269560 KB |
Output is correct |
29 |
Correct |
183 ms |
269560 KB |
Output is correct |
30 |
Correct |
186 ms |
269560 KB |
Output is correct |
31 |
Correct |
182 ms |
269560 KB |
Output is correct |
32 |
Correct |
180 ms |
269564 KB |
Output is correct |
33 |
Correct |
181 ms |
269560 KB |
Output is correct |
34 |
Correct |
183 ms |
269536 KB |
Output is correct |
35 |
Correct |
181 ms |
269688 KB |
Output is correct |
36 |
Correct |
187 ms |
269560 KB |
Output is correct |
37 |
Correct |
187 ms |
269712 KB |
Output is correct |
38 |
Correct |
185 ms |
269560 KB |
Output is correct |
39 |
Correct |
183 ms |
269688 KB |
Output is correct |
40 |
Correct |
185 ms |
269560 KB |
Output is correct |
41 |
Correct |
193 ms |
269688 KB |
Output is correct |
42 |
Correct |
181 ms |
269560 KB |
Output is correct |
43 |
Correct |
186 ms |
269564 KB |
Output is correct |
44 |
Correct |
183 ms |
269560 KB |
Output is correct |
45 |
Correct |
186 ms |
269560 KB |
Output is correct |
46 |
Correct |
179 ms |
269560 KB |
Output is correct |
47 |
Correct |
203 ms |
269560 KB |
Output is correct |
48 |
Correct |
184 ms |
269560 KB |
Output is correct |
49 |
Correct |
181 ms |
269688 KB |
Output is correct |
50 |
Correct |
183 ms |
269560 KB |
Output is correct |
51 |
Correct |
179 ms |
269560 KB |
Output is correct |
52 |
Correct |
194 ms |
269688 KB |
Output is correct |
53 |
Correct |
198 ms |
269688 KB |
Output is correct |
54 |
Correct |
203 ms |
270072 KB |
Output is correct |
55 |
Correct |
203 ms |
270072 KB |
Output is correct |
56 |
Correct |
237 ms |
274428 KB |
Output is correct |
57 |
Correct |
237 ms |
274424 KB |
Output is correct |
58 |
Correct |
241 ms |
274424 KB |
Output is correct |
59 |
Correct |
236 ms |
274424 KB |
Output is correct |
60 |
Correct |
242 ms |
275772 KB |
Output is correct |
61 |
Correct |
237 ms |
275572 KB |
Output is correct |
62 |
Correct |
234 ms |
275704 KB |
Output is correct |
63 |
Correct |
233 ms |
275704 KB |
Output is correct |
64 |
Correct |
232 ms |
275704 KB |
Output is correct |
65 |
Correct |
238 ms |
275704 KB |
Output is correct |
66 |
Correct |
233 ms |
275812 KB |
Output is correct |
67 |
Correct |
237 ms |
275448 KB |
Output is correct |
68 |
Correct |
241 ms |
275704 KB |
Output is correct |
69 |
Correct |
187 ms |
269560 KB |
Output is correct |
70 |
Correct |
184 ms |
269560 KB |
Output is correct |
71 |
Correct |
237 ms |
275704 KB |
Output is correct |
72 |
Correct |
236 ms |
275704 KB |
Output is correct |
73 |
Correct |
240 ms |
275708 KB |
Output is correct |
74 |
Correct |
183 ms |
269564 KB |
Output is correct |
75 |
Correct |
185 ms |
269560 KB |
Output is correct |
76 |
Correct |
184 ms |
269560 KB |
Output is correct |
77 |
Correct |
187 ms |
269564 KB |
Output is correct |
78 |
Correct |
283 ms |
272736 KB |
Output is correct |
79 |
Correct |
261 ms |
274040 KB |
Output is correct |
80 |
Correct |
277 ms |
273912 KB |
Output is correct |
81 |
Correct |
233 ms |
275704 KB |
Output is correct |
82 |
Correct |
279 ms |
274040 KB |
Output is correct |
83 |
Correct |
278 ms |
274044 KB |
Output is correct |
84 |
Correct |
182 ms |
269688 KB |
Output is correct |
85 |
Correct |
181 ms |
269688 KB |
Output is correct |
86 |
Correct |
186 ms |
269588 KB |
Output is correct |
87 |
Correct |
183 ms |
269560 KB |
Output is correct |
88 |
Correct |
179 ms |
269560 KB |
Output is correct |
89 |
Correct |
183 ms |
269688 KB |
Output is correct |
90 |
Correct |
180 ms |
269688 KB |
Output is correct |
91 |
Correct |
185 ms |
269560 KB |
Output is correct |
92 |
Correct |
182 ms |
269560 KB |
Output is correct |
93 |
Correct |
190 ms |
269688 KB |
Output is correct |
94 |
Correct |
186 ms |
269688 KB |
Output is correct |
95 |
Correct |
186 ms |
269688 KB |
Output is correct |
96 |
Correct |
183 ms |
269560 KB |
Output is correct |
97 |
Correct |
204 ms |
269688 KB |
Output is correct |
98 |
Correct |
200 ms |
269816 KB |
Output is correct |
99 |
Correct |
182 ms |
269688 KB |
Output is correct |
100 |
Correct |
187 ms |
269688 KB |
Output is correct |
101 |
Correct |
188 ms |
270072 KB |
Output is correct |
102 |
Correct |
187 ms |
270072 KB |
Output is correct |
103 |
Correct |
297 ms |
274040 KB |
Output is correct |
104 |
Correct |
288 ms |
274128 KB |
Output is correct |
105 |
Correct |
286 ms |
274040 KB |
Output is correct |
106 |
Correct |
302 ms |
274040 KB |
Output is correct |
107 |
Correct |
265 ms |
274008 KB |
Output is correct |
108 |
Correct |
283 ms |
274168 KB |
Output is correct |
109 |
Correct |
239 ms |
275704 KB |
Output is correct |
110 |
Correct |
238 ms |
275704 KB |
Output is correct |
111 |
Correct |
288 ms |
273912 KB |
Output is correct |
112 |
Correct |
305 ms |
274168 KB |
Output is correct |
113 |
Correct |
259 ms |
274144 KB |
Output is correct |
114 |
Correct |
295 ms |
274040 KB |
Output is correct |
115 |
Correct |
302 ms |
274168 KB |
Output is correct |