제출 #1038218

#제출 시각아이디문제언어결과실행 시간메모리
1038218LudisseyFire (BOI24_fire)C++17
0 / 100
1 ms348 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<pair<int,int>> a; vector<int> depth; vector<int> tree; 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); 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; if(p>=0&&p!=i) child[p].push_back(i); else roots.push_back(i); } for (int i = 0; i < sz(roots); i++){ tree[roots[i]]=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||tree[p]!=tree[i]||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...