#include "bubblesort2.h"
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class P , class Q > ostream& operator<<(ostream& stream, pair < P , Q > v){ stream << "(" << v.fi << ',' << v.se << ")"; return stream;}
template < class T > ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream;}
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
const int oo = 1e9;
const int N = 4e6 + 5;
int s[N];
int p[N];
int q[N];
int t[N];
int ok[N];
int lz[N];
int get(int node) {
if (ok[node])
return t[node];
return 0;
}
void upd(int node) {
ok[node] = ok[node * 2] + ok[node * 2 + 1];
t[node] = max(get(node * 2),get(node * 2 + 1));
}
void upd_lz(int l,int r,int node) {
if (!lz[node])
return;
if (l != r)
lz[node * 2] += lz[node],
lz[node * 2 + 1] += lz[node];
t[node] += lz[node];
lz[node] = 0;
return;
}
vector< pii > w;
void build(int l,int r,int node) {
if (l == r) {
t[node] = w[l - 1].se;
} else {
int m = (l + r) / 2;
build(l,m,node * 2);
build(m+1,r,node * 2 + 1);
}
}
void Update1(int l,int r,int pos,int node) {
upd_lz(l,r,node);
if (l == r) {
ok[node] ^= 1;
}
else {
int m = (l + r) / 2;
if (pos <= m)
Update1(l,m,pos,node * 2),upd_lz(m+1,r,node * 2 + 1);
else
Update1(m+1,r,pos,node * 2 + 1),upd_lz(l,m,node * 2);
upd(node);
}
}
void Update2(int l,int r,int l1,int r1,int v,int node) {
upd_lz(l,r,node);
if (l1 <= l && r <= r1)
lz[node] += v,upd_lz(l,r,node);
else {
int m = (l + r) / 2;
if (l1 <= m)
Update2(l,m,l1,r1,v,node * 2);
else
upd_lz(l,m,node * 2);
if (m+1<=r1)
Update2(m+1,r,l1,r1,v,node * 2 + 1);
upd(node);
}
}
int query(int l,int r,int l1,int r1,int node) {
upd_lz(l,r,node);
if (l1 <= l && r <= r1)
return get(node);
int m = (l + r) / 2;
int ans = 0;
if (l1 <= m)
smax(ans,query(l,m,l1,r1,node * 2));
else
upd_lz(l,m,node * 2);
if (m+1<=r1)
smax(ans,query(m+1,r,l1,r1,node * 2 + 1));
else
upd_lz(m+1,r,node * 2 + 1);
upd(node);
return ans;
}
void print(int l,int r,int node) {
upd_lz(l,r,node);
if (l != r) {
int m = (l + r) / 2;
print(l,m,node * 2);
print(m+1,r,node * 2 + 1);
} else {
dbg((vi{l,get(node),w[l - 1].fi,w[l - 1].se,ok[node]}));
}
//dbg((vi{l,r,node,t[node],ok[node]}));
}
vi countScans(vi S1,vi S2,vi S3){
int n = S1.size();
for (int i = 0;i < n;++i)
w.pb(mp(S1[i],i + 1));
for (int i = 0;i < S2.size();++i)
w.pb(mp(S3[i],S2[i] + 1));
sort(all(w));
w.resize(unique(all(w)) - w.begin());
const int SZ = w.size();
vector < pii > qq;
for (int i = 0;i < S2.size();++i)
qq.pb(mp(S3[i],S2[i] + 1));
for (int i = 0;i < n;++i)
s[i + 1] = S1[i];
vi answer;
build(1,SZ,1);
for (int i = 1;i <= n;++i) {
int Who = lower_bound(all(w),mp(s[i],i)) - w.begin() + 1;
Update1(1,SZ,Who,1);
Update2(1,SZ,Who,SZ,-1,1);
}
for (auto it : qq) {
int Who = lower_bound(all(w),it) - w.begin() + 1;
int Del = lower_bound(all(w),mp(s[it.se],it.se)) - w.begin() + 1;
Update1(1,SZ,Del,1);
Update2(1,SZ,Del,SZ,1,1);
Update1(1,SZ,Who,1);
Update2(1,SZ,Who,SZ,-1,1);
answer.pb(t[1]);
s[it.se] = it.fi;
}
return answer;
}
Compilation message
bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:135:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0;i < S2.size();++i)
~~^~~~~~~~~~~
bubblesort2.cpp:141:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0;i < S2.size();++i)
~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
580 KB |
Output is correct |
2 |
Correct |
6 ms |
656 KB |
Output is correct |
3 |
Correct |
8 ms |
908 KB |
Output is correct |
4 |
Correct |
9 ms |
976 KB |
Output is correct |
5 |
Correct |
9 ms |
1072 KB |
Output is correct |
6 |
Correct |
8 ms |
1072 KB |
Output is correct |
7 |
Correct |
8 ms |
1100 KB |
Output is correct |
8 |
Correct |
8 ms |
1204 KB |
Output is correct |
9 |
Correct |
7 ms |
1252 KB |
Output is correct |
10 |
Correct |
10 ms |
1316 KB |
Output is correct |
11 |
Correct |
7 ms |
1408 KB |
Output is correct |
12 |
Correct |
8 ms |
1452 KB |
Output is correct |
13 |
Correct |
8 ms |
1488 KB |
Output is correct |
14 |
Correct |
8 ms |
1528 KB |
Output is correct |
15 |
Correct |
7 ms |
1568 KB |
Output is correct |
16 |
Correct |
8 ms |
1608 KB |
Output is correct |
17 |
Correct |
7 ms |
1648 KB |
Output is correct |
18 |
Correct |
7 ms |
1672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
580 KB |
Output is correct |
2 |
Correct |
6 ms |
656 KB |
Output is correct |
3 |
Correct |
8 ms |
908 KB |
Output is correct |
4 |
Correct |
9 ms |
976 KB |
Output is correct |
5 |
Correct |
9 ms |
1072 KB |
Output is correct |
6 |
Correct |
8 ms |
1072 KB |
Output is correct |
7 |
Correct |
8 ms |
1100 KB |
Output is correct |
8 |
Correct |
8 ms |
1204 KB |
Output is correct |
9 |
Correct |
7 ms |
1252 KB |
Output is correct |
10 |
Correct |
10 ms |
1316 KB |
Output is correct |
11 |
Correct |
7 ms |
1408 KB |
Output is correct |
12 |
Correct |
8 ms |
1452 KB |
Output is correct |
13 |
Correct |
8 ms |
1488 KB |
Output is correct |
14 |
Correct |
8 ms |
1528 KB |
Output is correct |
15 |
Correct |
7 ms |
1568 KB |
Output is correct |
16 |
Correct |
8 ms |
1608 KB |
Output is correct |
17 |
Correct |
7 ms |
1648 KB |
Output is correct |
18 |
Correct |
7 ms |
1672 KB |
Output is correct |
19 |
Correct |
25 ms |
2536 KB |
Output is correct |
20 |
Correct |
28 ms |
2708 KB |
Output is correct |
21 |
Correct |
29 ms |
2904 KB |
Output is correct |
22 |
Correct |
32 ms |
3092 KB |
Output is correct |
23 |
Correct |
27 ms |
3416 KB |
Output is correct |
24 |
Correct |
27 ms |
3536 KB |
Output is correct |
25 |
Correct |
29 ms |
3700 KB |
Output is correct |
26 |
Correct |
25 ms |
3792 KB |
Output is correct |
27 |
Correct |
27 ms |
4020 KB |
Output is correct |
28 |
Correct |
28 ms |
4048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
4772 KB |
Output is correct |
2 |
Correct |
120 ms |
7040 KB |
Output is correct |
3 |
Correct |
266 ms |
10300 KB |
Output is correct |
4 |
Correct |
217 ms |
10908 KB |
Output is correct |
5 |
Correct |
244 ms |
11436 KB |
Output is correct |
6 |
Correct |
267 ms |
12012 KB |
Output is correct |
7 |
Correct |
262 ms |
12592 KB |
Output is correct |
8 |
Correct |
248 ms |
13172 KB |
Output is correct |
9 |
Correct |
212 ms |
13752 KB |
Output is correct |
10 |
Correct |
173 ms |
13752 KB |
Output is correct |
11 |
Correct |
159 ms |
13752 KB |
Output is correct |
12 |
Correct |
153 ms |
14300 KB |
Output is correct |
13 |
Correct |
220 ms |
14908 KB |
Output is correct |
14 |
Correct |
159 ms |
15488 KB |
Output is correct |
15 |
Correct |
165 ms |
16196 KB |
Output is correct |
16 |
Correct |
142 ms |
16844 KB |
Output is correct |
17 |
Correct |
150 ms |
17480 KB |
Output is correct |
18 |
Correct |
162 ms |
18116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
580 KB |
Output is correct |
2 |
Correct |
6 ms |
656 KB |
Output is correct |
3 |
Correct |
8 ms |
908 KB |
Output is correct |
4 |
Correct |
9 ms |
976 KB |
Output is correct |
5 |
Correct |
9 ms |
1072 KB |
Output is correct |
6 |
Correct |
8 ms |
1072 KB |
Output is correct |
7 |
Correct |
8 ms |
1100 KB |
Output is correct |
8 |
Correct |
8 ms |
1204 KB |
Output is correct |
9 |
Correct |
7 ms |
1252 KB |
Output is correct |
10 |
Correct |
10 ms |
1316 KB |
Output is correct |
11 |
Correct |
7 ms |
1408 KB |
Output is correct |
12 |
Correct |
8 ms |
1452 KB |
Output is correct |
13 |
Correct |
8 ms |
1488 KB |
Output is correct |
14 |
Correct |
8 ms |
1528 KB |
Output is correct |
15 |
Correct |
7 ms |
1568 KB |
Output is correct |
16 |
Correct |
8 ms |
1608 KB |
Output is correct |
17 |
Correct |
7 ms |
1648 KB |
Output is correct |
18 |
Correct |
7 ms |
1672 KB |
Output is correct |
19 |
Correct |
25 ms |
2536 KB |
Output is correct |
20 |
Correct |
28 ms |
2708 KB |
Output is correct |
21 |
Correct |
29 ms |
2904 KB |
Output is correct |
22 |
Correct |
32 ms |
3092 KB |
Output is correct |
23 |
Correct |
27 ms |
3416 KB |
Output is correct |
24 |
Correct |
27 ms |
3536 KB |
Output is correct |
25 |
Correct |
29 ms |
3700 KB |
Output is correct |
26 |
Correct |
25 ms |
3792 KB |
Output is correct |
27 |
Correct |
27 ms |
4020 KB |
Output is correct |
28 |
Correct |
28 ms |
4048 KB |
Output is correct |
29 |
Correct |
41 ms |
4772 KB |
Output is correct |
30 |
Correct |
120 ms |
7040 KB |
Output is correct |
31 |
Correct |
266 ms |
10300 KB |
Output is correct |
32 |
Correct |
217 ms |
10908 KB |
Output is correct |
33 |
Correct |
244 ms |
11436 KB |
Output is correct |
34 |
Correct |
267 ms |
12012 KB |
Output is correct |
35 |
Correct |
262 ms |
12592 KB |
Output is correct |
36 |
Correct |
248 ms |
13172 KB |
Output is correct |
37 |
Correct |
212 ms |
13752 KB |
Output is correct |
38 |
Correct |
173 ms |
13752 KB |
Output is correct |
39 |
Correct |
159 ms |
13752 KB |
Output is correct |
40 |
Correct |
153 ms |
14300 KB |
Output is correct |
41 |
Correct |
220 ms |
14908 KB |
Output is correct |
42 |
Correct |
159 ms |
15488 KB |
Output is correct |
43 |
Correct |
165 ms |
16196 KB |
Output is correct |
44 |
Correct |
142 ms |
16844 KB |
Output is correct |
45 |
Correct |
150 ms |
17480 KB |
Output is correct |
46 |
Correct |
162 ms |
18116 KB |
Output is correct |
47 |
Correct |
780 ms |
39088 KB |
Output is correct |
48 |
Correct |
3553 ms |
81376 KB |
Output is correct |
49 |
Correct |
3791 ms |
96176 KB |
Output is correct |
50 |
Correct |
3773 ms |
109196 KB |
Output is correct |
51 |
Correct |
3538 ms |
122048 KB |
Output is correct |
52 |
Correct |
3598 ms |
134960 KB |
Output is correct |
53 |
Correct |
3455 ms |
147952 KB |
Output is correct |
54 |
Correct |
3133 ms |
160764 KB |
Output is correct |
55 |
Correct |
3526 ms |
174060 KB |
Output is correct |
56 |
Correct |
3149 ms |
186856 KB |
Output is correct |
57 |
Correct |
3238 ms |
200048 KB |
Output is correct |
58 |
Correct |
3096 ms |
212960 KB |
Output is correct |
59 |
Correct |
2761 ms |
224852 KB |
Output is correct |
60 |
Correct |
2860 ms |
236576 KB |
Output is correct |
61 |
Correct |
2942 ms |
247136 KB |
Output is correct |
62 |
Correct |
2705 ms |
256984 KB |
Output is correct |
63 |
Correct |
2723 ms |
266884 KB |
Output is correct |
64 |
Correct |
2596 ms |
276360 KB |
Output is correct |
65 |
Correct |
2322 ms |
285344 KB |
Output is correct |
66 |
Correct |
2837 ms |
295728 KB |
Output is correct |
67 |
Correct |
2510 ms |
305368 KB |
Output is correct |