Submission #14203

#TimeUsernameProblemLanguageResultExecution timeMemory
14203gs14004개구리 (KOI13_frog)C++14
22 / 22
194 ms11360 KiB
#include <cstdio> #include <utility> #include <algorithm> #include <vector> #include <queue> using namespace std; typedef pair<int,int> pi; const int TSIZE = (1<<18) + 1; struct pos{int x, y, num;}; bool operator<(pos a, pos b){return a.x < b.x;} int n,r,d; pos a[100005]; vector<int> vx, vy; int vis[100005]; vector<int> graph[100005]; queue<int> q; struct rmq{ int lim; pi tree[TSIZE]; void init(int n){ for(lim = 1; lim <= n; lim <<= 1); fill(tree,tree+TSIZE,pi(-1e9,-1)); } void add(int x, pi v){ x += lim; tree[x] = max(tree[x],v); while(x > 1){ x >>= 1; tree[x] = max(tree[2*x],tree[2*x+1]); } } pi q(int s, int e){ s += lim; e += lim; pi v(-1e9,-1); while(s < e){ if(s%2 == 1) v = max(v,tree[s++]); if(e%2 == 0) v = max(v,tree[e--]); s >>= 1; e >>= 1; } if(s == e) v = max(v,tree[s]); return v; } }rmq; bool cmp1(pos a, pos b){ return a.x < b.x; } bool cmp2(pos a, pos b){ return a.y < b.y; } bool cmp3(pos a, pos b){ return a.num < b.num; } void const_graph(){ sort(a,a+n,cmp1); rmq.init(n); for (int i=0; i<n; i++){ int low = (int)(lower_bound(vy.begin(),vy.end(),vy[a[i].y]-r) - vy.begin()); int high = (int)(lower_bound(vy.begin(),vy.end(),vy[a[i].y]) - vy.begin()); pi t = rmq.q(low,high-1); if(t.first >= vx[a[i].x] - r - d){ graph[a[i].num].push_back(t.second); graph[t.second].push_back(a[i].num); } low = (int)(lower_bound(vy.begin(),vy.end(),vy[a[i].y]) - vy.begin()); high = (int)(lower_bound(vy.begin(),vy.end(),vy[a[i].y]+r+1) - vy.begin()); t = rmq.q(low,high-1); if(t.first >= vx[a[i].x] - r - d){ graph[a[i].num].push_back(t.second); graph[t.second].push_back(a[i].num); } rmq.add(a[i].y,pi(vx[a[i].x],a[i].num)); } sort(a,a+n,cmp2); rmq.init(n); for (int i=0; i<n; i++) { int low = (int)(lower_bound(vx.begin(),vx.end(),vx[a[i].x]-r) - vx.begin()); int high = (int)(lower_bound(vx.begin(),vx.end(),vx[a[i].x]) - vx.begin()); pi t = rmq.q(low,high-1); if(t.first >= vy[a[i].y] - r - d){ graph[a[i].num].push_back(t.second); graph[t.second].push_back(a[i].num); } low = (int)(lower_bound(vx.begin(),vx.end(),vx[a[i].x]) - vx.begin()); high = (int)(lower_bound(vx.begin(),vx.end(),vx[a[i].x]+r+1) - vx.begin()); t = rmq.q(low,high-1); if(t.first >= vy[a[i].y] - r - d){ graph[a[i].num].push_back(t.second); graph[t.second].push_back(a[i].num); } rmq.add(a[i].x,pi(vy[a[i].y],a[i].num)); } } int main(){ scanf("%d %d",&n,&r); for (int i=0; i<n; i++) { scanf("%d %d",&a[i].x,&a[i].y); vx.push_back(a[i].x); vy.push_back(a[i].y); a[i].num = i; } sort(vx.begin(),vx.end()); sort(vy.begin(),vy.end()); vx.resize(unique(vx.begin(),vx.end()) - vx.begin()); vy.resize(unique(vy.begin(),vy.end()) - vy.begin()); for (int i=0; i<n; i++) { a[i].x = (int)(lower_bound(vx.begin(),vx.end(),a[i].x) - vx.begin()); a[i].y = (int)(lower_bound(vy.begin(),vy.end(),a[i].y) - vy.begin()); } scanf("%d",&d); const_graph(); sort(a,a+n,cmp3); for (int i=0; i<n; i++) { if(a[i].x == 0 && a[i].y == 0){ q.push(i); vis[i] = 1; } } int ret = 0; while (!q.empty()) { int qf = q.front(); q.pop(); ret = max(ret,vx[a[qf].x] + vy[a[qf].y] + 2 * r); for (int i=0; i<graph[qf].size(); i++) { int t = graph[qf][i]; if(vis[t]) continue; q.push(t); vis[t] = 1; } } printf("%d",ret); }
#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...