제출 #1159850

#제출 시각아이디문제언어결과실행 시간메모리
1159850FIFI_cpp별자리 3 (JOI20_constellation3)C++20
0 / 100
1 ms2112 KiB
#include <bits/stdc++.h> #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <cstdlib> #include <cmath> #include <queue> #include <stack> #include <deque> #include <fstream> #include <iterator> #include <set> #include <map> #include <unordered_map> #include <iomanip> #include <cctype> #include <string> #include <cassert> #include <set> #include <bitset> #include <unordered_set> #include <numeric> #define all(a) a.begin(), a.end() #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define pb push_back #define ppi pair<int,pair<int,int>> #define int int64_t using namespace std; // /\_/\ // (= ._.) // / > \> // encouraging cat const int INF = 10000000000000000; //const int mod = 1000000007; const int mod = 998244353; const int MAXN = 200005; //ifstream fin('xor.in'); //ofstream fout('xor.out'); const int K = 25; int n; long long st[K + 1][MAXN]; int lg[MAXN+1]; void precompute() { lg[1] = 0; for (int i = 2; i <= MAXN; i++) lg[i] = lg[i/2] + 1; } void init(vector<int> array) { copy(array.begin(), array.end(), st[0]); for (int i = 1; i <= K; i++) { for (int j = 0; j + (1 << i) <= n; j++) { st[i][j] = max(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]); } } } int query(int l,int r) { int i = lg[r - l + 1]; int maximum = max(st[i][l], st[i][r - (1 << i) + 1]); return maximum; } vector<multiset<vector<int>>> stars; vector<int> a,val, dp, diff, taken; int res = 0; signed main() { cin >> n; a.resize(n); dp.resize(n,0); precompute(); vector<pair<int,int>> b(n); vector<pair<int,int>> lr(n, {-1,-1}); vector<int> pref(n,0), suf(n,0); int curr = 0; for (int i = 0;i < n;i++) { cin >> a[i]; b[i].first = a[i]; b[i].second = i; curr = max(curr, a[i]); pref[i] = curr; } init(a); curr = 0; for (int i = n - 1;i >= 0;i--) { curr = max(curr, a[i]); suf[i] = curr; } sort(all(b)); //a.push_back(INF); int M; cin >> M; stars.resize(n); val.resize(M,0); diff.resize(n,0); taken.resize(n,0); int sum = 0; for (int i = 0;i < M;i++) { int x,y,c; cin >> x >> y >> c; x--; stars[x].insert({y,c, i}); val[i] = c; sum += c; } //solve(-1, n, INF); vector<bool> visited(n, false); for (int i = 0;i < n;i++) { int pos = b[i].second; int M = b[i].first; int l = lr[b[i].second].first; int r = lr[b[i].second].second; visited[b[i].second] = true; if (l != -1) { if (stars[l].size() > stars[pos].size()) { swap(stars[l], stars[pos]); } stars[pos].insert(all(stars[l])); } if (r != -1) { if (stars[r].size() > stars[pos].size()) { swap(stars[r], stars[pos]); } stars[pos].insert(all(stars[r])); } int lb = -1,rb = -1; int lo = 0, hi = pos - 1, mid = (lo + hi) / 2; while (lo < hi) { mid = (lo + hi + 1) / 2; if (query(mid, pos - 1) >= M) { lo = mid; } else { hi = mid - 1; } } if (query(lo, pos - 1) < M || visited[lo]) { lo = -1; } lb = lo; lo = pos + 1, hi = n - 1, mid = (lo + hi + 1) / 2; while (lo < hi) { mid = (lo + hi) / 2; if (query(pos + 1, mid) >= M) { hi = mid; } else { lo = mid + 1; } } if (query(pos + 1, lo) < M | visited[lo]) { lo = -1; } rb = lo; int limit = INF; int dir = 0; // 0 -> n/a 1-> left 2-> right if (lb != -1) { if (a[lb] < limit) { limit = a[lb]; dir = 1; } } if (rb != -1) { if (a[rb] < limit) { limit = a[rb]; dir = 2; } } int Lans = 0; int Rans = 0; if (l != -1) { Lans = dp[l]; } if (r != -1) { Rans = dp[r]; } int ad = 0; int s = 0; for (auto i : stars[pos]) { if (i[0] > limit) { break; } s += val[i[2]]; ad = max(ad, val[i[2]]); } int high = 0; for (auto i: stars[pos]) { if (i[0] <= limit) { continue; } high = max(high, val[i[2]]); val[i[2]] -= ad; } res = max(res, max(Lans + Rans + high, Lans + Rans + ad)); dp[pos] = Lans + Rans + ad; if (dir == 1) { lr[lb].second = pos; } else if (dir == 2) { lr[rb].first = pos; } auto it = stars[pos].lower_bound({limit, INF, INF}); stars[pos].erase(stars[pos].begin(), it); } cout << sum - res << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...