#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 12;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
ll inv(ll i){
if(i == 1) return 1;
return (MOD - MOD / i) * inv(MOD % i) % MOD;
}
vector<ll> BIT;
int lowbit(int x){
return x & -x;
}
void modify(int x, ll v){
for(; x < BIT.size(); x += lowbit(x)){
BIT[x] += v;
}
}
void modify(int l, int r, ll v){
if(l > r) return;
modify(l, v);
modify(r + 1, -v);
}
ll query(int x){
ll ans = 0;
for(; x > 0; x -= lowbit(x)){
ans += BIT[x];
}
return ans;
}
struct seg{
int l, r, id;
bool operator<(seg b) const{
return l < b.l;
}
};
int main(){
StarBurstStream
int n;
cin >> n;
vector<pll> e;
vector<ll> a(n + 1);
for(int i = 1; i <= n; i++){
cin >> a[i];
e.eb(mp(a[i], i));
}
int m;
cin >> m;
vector<ll> X(m + 1), Y(m + 1), C(m + 1);
for(int i = 1; i <= m; i++){
ll x, y, c;
cin >> x >> y >> c;
X[i] = x; Y[i] = y; C[i] = c;
e.eb(mp(y, -i));
}
gsort(e);
set<seg> st;
vector<vector<int>> g(2);
vector<int> l(2), r(2), pr(2), dpt(2);
vector<vector<int>> star(2);
vector<vector<int>> vs(1);
int cnt = 1;
l[1] = 1; r[1] = n; pr[1] = 1; dpt[1] = 0;
vs[0].eb(1);
st.insert(seg({1, n, 1}));
auto addv = [&](int L, int R, int PR){
int id = ++cnt;
g.eb(); l.eb(); r.eb(); pr.eb(); dpt.eb(); star.eb();
g[PR].eb(id);
l[id] = L;
r[id] = R;
pr[id] = PR;
dpt[id] = dpt[PR] + 1;
if(vs.size() <= dpt[id]) vs.eb();
vs[dpt[id]].eb(id);
st.insert(seg({L, R, id}));
};
for(pll i : e){
int y = i.F;
if(i.S > 0){
int x = i.S;
auto it = st.upper_bound(seg({x}));
it--;
int id = it->id;
int l = it->l, r = it->r;
st.erase(it);
if(l < x) addv(l, x - 1, id);
if(x < r) addv(x + 1, r, id);
}
else{
i.S *= -1;
int x = X[i.S];
auto it = st.upper_bound(seg({x}));
it--;
int id = it->id;
star[id].eb(i.S);
}
}
BIT.resize(n + 1);
/*cerr << cnt << "\n";
for(int i = 1; i <= cnt; i++){
cerr << "v " << i << " " << l[i] << " " << r[i] << " ";
printv(g[i], cerr);
printv(star[i], cerr);
}*/
/*cerr << "depth\n";
for(int i = 0; i < vs.size(); i++){
cerr << i << " ";
printv(vs[i], cerr);
}*/
vector<ll> dp(cnt + 1), sum(cnt + 1);
for(int i = (int)vs.size() - 1; i >= 0; i--){
/*cerr << "dp " << i << " ";
for(int j = 1; j <= n; j++) cerr << query(j) << " ";
cerr << "\n";*/
for(int j : vs[i]){
ll total = 0;
for(int k : star[j]) total += C[k];
dp[j] = sum[j] = total;
for(int k : g[j]) dp[j] += dp[k], sum[j] += sum[k];
for(int k : star[j]){
dp[j] = min(dp[j], query(X[k]) + sum[j] - C[k]);
}
}
for(int j : vs[i]){
int p = pr[j];
int pl = l[p], pr = r[p];
int vl = l[j], vr = r[j];
modify(pl, vl - 1, - sum[j] + dp[j]);
modify(vr + 1, pr, - sum[j] + dp[j]);
}
}
//printv(sum, cerr);
//printv(dp, cerr);
cout << dp[1] << "\n";
return 0;
}
Compilation message
constellation3.cpp: In function 'void modify(int, ll)':
constellation3.cpp:75:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(; x < BIT.size(); x += lowbit(x)){
| ~~^~~~~~~~~~~~
constellation3.cpp: In lambda function:
constellation3.cpp:144:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
144 | if(vs.size() <= dpt[id]) vs.eb();
constellation3.cpp: In function 'int main()':
constellation3.cpp:150:13: warning: unused variable 'y' [-Wunused-variable]
150 | int y = i.F;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
3 ms |
700 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
3 ms |
696 KB |
Output is correct |
26 |
Correct |
3 ms |
716 KB |
Output is correct |
27 |
Correct |
2 ms |
716 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
3 ms |
704 KB |
Output is correct |
30 |
Correct |
2 ms |
716 KB |
Output is correct |
31 |
Correct |
2 ms |
716 KB |
Output is correct |
32 |
Correct |
2 ms |
716 KB |
Output is correct |
33 |
Correct |
3 ms |
716 KB |
Output is correct |
34 |
Correct |
3 ms |
844 KB |
Output is correct |
35 |
Correct |
2 ms |
716 KB |
Output is correct |
36 |
Correct |
2 ms |
716 KB |
Output is correct |
37 |
Correct |
2 ms |
716 KB |
Output is correct |
38 |
Correct |
2 ms |
844 KB |
Output is correct |
39 |
Correct |
2 ms |
716 KB |
Output is correct |
40 |
Correct |
2 ms |
708 KB |
Output is correct |
41 |
Correct |
3 ms |
716 KB |
Output is correct |
42 |
Correct |
2 ms |
716 KB |
Output is correct |
43 |
Correct |
3 ms |
836 KB |
Output is correct |
44 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
3 ms |
700 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
3 ms |
696 KB |
Output is correct |
26 |
Correct |
3 ms |
716 KB |
Output is correct |
27 |
Correct |
2 ms |
716 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
3 ms |
704 KB |
Output is correct |
30 |
Correct |
2 ms |
716 KB |
Output is correct |
31 |
Correct |
2 ms |
716 KB |
Output is correct |
32 |
Correct |
2 ms |
716 KB |
Output is correct |
33 |
Correct |
3 ms |
716 KB |
Output is correct |
34 |
Correct |
3 ms |
844 KB |
Output is correct |
35 |
Correct |
2 ms |
716 KB |
Output is correct |
36 |
Correct |
2 ms |
716 KB |
Output is correct |
37 |
Correct |
2 ms |
716 KB |
Output is correct |
38 |
Correct |
2 ms |
844 KB |
Output is correct |
39 |
Correct |
2 ms |
716 KB |
Output is correct |
40 |
Correct |
2 ms |
708 KB |
Output is correct |
41 |
Correct |
3 ms |
716 KB |
Output is correct |
42 |
Correct |
2 ms |
716 KB |
Output is correct |
43 |
Correct |
3 ms |
836 KB |
Output is correct |
44 |
Correct |
2 ms |
716 KB |
Output is correct |
45 |
Correct |
411 ms |
48032 KB |
Output is correct |
46 |
Correct |
378 ms |
47516 KB |
Output is correct |
47 |
Correct |
424 ms |
46240 KB |
Output is correct |
48 |
Correct |
448 ms |
47648 KB |
Output is correct |
49 |
Correct |
388 ms |
45716 KB |
Output is correct |
50 |
Correct |
385 ms |
45744 KB |
Output is correct |
51 |
Correct |
397 ms |
45748 KB |
Output is correct |
52 |
Correct |
391 ms |
48092 KB |
Output is correct |
53 |
Correct |
400 ms |
47780 KB |
Output is correct |
54 |
Correct |
197 ms |
50444 KB |
Output is correct |
55 |
Correct |
216 ms |
49280 KB |
Output is correct |
56 |
Correct |
195 ms |
48140 KB |
Output is correct |
57 |
Correct |
201 ms |
45992 KB |
Output is correct |
58 |
Correct |
168 ms |
48808 KB |
Output is correct |
59 |
Correct |
181 ms |
48940 KB |
Output is correct |
60 |
Correct |
153 ms |
55852 KB |
Output is correct |
61 |
Correct |
215 ms |
44008 KB |
Output is correct |
62 |
Correct |
229 ms |
51572 KB |
Output is correct |
63 |
Correct |
222 ms |
43088 KB |
Output is correct |
64 |
Correct |
218 ms |
43132 KB |
Output is correct |
65 |
Correct |
189 ms |
51508 KB |
Output is correct |
66 |
Correct |
220 ms |
43476 KB |
Output is correct |