#include<bits/stdc++.h>
#include "shoes.h"
using namespace std;
struct segtree {
int n;
vector<int> st;
segtree(int a) {
n=a;
while (n&(n-1)) n++;
vector<int> b(2*n);
swap(b, st);
}
int query(int ql, int qr, int v=1, int nl=1, int nr=-1) {
if (nr==-1) { nr=this->n; ql++; qr++; }
if (ql>nr || qr<nl) return 0;
if (ql<=nl && qr>=nr) return st[v];
return query(ql, qr, 2*v, nl, (nl+nr)/2) + query(ql, qr, 2*v+1, (nl+nr)/2+1, nr);
}
void update(int p) {
for (p+=n; p>0; p/=2) st[p]++;
}
int operator[](int a) { return st[a+n]; }
};
long long count_swaps(vector<int> s) {
int n = s.size();
queue<int> left[n+1], right[n+1];
segtree st(n);
for (int i = 0; i < n; ++i) {
if (s[i]<0) {
left[-s[i]].push(i);
} else right[s[i]].push(i);
}
long long ans=0;
for (int i = 0; i < n; ++i) {
while (i<n && st[i]) i++;
if (i>=n) break;
if (s[i]<0) {
ans+=right[-s[i]].front()-i-1-st.query(i, right[-s[i]].front());
st.update(right[-s[i]].front());
right[-s[i]].pop(); left[-s[i]].pop();
} else {
ans+=left[s[i]].front()-i-st.query(i, left[s[i]].front());
st.update(left[s[i]].front());
left[s[i]].pop(); right[s[i]].pop();
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
600 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
432 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
544 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
452 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
2 ms |
3164 KB |
Output is correct |
19 |
Correct |
2 ms |
3160 KB |
Output is correct |
20 |
Correct |
18 ms |
27740 KB |
Output is correct |
21 |
Correct |
20 ms |
27740 KB |
Output is correct |
22 |
Correct |
191 ms |
275420 KB |
Output is correct |
23 |
Correct |
174 ms |
275176 KB |
Output is correct |
24 |
Correct |
190 ms |
275280 KB |
Output is correct |
25 |
Correct |
183 ms |
274988 KB |
Output is correct |
26 |
Correct |
199 ms |
275264 KB |
Output is correct |
27 |
Correct |
176 ms |
275268 KB |
Output is correct |
28 |
Correct |
180 ms |
275204 KB |
Output is correct |
29 |
Correct |
216 ms |
275420 KB |
Output is correct |
30 |
Correct |
181 ms |
275412 KB |
Output is correct |
31 |
Correct |
183 ms |
275284 KB |
Output is correct |
32 |
Correct |
185 ms |
275400 KB |
Output is correct |
33 |
Correct |
197 ms |
275208 KB |
Output is correct |
34 |
Correct |
195 ms |
275376 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
187 ms |
275284 KB |
Output is correct |
38 |
Correct |
184 ms |
275392 KB |
Output is correct |
39 |
Correct |
196 ms |
275280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
436 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
233 ms |
274356 KB |
Output is correct |
6 |
Correct |
201 ms |
274512 KB |
Output is correct |
7 |
Correct |
224 ms |
274512 KB |
Output is correct |
8 |
Correct |
182 ms |
275360 KB |
Output is correct |
9 |
Correct |
222 ms |
274520 KB |
Output is correct |
10 |
Correct |
220 ms |
274560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
600 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
432 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
356 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
600 KB |
Output is correct |
40 |
Correct |
1 ms |
1116 KB |
Output is correct |
41 |
Correct |
2 ms |
2908 KB |
Output is correct |
42 |
Correct |
2 ms |
3164 KB |
Output is correct |
43 |
Correct |
2 ms |
3160 KB |
Output is correct |
44 |
Correct |
2 ms |
3164 KB |
Output is correct |
45 |
Correct |
2 ms |
3000 KB |
Output is correct |
46 |
Correct |
3 ms |
3164 KB |
Output is correct |
47 |
Correct |
2 ms |
2904 KB |
Output is correct |
48 |
Correct |
2 ms |
3164 KB |
Output is correct |
49 |
Correct |
2 ms |
3164 KB |
Output is correct |
50 |
Correct |
2 ms |
3164 KB |
Output is correct |
51 |
Correct |
2 ms |
3160 KB |
Output is correct |
52 |
Correct |
2 ms |
2904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
600 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
432 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
544 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
452 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
604 KB |
Output is correct |
51 |
Correct |
1 ms |
1116 KB |
Output is correct |
52 |
Correct |
2 ms |
3164 KB |
Output is correct |
53 |
Correct |
2 ms |
3160 KB |
Output is correct |
54 |
Correct |
18 ms |
27740 KB |
Output is correct |
55 |
Correct |
20 ms |
27740 KB |
Output is correct |
56 |
Correct |
191 ms |
275420 KB |
Output is correct |
57 |
Correct |
174 ms |
275176 KB |
Output is correct |
58 |
Correct |
190 ms |
275280 KB |
Output is correct |
59 |
Correct |
183 ms |
274988 KB |
Output is correct |
60 |
Correct |
199 ms |
275264 KB |
Output is correct |
61 |
Correct |
176 ms |
275268 KB |
Output is correct |
62 |
Correct |
180 ms |
275204 KB |
Output is correct |
63 |
Correct |
216 ms |
275420 KB |
Output is correct |
64 |
Correct |
181 ms |
275412 KB |
Output is correct |
65 |
Correct |
183 ms |
275284 KB |
Output is correct |
66 |
Correct |
185 ms |
275400 KB |
Output is correct |
67 |
Correct |
197 ms |
275208 KB |
Output is correct |
68 |
Correct |
195 ms |
275376 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
1 ms |
348 KB |
Output is correct |
71 |
Correct |
187 ms |
275284 KB |
Output is correct |
72 |
Correct |
184 ms |
275392 KB |
Output is correct |
73 |
Correct |
196 ms |
275280 KB |
Output is correct |
74 |
Correct |
0 ms |
344 KB |
Output is correct |
75 |
Correct |
0 ms |
436 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
344 KB |
Output is correct |
78 |
Correct |
233 ms |
274356 KB |
Output is correct |
79 |
Correct |
201 ms |
274512 KB |
Output is correct |
80 |
Correct |
224 ms |
274512 KB |
Output is correct |
81 |
Correct |
182 ms |
275360 KB |
Output is correct |
82 |
Correct |
222 ms |
274520 KB |
Output is correct |
83 |
Correct |
220 ms |
274560 KB |
Output is correct |
84 |
Correct |
1 ms |
356 KB |
Output is correct |
85 |
Correct |
1 ms |
344 KB |
Output is correct |
86 |
Correct |
0 ms |
600 KB |
Output is correct |
87 |
Correct |
1 ms |
1116 KB |
Output is correct |
88 |
Correct |
2 ms |
2908 KB |
Output is correct |
89 |
Correct |
2 ms |
3164 KB |
Output is correct |
90 |
Correct |
2 ms |
3160 KB |
Output is correct |
91 |
Correct |
2 ms |
3164 KB |
Output is correct |
92 |
Correct |
2 ms |
3000 KB |
Output is correct |
93 |
Correct |
3 ms |
3164 KB |
Output is correct |
94 |
Correct |
2 ms |
2904 KB |
Output is correct |
95 |
Correct |
2 ms |
3164 KB |
Output is correct |
96 |
Correct |
2 ms |
3164 KB |
Output is correct |
97 |
Correct |
2 ms |
3164 KB |
Output is correct |
98 |
Correct |
2 ms |
3160 KB |
Output is correct |
99 |
Correct |
2 ms |
2904 KB |
Output is correct |
100 |
Correct |
2 ms |
3064 KB |
Output is correct |
101 |
Correct |
23 ms |
27880 KB |
Output is correct |
102 |
Correct |
19 ms |
27736 KB |
Output is correct |
103 |
Correct |
245 ms |
274676 KB |
Output is correct |
104 |
Correct |
243 ms |
274568 KB |
Output is correct |
105 |
Correct |
219 ms |
274516 KB |
Output is correct |
106 |
Correct |
217 ms |
274512 KB |
Output is correct |
107 |
Correct |
225 ms |
274512 KB |
Output is correct |
108 |
Correct |
197 ms |
274540 KB |
Output is correct |
109 |
Correct |
184 ms |
275280 KB |
Output is correct |
110 |
Correct |
186 ms |
275184 KB |
Output is correct |
111 |
Correct |
241 ms |
274812 KB |
Output is correct |
112 |
Correct |
219 ms |
274480 KB |
Output is correct |
113 |
Correct |
201 ms |
274512 KB |
Output is correct |
114 |
Correct |
228 ms |
274436 KB |
Output is correct |
115 |
Correct |
223 ms |
274564 KB |
Output is correct |