Submission #1038304

#TimeUsernameProblemLanguageResultExecution timeMemory
1038304LudisseyFire (BOI24_fire)C++17
100 / 100
597 ms164608 KiB
#include <bits/stdc++.h> #define int long long #define sz(a) (int)a.size() #define all(a) a.begin(), a.end() using namespace std; struct node { int sum,index; node* left, *right; void update() { sum = max(left->sum,right->sum); if(left->sum > right->sum) index=left->index; else index=right->index; } node(){ sum=0; index=0; left=NULL; right=NULL; } }; pair<int,int> query(node* root, int left, int right, int qLeft, int qRight) { if (left > qRight || right < qLeft) return {-1e9,-1}; if (left >= qLeft && right <= qRight) return {root->sum, root->index}; int mid = (left + right) / 2; pair<int,int> l=query(root->left, left, mid, qLeft, qRight); pair<int,int> r=query(root->right, mid + 1, right, qLeft, qRight); if(l.first>r.first) return l; else return r; } void build(node* root, int left, int right, const vector<pair<int,int>>& v) { if (left == right) { root->sum = v[left].first; root->index=v[left].second; return; } int mid = (left + right) / 2; root->left = new node(); root->right = new node(); build(root->left, left, mid, move(v)); build(root->right, mid+1, right, move(v)); root->update(); } node* root; vector<vector<int>> parent; const int LOG=31; vector<int> depth; vector<int> tree; bool lca(int a, int b){ if(tree[a]!=tree[b]||depth[b]<depth[a]) return false; int d=depth[b]-depth[a]; for (int j = LOG-1; j>=0; j--) { if(d&(1<<j)) b=parent[b][j]; } return (a==b); } vector<pair<int,int>> a; vector<vector<int>> child; int n,m; void dfs(int x, int d){ depth[x]=d; for (auto u : child[x]) { tree[u]=tree[x]; dfs(u,d+1); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; a.resize(n); depth.resize(n); tree.resize(n); parent.resize(n,vector<int>(LOG)); child.resize(n); unordered_map<int,int> conv; set<int> cord; cord.insert(0); cord.insert(m); for (int i = 0; i < n; i++){ cin >> a[i].first >> a[i].second; cord.insert(a[i].first); cord.insert(a[i].second); if(a[i].first>a[i].second) { a[i].second+=m; cord.insert(a[i].second); } } int x=0; for (auto u : cord) { conv[u]=x; x++; } vector<pair<int,int>> buildSEGTREE(x,{-1e9,-1}); sort(a.begin(),a.end()); for (int i = 0; i < n; i++) { if(buildSEGTREE[conv[a[i].first]].first<conv[a[i].second]){ buildSEGTREE[conv[a[i].first]]={conv[a[i].second],i}; } } root=new node(); build(root, 0,x-1,move(buildSEGTREE)); vector<int> roots; for (int i = 0; i < n; i++) { int p=query(root,0,x-1,conv[a[i].first],conv[a[i].second]).second; parent[i][0]=i; if(p>=0&&p!=i) { child[p].push_back(i); parent[i][0]=p; } else roots.push_back(i); } for (int j = 1; j < LOG; j++) { for (int i = 0; i < n; i++) { parent[i][j]=parent[parent[i][j-1]][j-1]; } } for (int i = 0; i < sz(roots); i++){ dfs(roots[i],0); } int mn=1e12; for (int i = 0; i < n; i++){ if(a[i].second>=m){ int p=query(root,0,x-1,conv[0],conv[(a[i].second-m)]).second; if(p==-1||i==p||!lca(i,p)) continue; mn=min(abs(depth[p]-depth[i])+1,mn); } } if(mn==1e12) mn=-1; cout << mn << "\n"; return 0; } /* 10 240 3 68 7 126 40 121 58 152 73 139 89 166 121 197 126 311 212 271 237 350 */
#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...