제출 #730206

#제출 시각아이디문제언어결과실행 시간메모리
730206Sam_a17Reconstruction Project (JOI22_reconstruction)C++17
49 / 100
5040 ms431076 KiB
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> //#include "temp.cpp" #include <cstdio> using namespace std; #ifndef ONLINE_JUDGE #define dbg(x) cerr << #x <<" "; print(x); cerr << endl; #else #define dbg(x) #endif #define sz(x) (int((x).size())) #define len(x) (int)x.length() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define clr(x) (x).clear() #define uniq(x) x.resize(unique(all(x)) - x.begin()); #define blt(x) __builtin_popcount(x) #define pb push_back #define popf pop_front #define popb pop_back void print(long long t) {cerr << t;} void print(int t) {cerr << t;} void print(string t) {cerr << t;} void print(char t) {cerr << t;} void print(double t) {cerr << t;} void print(long double t) {cerr << t;} void print(unsigned long long t) {cerr << t;} template <class T, class V> void print(pair <T, V> p); template <class T> void print(vector <T> v); template <class T> void print(set <T> v); template <class T, class V> void print(map <T, V> v); template <class T> void print(multiset <T> v); template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";} template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";} template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";} #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; #define nl '\n' // for grid problems int dx[8] = {-1,0,1,0,1,-1,1,-1}; int dy[8] = {0,1,0,-1,1,1,-1,-1}; // lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6 void fastIO() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } // file in/out void setIO(string str = "") { fastIO(); if (str != "") { freopen((str + ".in").c_str(), "r", stdin); freopen((str + ".out").c_str(), "w", stdout); } } // Indexed Set template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 5e2 + 10, M = 2e5 + 10; vector<pair<int, long long>> adj[N]; int p[N], sz[N], n, m, q, comp, its[N]; vector<int> pref[M], suf[M]; // dsu void init() { comp = n; for(int i = 1; i <= n; i++) { p[i] = i, sz[i] = 1; } } int find(int a) { if(a != p[a]) { p[a] = find(p[a]); } return p[a]; } int same(int a, int b) { if(find(a) == find(b)) { return 1; } return 0; } int merge(int a, int b) { a = find(a), b = find(b); if(a == b) { return 0; } if(sz[b] > sz[a]) { swap(a, b); } comp--; p[b] = a, sz[a] += sz[b]; return 1; } vector<pair<int, pair<int, int>>> edges; long long get(int l, int r, long long x) { // dbg(l) // dbg(r) int li = 0, ri = 0; long long answ = 0; init(); while(comp != 1) { if(li < sz(pref[l]) && ri < sz(suf[r])) { int i1 = pref[l][li], i2 = suf[r][ri]; long long first = abs(edges[i1].first - x); long long second = abs(edges[i2].first - x); if(first > second) { if(merge(edges[i2].second.first, edges[i2].second.second)) { answ += second; } ri++; } else { if(merge(edges[i1].second.first, edges[i1].second.second)) { answ += first; } li++; } } else if(li < sz(pref[l])) { int i1 = pref[l][li]; long long first = abs(edges[i1].first - x); if(merge(edges[i1].second.first, edges[i1].second.second)) { answ += first; } li++; } else { int i2 = suf[r][ri]; long long second = abs(edges[i2].first - x); if(merge(edges[i2].second.first, edges[i2].second.second)) { answ += second; } ri++; } } return answ; } void solve_() { cin >> n >> m; int okk = 1; for(int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; if(y != x + 1) { okk = 0; } edges.push_back({z, {x, y}}); adj[x].push_back({z, y}); } sort(all(edges)); for(int i = 1; i <= m; i++) { init(); pref[i].push_back(i - 1); merge(edges[i - 1].second.first, edges[i - 1].second.second); for(auto j: pref[i - 1]) { if(merge(edges[j].second.first, edges[j].second.second)) { pref[i].push_back(j); } } } for(int i = m; i >= 1; i--) { init(); suf[i].push_back(i - 1); merge(edges[i - 1].second.first, edges[i - 1].second.second); for(auto j: suf[i + 1]) { if(merge(edges[j].second.first, edges[j].second.second)) { suf[i].push_back(j); } } } for(int i = 1; i <= n; i++) { sort(all(adj[i])); } cin >> q; int st = 0, bt = 0; while(q--) { int x; cin >> x; if(okk) { long long answ = 0; if(st == 0) { for(int i = 1; i < n; i++) { auto it = lower_bound(all(adj[i]), make_pair(x, 0ll)) - adj[i].begin(); its[i] = it; } } for(int i = 1; i < n; i++) { while(its[i] < sz(adj[i]) && adj[i][its[i]].first < x) { its[i]++; } int mn = 1e9; if(its[i] < sz(adj[i])) { mn = min(mn, abs(adj[i][its[i]].first - x)); } if(its[i] > 0) { mn = min(mn, abs(adj[i][its[i] - 1].first - x)); } answ += mn; } cout << answ << '\n'; st++; } else { while(bt < m - 1 && edges[bt].first < x) { bt++; } cout << get(bt, bt + 1, x) << '\n'; } } } int main() { setIO(); auto solve = [&](int test_case)-> void { while(test_case--) { solve_(); } }; int test_cases = 1; // cin >> test_cases; solve(test_cases); return 0; }

컴파일 시 표준 에러 (stderr) 메시지

reconstruction.cpp: In function 'void setIO(std::string)':
reconstruction.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...