#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18;
int A[250005];
int B[250005];
int C[250005];
int D[250005];
vector<array<int, 2>> V;
int ans = -INF;
int K;
int F[250005];
struct SegTree {
struct Node {
int l, r, cnt, sum;
Node():l(-1),r(-1),cnt(0),sum(0) {}
Node(int c) : l(-1), r(-1), cnt(1), sum(c) {}
};
vector<Node> seg;
vector<int> root;
int MAX;
void init(int N) {
int i;
for(i=1;i<N;i*=2) {}
MAX = i;
seg.clear();
root.clear();
seg.push_back(Node());
root.push_back(0);
cons(0, 0, MAX);
}
void cons(int n, int s, int e) {
seg[n].cnt = 0, seg[n].sum = 0;
int mid = s + e >> 1;
//cout << s << ' ' << e << ' ' << v << '\n';
if(s != mid) {
seg[n].l = seg.size();
seg.push_back(Node());
cons(seg[n].l, s, mid);
seg[n].r = seg.size();
seg.push_back(Node());
cons(seg[n].r, mid, e);
}
//cout << s << ' ' << e << ' ' << v << '\n';
//cout << seg[v].l <<' ' << seg[v].r << '\n';
}
void add(int k, int v, int n2, int n, int ns, int ne) {
seg[n2].l = seg[n].l;
seg[n2].r = seg[n].r;
seg[n2].cnt = seg[n].cnt + 1;
seg[n2].sum = seg[n].sum + v;
//cout << k << ' ' << v << ' ' << n << ' ' << ns << ' ' << ne << '\n';
if(ns+1==ne) {
return;
}
int mid = ns + ne >> 1;
if(mid <= k) {
seg[n2].r = seg.size();
seg.push_back(Node());
add(k, v, seg[n2].r, seg[n].r, mid, ne);
}
else {
seg[n2].l = seg.size();
seg.push_back(Node());
add(k, v, seg[n2].l, seg[n].l, ns, mid);
}
}
void add_node(int k, int v) {
//cout << "ADD:" << k << ' ' << v << '\n';
int prv = root.back();
root.push_back(seg.size());
seg.push_back(Node());
add(k, v, root.back(), prv, 0, MAX);
}
int get_val(int n1, int n2, int ns, int ne, int k) {
//cout << n1 << ' ' << n2 <<' ' << ns << ' ' << ne << ' ' << k << '\n';
if(k==0) return 0;
if(ns+1==ne) return seg[n1].sum - seg[n2].sum;
int r1 = seg[n1].r, r2 = seg[n2].r;
assert(r1 != -1 && r2 != -1);
int v = seg[r1].cnt - seg[r2].cnt;
if(v==k) {
int res = seg[r1].sum - seg[r2].sum;
//cout << n1 << ' ' << n2 << ' ' << res << '\n';
return res;
}
int mid = ns + ne >> 1;
if(v < k) {
int res = seg[r1].sum - seg[r2].sum + get_val(seg[n1].l, seg[n2].l, ns, mid, k-v);
//cout << n1 << ' ' << n2 << ' ' << res << '\n';
return res;
}
int res = get_val(r1, r2, mid, ne, k);
//cout << n1 << ' ' << n2 << ' ' << res << '\n';
return res;
}
int get_calc(int s, int e, int k) {
int res = get_val(root[e+1], root[s], 0, MAX, k);
//cout << s+1 << ' ' << e+1 << " : " << res << '\n';
return res;
}
};
SegTree tree;
int G[250005];
void tree_cons(int N) {
int i, j;
tree.init(N+3);
/*cout << "init done\n";
for(i=0;i<tree.seg.size();i++) {
cout << i << " : " << tree.seg[i].l <<' ' << tree.seg[i].r << ' ' << tree.seg[i].cnt <<' ' << tree.seg[i].sum << '\n';
}
for(i=0;i<tree.root.size();i++) cout <<tree.root[i] << ' ';
cout << '\n';*/
vector<array<int, 2>> V2;
for(i=0;i<N;i++) V2.push_back({B[i], i});
sort(V2.begin(),V2.end());
for(i=0;i<N;i++) G[V2[i][1]] = i;
for(i=0;i<N;i++) tree.add_node(G[i], B[i]);
/*for(i=0;i<tree.seg.size();i++) {
cout << i << " : " << tree.seg[i].l <<' ' << tree.seg[i].r << ' ' << tree.seg[i].cnt <<' ' << tree.seg[i].sum << '\n';
}
for(i=0;i<tree.root.size();i++) cout <<tree.root[i] << ' ';
cout << '\n';*/
}
int calc_val(int s, int e) {
int res = tree.get_calc(s, e, K) - (F[e] - (s-1>=0?F[s-1]:0));
//cout << s+1 <<' ' << e+1 << " : " << res <<'\n';
return res;
}
void calc(int n, int s, int e) {
int i, j;
int ma = -INF, id = -1;
int sum = 0;
for(i=s;i<=e;i++) {
if(n+K-1>s) continue;
if(calc_val(n, i) == ans) {
V.push_back({n, i});
}
}
}
void DnC(int s, int e, int a, int b) {
if(s>e) return;
int i, j;
a = max(a, s+K-1);
int mid = s + e >> 1;
int ma = -INF, id = b;
//cout <<"DNC: " << s << ' ' << e << ' ' << a << ' ' << b << '\n';
for(i=max(mid+K-1, a); i <= b; i++) {
int v = calc_val(mid, i);
if(v > ma) {
ma = v;
id = i;
}
}
C[mid] = ma, D[mid] = id;
if(ma > ans) ans = ma;
DnC(s, mid-1, a, id);
DnC(mid+1, e, id, b);
}
int E[250005];
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
cin >> N >> K;
int i, j;
for(i=0;i<N;i++) cin >> A[i];
for(i=0;i<N;i++) F[i] = A[i];
for(i=1;i<N;i++) F[i] += F[i-1];
for(i=0;i<N;i++) cin >> B[i];
tree_cons(N);
DnC(0, N-1, 0, N-1);
vector<int> pos;
for(i=0;i<N;i++) {
if(C[i]==ans) pos.push_back(i);
}
pos.push_back(N);
D[N] = N;
//cout << ans << '\n';
for(int i=0;i<pos.size();i++) {
int n = pos[i];
if(n==N) continue;
V.push_back({n, D[n]});
calc(pos[i], D[pos[i]], D[pos[i+1]]-1);
}
vector<vector<array<int, 2>>> in, out;
in.resize(N);
out.resize(N);
int cnt = 0;
for(auto k : V) {
//k[0] ~ k[1]
// kth element <= a[i] -> ?
int v = (ans + F[k[1]] - (k[0]-1>=0?F[k[0]-1]:0)) - tree.get_calc(k[0], k[1], K-1);
in[k[0]].push_back({v, cnt});
out[k[1]].push_back({v, cnt});
cnt++;
}
/*for(i=0;i<N;i++) cout << D[i] <<' ';
cout << '\n';
for(auto k : V) cout << k[0]+1 << ' ' << k[1]+1 << '\n';*/
cout << ans <<'\n';
set<array<int, 2>> S;
for(i=0;i<N;i++) {
for(auto n : in[i]) {
S.insert(n);
}
if(!S.empty() && B[i] >= (*S.begin())[0]) cout << "1";
else cout << "0";
for(auto n : out[i]) {
S.erase(n);
}
}
}
Compilation message
trade.cpp: In member function 'void SegTree::cons(long long int, long long int, long long int)':
trade.cpp:34:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | int mid = s + e >> 1;
| ~~^~~
trade.cpp: In member function 'void SegTree::add(long long int, long long int, long long int, long long int, long long int, long long int)':
trade.cpp:56:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = ns + ne >> 1;
| ~~~^~~~
trade.cpp: In member function 'long long int SegTree::get_val(long long int, long long int, long long int, long long int, long long int)':
trade.cpp:87:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | int mid = ns + ne >> 1;
| ~~~^~~~
trade.cpp: In function 'void tree_cons(long long int)':
trade.cpp:106:12: warning: unused variable 'j' [-Wunused-variable]
106 | int i, j;
| ^
trade.cpp: In function 'void calc(long long int, long long int, long long int)':
trade.cpp:131:12: warning: unused variable 'j' [-Wunused-variable]
131 | int i, j;
| ^
trade.cpp:132:9: warning: unused variable 'ma' [-Wunused-variable]
132 | int ma = -INF, id = -1;
| ^~
trade.cpp:132:20: warning: unused variable 'id' [-Wunused-variable]
132 | int ma = -INF, id = -1;
| ^~
trade.cpp:133:9: warning: unused variable 'sum' [-Wunused-variable]
133 | int sum = 0;
| ^~~
trade.cpp: In function 'void DnC(long long int, long long int, long long int, long long int)':
trade.cpp:145:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
145 | int mid = s + e >> 1;
| ~~^~~
trade.cpp:143:12: warning: unused variable 'j' [-Wunused-variable]
143 | int i, j;
| ^
trade.cpp: In function 'int main()':
trade.cpp:181:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
181 | for(int i=0;i<pos.size();i++) {
| ~^~~~~~~~~~~
trade.cpp:167:12: warning: unused variable 'j' [-Wunused-variable]
167 | int i, j;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12636 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12808 KB |
Output is correct |
5 |
Correct |
2 ms |
12888 KB |
Output is correct |
6 |
Correct |
1 ms |
12892 KB |
Output is correct |
7 |
Correct |
1 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
1 ms |
12892 KB |
Output is correct |
10 |
Correct |
1 ms |
12892 KB |
Output is correct |
11 |
Partially correct |
1 ms |
12892 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12808 KB |
Output is correct |
5 |
Correct |
2 ms |
12888 KB |
Output is correct |
6 |
Correct |
1 ms |
12892 KB |
Output is correct |
7 |
Correct |
1 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
1 ms |
12892 KB |
Output is correct |
10 |
Correct |
1 ms |
12892 KB |
Output is correct |
11 |
Partially correct |
1 ms |
12892 KB |
Partially correct |
12 |
Correct |
1 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12632 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12756 KB |
Output is correct |
16 |
Correct |
1 ms |
12892 KB |
Output is correct |
17 |
Correct |
1 ms |
12892 KB |
Output is correct |
18 |
Correct |
1 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
1 ms |
12892 KB |
Output is correct |
21 |
Correct |
2 ms |
12888 KB |
Output is correct |
22 |
Partially correct |
1 ms |
12892 KB |
Partially correct |
23 |
Correct |
5 ms |
16284 KB |
Output is correct |
24 |
Correct |
11 ms |
17104 KB |
Output is correct |
25 |
Correct |
9 ms |
16336 KB |
Output is correct |
26 |
Correct |
13 ms |
17360 KB |
Output is correct |
27 |
Correct |
8 ms |
16848 KB |
Output is correct |
28 |
Correct |
7 ms |
16848 KB |
Output is correct |
29 |
Correct |
7 ms |
16168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12636 KB |
Output is correct |
2 |
Correct |
314 ms |
283152 KB |
Output is correct |
3 |
Correct |
361 ms |
283052 KB |
Output is correct |
4 |
Correct |
647 ms |
282768 KB |
Output is correct |
5 |
Correct |
659 ms |
283604 KB |
Output is correct |
6 |
Correct |
522 ms |
283084 KB |
Output is correct |
7 |
Correct |
929 ms |
283260 KB |
Output is correct |
8 |
Correct |
336 ms |
282524 KB |
Output is correct |
9 |
Correct |
380 ms |
283788 KB |
Output is correct |
10 |
Correct |
402 ms |
283792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12636 KB |
Output is correct |
2 |
Correct |
314 ms |
283152 KB |
Output is correct |
3 |
Correct |
361 ms |
283052 KB |
Output is correct |
4 |
Correct |
647 ms |
282768 KB |
Output is correct |
5 |
Correct |
659 ms |
283604 KB |
Output is correct |
6 |
Correct |
522 ms |
283084 KB |
Output is correct |
7 |
Correct |
929 ms |
283260 KB |
Output is correct |
8 |
Correct |
336 ms |
282524 KB |
Output is correct |
9 |
Correct |
380 ms |
283788 KB |
Output is correct |
10 |
Correct |
402 ms |
283792 KB |
Output is correct |
11 |
Correct |
1 ms |
12636 KB |
Output is correct |
12 |
Correct |
329 ms |
284208 KB |
Output is correct |
13 |
Correct |
376 ms |
283356 KB |
Output is correct |
14 |
Correct |
664 ms |
283012 KB |
Output is correct |
15 |
Correct |
704 ms |
282840 KB |
Output is correct |
16 |
Correct |
515 ms |
284384 KB |
Output is correct |
17 |
Correct |
900 ms |
283164 KB |
Output is correct |
18 |
Correct |
331 ms |
283048 KB |
Output is correct |
19 |
Correct |
353 ms |
283032 KB |
Output is correct |
20 |
Correct |
375 ms |
284060 KB |
Output is correct |
21 |
Correct |
1 ms |
12632 KB |
Output is correct |
22 |
Correct |
1 ms |
12636 KB |
Output is correct |
23 |
Correct |
1 ms |
12892 KB |
Output is correct |
24 |
Correct |
2 ms |
12892 KB |
Output is correct |
25 |
Correct |
2 ms |
12892 KB |
Output is correct |
26 |
Correct |
2 ms |
12892 KB |
Output is correct |
27 |
Correct |
2 ms |
12892 KB |
Output is correct |
28 |
Correct |
2 ms |
12892 KB |
Output is correct |
29 |
Correct |
1 ms |
12892 KB |
Output is correct |
30 |
Partially correct |
1 ms |
12892 KB |
Partially correct |
31 |
Correct |
399 ms |
283084 KB |
Output is correct |
32 |
Correct |
855 ms |
283040 KB |
Output is correct |
33 |
Correct |
854 ms |
282768 KB |
Output is correct |
34 |
Correct |
629 ms |
283756 KB |
Output is correct |
35 |
Correct |
516 ms |
284384 KB |
Output is correct |
36 |
Correct |
893 ms |
283124 KB |
Output is correct |
37 |
Correct |
454 ms |
284332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12636 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12632 KB |
Output is correct |
4 |
Correct |
1 ms |
12636 KB |
Output is correct |
5 |
Correct |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12808 KB |
Output is correct |
7 |
Correct |
2 ms |
12888 KB |
Output is correct |
8 |
Correct |
1 ms |
12892 KB |
Output is correct |
9 |
Correct |
1 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
1 ms |
12892 KB |
Output is correct |
12 |
Correct |
1 ms |
12892 KB |
Output is correct |
13 |
Partially correct |
1 ms |
12892 KB |
Partially correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12632 KB |
Output is correct |
16 |
Correct |
1 ms |
12636 KB |
Output is correct |
17 |
Correct |
1 ms |
12756 KB |
Output is correct |
18 |
Correct |
1 ms |
12892 KB |
Output is correct |
19 |
Correct |
1 ms |
12892 KB |
Output is correct |
20 |
Correct |
1 ms |
12892 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
1 ms |
12892 KB |
Output is correct |
23 |
Correct |
2 ms |
12888 KB |
Output is correct |
24 |
Partially correct |
1 ms |
12892 KB |
Partially correct |
25 |
Correct |
5 ms |
16284 KB |
Output is correct |
26 |
Correct |
11 ms |
17104 KB |
Output is correct |
27 |
Correct |
9 ms |
16336 KB |
Output is correct |
28 |
Correct |
13 ms |
17360 KB |
Output is correct |
29 |
Correct |
8 ms |
16848 KB |
Output is correct |
30 |
Correct |
7 ms |
16848 KB |
Output is correct |
31 |
Correct |
7 ms |
16168 KB |
Output is correct |
32 |
Correct |
1 ms |
12636 KB |
Output is correct |
33 |
Correct |
314 ms |
283152 KB |
Output is correct |
34 |
Correct |
361 ms |
283052 KB |
Output is correct |
35 |
Correct |
647 ms |
282768 KB |
Output is correct |
36 |
Correct |
659 ms |
283604 KB |
Output is correct |
37 |
Correct |
522 ms |
283084 KB |
Output is correct |
38 |
Correct |
929 ms |
283260 KB |
Output is correct |
39 |
Correct |
336 ms |
282524 KB |
Output is correct |
40 |
Correct |
380 ms |
283788 KB |
Output is correct |
41 |
Correct |
402 ms |
283792 KB |
Output is correct |
42 |
Correct |
1 ms |
12636 KB |
Output is correct |
43 |
Correct |
329 ms |
284208 KB |
Output is correct |
44 |
Correct |
376 ms |
283356 KB |
Output is correct |
45 |
Correct |
664 ms |
283012 KB |
Output is correct |
46 |
Correct |
704 ms |
282840 KB |
Output is correct |
47 |
Correct |
515 ms |
284384 KB |
Output is correct |
48 |
Correct |
900 ms |
283164 KB |
Output is correct |
49 |
Correct |
331 ms |
283048 KB |
Output is correct |
50 |
Correct |
353 ms |
283032 KB |
Output is correct |
51 |
Correct |
375 ms |
284060 KB |
Output is correct |
52 |
Correct |
1 ms |
12632 KB |
Output is correct |
53 |
Correct |
1 ms |
12636 KB |
Output is correct |
54 |
Correct |
1 ms |
12892 KB |
Output is correct |
55 |
Correct |
2 ms |
12892 KB |
Output is correct |
56 |
Correct |
2 ms |
12892 KB |
Output is correct |
57 |
Correct |
2 ms |
12892 KB |
Output is correct |
58 |
Correct |
2 ms |
12892 KB |
Output is correct |
59 |
Correct |
2 ms |
12892 KB |
Output is correct |
60 |
Correct |
1 ms |
12892 KB |
Output is correct |
61 |
Partially correct |
1 ms |
12892 KB |
Partially correct |
62 |
Correct |
399 ms |
283084 KB |
Output is correct |
63 |
Correct |
855 ms |
283040 KB |
Output is correct |
64 |
Correct |
854 ms |
282768 KB |
Output is correct |
65 |
Correct |
629 ms |
283756 KB |
Output is correct |
66 |
Correct |
516 ms |
284384 KB |
Output is correct |
67 |
Correct |
893 ms |
283124 KB |
Output is correct |
68 |
Correct |
454 ms |
284332 KB |
Output is correct |
69 |
Correct |
1 ms |
12636 KB |
Output is correct |
70 |
Correct |
323 ms |
282884 KB |
Output is correct |
71 |
Correct |
382 ms |
282592 KB |
Output is correct |
72 |
Correct |
629 ms |
284308 KB |
Output is correct |
73 |
Correct |
606 ms |
283540 KB |
Output is correct |
74 |
Correct |
480 ms |
284312 KB |
Output is correct |
75 |
Correct |
734 ms |
282868 KB |
Output is correct |
76 |
Correct |
301 ms |
282772 KB |
Output is correct |
77 |
Correct |
310 ms |
283544 KB |
Output is correct |
78 |
Correct |
365 ms |
283632 KB |
Output is correct |
79 |
Correct |
1 ms |
12636 KB |
Output is correct |
80 |
Correct |
2 ms |
12636 KB |
Output is correct |
81 |
Correct |
2 ms |
12892 KB |
Output is correct |
82 |
Correct |
2 ms |
12892 KB |
Output is correct |
83 |
Correct |
1 ms |
12892 KB |
Output is correct |
84 |
Correct |
1 ms |
12892 KB |
Output is correct |
85 |
Correct |
2 ms |
12892 KB |
Output is correct |
86 |
Correct |
1 ms |
12892 KB |
Output is correct |
87 |
Correct |
1 ms |
12892 KB |
Output is correct |
88 |
Partially correct |
2 ms |
12888 KB |
Partially correct |
89 |
Correct |
365 ms |
283160 KB |
Output is correct |
90 |
Correct |
635 ms |
283424 KB |
Output is correct |
91 |
Correct |
687 ms |
283536 KB |
Output is correct |
92 |
Correct |
555 ms |
282768 KB |
Output is correct |
93 |
Correct |
482 ms |
282772 KB |
Output is correct |
94 |
Correct |
661 ms |
284172 KB |
Output is correct |
95 |
Correct |
430 ms |
284048 KB |
Output is correct |
96 |
Correct |
5 ms |
16596 KB |
Output is correct |
97 |
Correct |
9 ms |
17192 KB |
Output is correct |
98 |
Correct |
10 ms |
16336 KB |
Output is correct |
99 |
Correct |
11 ms |
17104 KB |
Output is correct |
100 |
Correct |
7 ms |
17360 KB |
Output is correct |
101 |
Correct |
6 ms |
16848 KB |
Output is correct |
102 |
Correct |
6 ms |
16280 KB |
Output is correct |
103 |
Correct |
389 ms |
283204 KB |
Output is correct |
104 |
Correct |
502 ms |
284304 KB |
Output is correct |
105 |
Correct |
585 ms |
283528 KB |
Output is correct |
106 |
Correct |
792 ms |
283028 KB |
Output is correct |
107 |
Correct |
189 ms |
283284 KB |
Output is correct |
108 |
Correct |
436 ms |
283792 KB |
Output is correct |
109 |
Correct |
442 ms |
284572 KB |
Output is correct |
110 |
Correct |
474 ms |
284308 KB |
Output is correct |
111 |
Correct |
229 ms |
282768 KB |
Output is correct |
112 |
Partially correct |
589 ms |
282584 KB |
Partially correct |