-
Notifications
You must be signed in to change notification settings - Fork 0
/
C2.cpp
262 lines (193 loc) · 5.29 KB
/
C2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/****************************** begin /template.h ******************************/
/***********************************************************************
* [?] APIO 2021 Template
* [+] Made By: Lior Yehezkely, FiveSixFiveFour
* [~] Apples are ORZ
* *********************************************************************/
/*** begin #define flags ***/
// #define USE_ORDERED_STATISTICS
// #define BIG_BIGINT
/*** end #define flags ***/
/*** begin includes ***/
#include <bits/stdc++.h>
#ifdef USE_ORDERED_STATISTICS
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#endif
using namespace std;
/*** end includes ***/
/*** begin typedefs ***/
typedef long long int ll;
#ifdef BIG_BIGINT
typedef __int128_t bigint;
#else
typedef ll bigint;
#endif
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpi> vvpi;
typedef vector<vpl> vvpl;
typedef set<int> si;
typedef multiset<int> msi;
typedef set<ll> sl;
typedef multiset<ll> msl;
typedef long double ld;
template<class T> using func = function<T>;
#ifdef USE_ORDERED_STATISTICS
typedef tree<
pl,
null_type,
less<pl>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#endif
/*** end typedefs ***/
/*** begin #defines ***/
#define clrcin cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define GOGOGO ios::sync_with_stdio(false); cin.tie(nullptr);
#define BYEBYE return 0;
#define all(cn) (cn).begin(), (cn).end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define csz(c) ((int)c.size())
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define popcnt __builtin_popcount
#define popcntll __builtin_popcount_ll
#define what(x) cout << #x << " = " << x << '\n'
/*** end #defines ***/
const int INFI = 1e9 + 5;
const ll INFL = 1e18 + 5;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
auto dist = uniform_int_distribution<int>(0, INFI);
auto distll = uniform_int_distribution<ll>(0, INFL);
int rnd() { return dist(rng); }
ll rndl() { return distll(rng); }
/****************************** end /template.h ******************************/
//#define BRUH_WHY_TESTCASES
//#define PREC 7
//#define USACO "sleepy"
// orz orz orz orz orz orzo rzo
const int maxn = 2e5 +5;
struct Segtree {
int tl; int tr; int tm; ll val; Segtree* l; Segtree* r; ll lazy;
Segtree(int _tl, int _tr) {
tl=_tl; tr=_tr; tm = tl+(tr-tl)/2; l=r=nullptr; val=0; lazy = 0;
}
void pull() {
val = min(l->val, r->val);
}
void push() {
if(l == nullptr || r==nullptr) return;
if(lazy) {
l->val += lazy; r->val += lazy; l->lazy += lazy; r->lazy += lazy; lazy = 0;
}
}
void build() { // NO INITIAL ARRAY
if(tl == tr) {
val = 0;
} else {
l = new Segtree(tl, tm); r = new Segtree(tm+1, tr);
l->build(); r->build();
pull();
}
}
void build(ll* x) { // INITIAL ARRAY
if(tl == tr) {
val = x[tl];
} else {
l = new Segtree(tl, tm); r = new Segtree(tm+1, tr);
l->build(x); r->build(x);
pull();
}
}
void update(int ql, int qr, ll v) {
if(ql > qr || qr < tl || ql > tr) return;
if(ql == tl && qr == tr) {
val += v; lazy += v; return;
} else {
push();
l->update(ql, min(tm, qr), v); r->update(max(ql,tm+1), qr, v);
pull();
}
}
ll query(int ql, int qr) {
if(ql > qr || qr < tl || ql > tr) return INFL;
if(ql == tl && qr == tr) return val;
push();
return min(l->query(ql, min(tm, qr)), r->query(max(ql,tm+1), qr));
}
};
ll pre[maxn];
int n;
ll a[maxn];
bitset<maxn> vis = 0;
int res = 0;
void solve() {
cin >> n;
rep(i, n) {
cin >> a[i];
}
rep(i, n) {
if(i>0) {
pre[i] = pre[i-1];
}
if(a[i] >= 0) {
res++;
pre[i] += a[i];
vis[i] = true;
}
}
Segtree st(0, n-1);
st.build(pre);
multiset<pl> ds;
rep(i, n) {
if(!vis[i]) {
ds.insert({a[i], i});
}
}
while(!ds.empty()) {
auto c = *ds.rbegin(); ds.erase(ds.find(c));
auto mn = st.query(c.se, n-1);
if(mn + c.fi >= 0) {
res++;
st.update(c.se, n-1, c.fi);
}
}
cout << res << '\n';
}
signed main()
{
#ifdef USACO
freopen(USACO ".in", "r", stdin);
freopen(USACO ".out", "w", stdout);
#endif
GOGOGO
#ifdef PREC
cout << fixed << setprecision(PREC);
#endif
int t=1;
#ifdef BRUH_WHY_TESTCASES
cin >> t;
#endif
while(t--)
{
solve();
}
BYEBYE
}
/* PLEASE READ THIS
* N = 1
* GUESS A!!!!!!!
* DO SOMETHING INSTEAD OF NOTHING
*/