Submission #1867043


Source Code Expand

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

map<int,set<ii> > R;
map<int,set<ii> > C;
map<int,vector<int> > rows;
map<int,vector<int> > cols;
ii pt[111111];
ll ans;

int calcrow(int x, int l, int r)
{
	if(rows.find(x)==rows.end()) return 0;
	int lb = lower_bound(rows[x].begin(),rows[x].end(),l)-rows[x].begin();
	int rb = lower_bound(rows[x].begin(),rows[x].end(),r+1)-rows[x].begin();
	return rb-lb;
}

int calccol(int x, int l, int r)
{
	if(cols.find(x)==cols.end()) return 0;
	int lb = lower_bound(cols[x].begin(),cols[x].end(),l)-cols[x].begin();
	int rb = lower_bound(cols[x].begin(),cols[x].end(),r+1)-cols[x].begin();
	return rb-lb;
}

int d=0;
bool vis[111111];

void process(int u)
{
	vis[u]=1;
	int x=pt[u].fi; int y=pt[u].se;
	ans+=calcrow(x-d,y-d,y+d);
	ans+=calcrow(x+d,y-d,y+d);
	ans+=calccol(y-d,x-d+1,x+d-1);
	ans+=calccol(y+d,x-d+1,x+d-1);
	vi Q;
	if(!R[x-d].empty())
	{
		auto it = R[x-d].lower_bound(mp(y-d,-1));
		while(it!=R[x-d].end())
		{
			if((*it).fi<=y+d)
			{
				Q.pb((*it).se); it++;
				//C[(*it).se].erase(mp(x-d,(*it).se));
				//it=R[x-d].erase(it);				
			}
			else break;
		}
	}
	if(!R[x+d].empty())
	{
		auto it = R[x+d].lower_bound(mp(y-d,-1));
		while(it!=R[x+d].end())
		{
			if((*it).fi<=y+d)
			{
				Q.pb((*it).se);
				//C[(*it).se].erase(mp(x+d,(*it).se));
				it++;			
			}
			else break;
		}
	}
	if(!C[y+d].empty())
	{
		auto it = C[y+d].lower_bound(mp(x-d+1,-1));
		while(it!=C[y+d].end())
		{
			if((*it).fi<=x+d-1)
			{
				Q.pb((*it).se);
				//R[(*it).se].erase(mp(y+d,(*it).se));
				it++;			
			}
			else break;
		}
	}
	if(!C[y-d].empty())
	{
		auto it = C[y-d].lower_bound(mp(x-d+1,-1));
		while(it!=C[y-d].end())
		{
			if((*it).fi<=x+d-1)
			{
				Q.pb((*it).se);
				//R[(*it).se].erase(mp(y-d,(*it).se));
				//it=C[y-d].erase(it);				
				it++;
			}
			else break;
		}
	}
	for(int i=0;i<Q.size();i++) 
	{
		if(!vis[Q[i]]) process(Q[i]);
	}
}
 
int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n,s1,s2; cin>>n>>s1>>s2; s1--; s2--;
	for(int i=0;i<n;i++)
	{
		int x,y; cin>>x>>y;
		int px,py; px=x-y; py=x+y;
		pt[i].fi=px; pt[i].se=py;
		rows[px].pb(py); cols[py].pb(px);
		if(i!=s1) 
		{
			R[px].insert(mp(py,i)); C[py].insert(mp(px,i));
		}
	}
	for(auto it = rows.begin(); it != rows.end(); it++)
	{
		sort(it->se.begin(), it->se.end());
	}
	for(auto it = cols.begin(); it != cols.end(); it++)
	{
		sort(it->se.begin(), it->se.end());
	}
	d=max(abs(pt[s1].fi-pt[s2].fi),abs(pt[s1].se-pt[s2].se));
	process(s1);
	ans>>=1;
	cout<<ans<<'\n';
}

Submission Info

Submission Time
Task E - Manhattan Compass
User zscoder
Language C++14 (GCC 5.4.1)
Score 0
Code Size 3201 Byte
Status TLE
Exec Time 3181 ms
Memory 377788 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 900
Status
AC × 3
AC × 18
TLE × 10
Set Name Test Cases
Sample subtask0_0.txt, subtask0_1.txt, subtask0_2.txt
All subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask1_0.txt, subtask1_1.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt, subtask1_18.txt, subtask1_19.txt, subtask1_2.txt, subtask1_20.txt, subtask1_21.txt, subtask1_22.txt, subtask1_23.txt, subtask1_24.txt, subtask1_3.txt, subtask1_4.txt, subtask1_5.txt, subtask1_6.txt, subtask1_7.txt, subtask1_8.txt, subtask1_9.txt
Case Name Status Exec Time Memory
subtask0_0.txt AC 1 ms 256 KB
subtask0_1.txt AC 1 ms 256 KB
subtask0_2.txt AC 1 ms 256 KB
subtask1_0.txt TLE 3181 ms 351544 KB
subtask1_1.txt TLE 3181 ms 359660 KB
subtask1_10.txt AC 809 ms 52736 KB
subtask1_11.txt AC 100 ms 12032 KB
subtask1_12.txt AC 188 ms 30844 KB
subtask1_13.txt AC 198 ms 30844 KB
subtask1_14.txt AC 863 ms 75136 KB
subtask1_15.txt AC 208 ms 12032 KB
subtask1_16.txt TLE 3180 ms 333248 KB
subtask1_17.txt TLE 3181 ms 345112 KB
subtask1_18.txt AC 662 ms 61952 KB
subtask1_19.txt AC 100 ms 12032 KB
subtask1_2.txt AC 837 ms 84736 KB
subtask1_20.txt TLE 3180 ms 337832 KB
subtask1_21.txt TLE 3180 ms 338576 KB
subtask1_22.txt AC 577 ms 54656 KB
subtask1_23.txt AC 450 ms 39936 KB
subtask1_24.txt TLE 3181 ms 363984 KB
subtask1_3.txt AC 240 ms 19968 KB
subtask1_4.txt TLE 3180 ms 377788 KB
subtask1_5.txt TLE 3180 ms 344108 KB
subtask1_6.txt AC 616 ms 51072 KB
subtask1_7.txt AC 278 ms 27648 KB
subtask1_8.txt AC 183 ms 30460 KB
subtask1_9.txt TLE 3181 ms 345220 KB