Submission #514912

#TimeUsernameProblemLanguageResultExecution timeMemory
514912wiwihoConstellation 3 (JOI20_constellation3)C++14
100 / 100
448 ms55852 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...