Problem:
Problem J
New Game AI
Aoba is a beginner programmer who works for a game company. She was appointed to develop a battle strategy for the enemy AI (Artificial Intelligence) in a new game. In this game, each character has two parameters, hit point ($hp$) and defence point ($dp$). No two characters have the same $hp$ and $dp$ at the same time. The player forms a party by selecting one or more characters to battle with the enemy. Aoba decided to develop a strategy in which the AI attacks the weakest character in the party: that is, the AI attacks the character with the minimum hit point in the party (or, if there are several such characters, the character with the minimum defense point among them). She wrote a function
selectTarget
($v$) that takes an array of characters representing a party and returns a character that her AI will attack.
However, the project manager Ms. Yagami was not satisfied with the behavior of her AI. Ms. Yagami said this AI was not interesting.
Aoba struggled a lot, and eventually she found that it is interesting if she substitutes one of the constant zeros in her program with a constant $C$. The rewritten program is as follows. Note that
Character
is a type representing a character and has fields $hp$ and $dp$ which represent the hit point and the defense point of the character respectively.
int C = <constant integer>;

Character selectTarget(Character v[]) {
  int n = length(v);
  int r = 0;
  for (int i = 1; i < n; i++) {
    if (abs(v[r].hp - v[i].hp) > C) {
      if (v[r].hp > v[i].hp) r = i;
    } else {
      if (v[r].dp > v[i].dp) r = i;
    }
  }
  return v[r];
}
By the way, this function may return different characters according to the order of the characters in $v$, even if $v$ contains the same set of characters. Ms. Yagami wants to know how many characters in a party may become the target of the new AI. Aoba's next task is to write a program that takes a given party $v$ and a constant $C$, and then counts the number of characters that may become the return value of
selectTarget
($v$) if $v$ is re-ordered.
Input
The input consists of a single test case. The first line contains two integers $N$ $(1 \leq N \leq 50,000)$ and $C$ $(0 \leq C \leq 10^9)$. The first integer $N$ represents the size of $v$. The second integer $C$ represents the constant $C$ in Aoba's program. The
i
-th line of the following $N$ lines contains two integers $hp_i$ and $dp_i$ $(0 \leq hp_i, dp_i \leq 10^9)$. $hp_i$ represents the hit point of the $i$-th character in $v$, and $dp_i$ represents the defense point of the $i$-th character in $v$. You can assume that $hp_i \ne hp_j$ or $dpi \ne dp_j$ if $i \ne j$ for any $1 \leq i, j \leq N$.
Output
Display the number of characters that may become the return value of
selectTarget
($v$), if $v$ is shuffled in an arbitrary order.
Sample Input 1
5 3
1 5
3 4
5 3
7 2
9 1
Output for the Sample Input 1
5
Sample Input 2
3 2
1 2
3 1
5 1
Output for the Sample Input 2
2
Sample Input 3
4 1
2 0
0 4
1 1
5 9
Outp
...(truncated)

Submitted solutions:

code_snippet: // AOJ #2098 Two-finger Programming
// 2025.3.23

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int NMAX = 50005;

int n, c;
int usd[NMAX] = {0};
int flag[NMAX] = {0};
int nw = 1, tp = 1;
int stk[NMAX];
int ct = 0;
pair<int,int> st[NMAX];

bool cmp(int a, int b) {
    return st[a].second == st[b].second ? st[a].first > st[b].first : st[a].second < st[b].second;
}

struct Seg {
    struct Nd { int l, r, mn; } e[NMAX * 4];

    void pu(int i) {
        int lmn = e[i << 1].mn, rmn = e[i << 1 | 1].mn;
        if (!lmn || !rmn) e[i].mn = lmn + rmn;
        else e[i].mn = cmp(lmn, rmn) ? lmn : rmn;
    }

    void bd(int i, int l, int r) {
        e[i].l = l; e[i].r = r;
        if (l == r) { e[i].mn = l; return; }
        int mid = (l + r) >> 1;
        bd(i << 1, l, mid);
        bd(i << 1 | 1, mid + 1, r);
        pu(i);
    }

    void mod(int i, int v) {
        if (e[i].l == e[i].r) { e[i].mn = 0; return; }
        int mid = (e[i].l + e[i].r) >> 1;
        if (v <= mid) mod(i << 1, v);
        else mod(i << 1 | 1, v);
        pu(i);
    }

    int qu(int i, int l, int r) {
        if (e[i].l == l && e[i].r == r) return e[i].mn;
        int mid = (e[i].l + e[i].r) >> 1;
        if (r <= mid) return qu(i << 1, l, r);
        if (l > mid)  return qu(i << 1 | 1, l, r);
        int vl = qu(i << 1, l, mid), vr = qu(i << 1 | 1, mid + 1, r);
        if (!vl || !vr) return vl + vr;
        return cmp(vl, vr) ? vl : vr;
    }
} seg;

int qry(int v) {
    int pos = lower_bound(st + 1, st + n + 1, make_pair(v + 1, 0)) - st - 1;
    return seg.qu(1, 1, pos);
}

void run() {
    while (usd[nw]) nw++;
    if (nw > n) { tp = 0; return; }

    ct = 0;
    int idx = qry(st[nw].first + c);
    if (st[idx].second == st[nw].second) {
        idx = nw;
        usd[idx] = 1;
        stk[++ct] = idx;
        seg.mod(1, idx);
    } else {
        while (st[idx].second == st[stk[1]].second || ct == 0) {
            usd[idx] = 1;
            stk[++ct] = idx;
            seg.mod(1, idx);
            idx = qry(st[nw].first + c);
        }
    }

    for (int i = 1; i <= ct; i++) {
        if (flag[stk[i]]) tp = 0;
        while (usd[nw]) nw++;
        while (nw <= n && st[nw].first < st[stk[i]].first - c) {
            stk[++ct] = nw;
            usd[nw] = 1;
            seg.mod(1, nw);
            while (usd[nw]) nw++;
        }
        while (true) {
            int tmp = qry(st[stk[i]].first + c);
            if (!tmp || st[tmp].second >= st[stk[i]].second) break;
            stk[++ct] = tmp;
            usd[tmp] = 1;
            seg.mod(1, tmp);
        }
    }
    int base = st[stk[1]].second;
    for (int i = 1; i <= ct; i++) {
        if (st[stk[i]].second != base) tp = 0;
    }
    if (tp) {
        int mn = 1e9;
        for (int i = 1; i <= ct; i++) mn = min(mn, st[stk[i]].first);
        int tmp = qry(mn + c);
        if (!tmp || st[tmp].second > base) tp = 0;
        else flag[tmp] = 1;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> c;
    for (int i = 1; i <= n; i++) cin >> st[i].first >> st[i].second;
    sort(st + 1, st + n + 1);
    seg.bd(1, 1, n);
    while (tp) run();
    int ans = 0;
    for (int i = 1; i <= n; i++) ans += usd[i];
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5816
final_rank: 1

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
#include <map>
using namespace std;
#define F first
#define S second
#define PB push_back
//#define int long long int
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(const pair<int,int> &a,const pair<int,int> &b){return a.F<=b.F?a:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if(l==r)v[n]=make_pair(a[l],l);
		else if(l<r){
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(l==r)v[n].F=INF;
		else if(l<r){
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
map<int,int> m;
void dfs(int x){
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(m.find(v[x].S)==m.end())m[v[x].S]=r[x];
	else m[v[x].S]=min(m[v[x].S],r[x]);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	else if(temp.S>m[v[x].S])ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.v.resize(N<<2);
	sg.init(0,0,n-1,a);
	//for(int i=0;i<n;i++)sg.ask(0,0,n-1,i,i);
	for(int i=0;i<n;i++)went[i]=false;
	v.PB({INF,INF});
	for(int i=0;i<n;i++){
	    lpos=lower_bound(v.begin(),v.end(),make_pair(v[i].F-c,0))-v.begin();
	    rpos=lower_bound(v.begin(),v.end(),make_pair(v[i].F+c+1,0))-v.begin();
		l[i]=lpos;
		r[i]=rpos-1;
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 1
time_ms: 50
memory_kb: 25388
final_rank: 2

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
//#define int long long int
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(const pair<int,int> &a,const pair<int,int> &b){return a.F<=b.F?a:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if(l==r)v[n]=make_pair(a[l],l);
		else if(l<r){
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(l==r)v[n].F=INF;
		else if(l<r){
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
signed main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.v.resize(N<<2);
	sg.init(0,0,n-1,a);
	//for(int i=0;i<n;i++)sg.ask(0,0,n-1,i,i);
	for(int i=0;i<n;i++)went[i]=false;
	v.PB({INF,INF});
	for(int i=0;i<n;i++){
	    lpos=lower_bound(v.begin(),v.end(),make_pair(v[i].F-c,0))-v.begin();
	    rpos=lower_bound(v.begin(),v.end(),make_pair(v[i].F+c+1,0))-v.begin();
		l[i]=lpos;
		r[i]=rpos-1;
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 40
memory_kb: 22968
final_rank: 6

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
#define int long long int
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(const pair<int,int> &a,const pair<int,int> &b){return a.F<=b.F?a:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if(l==r)v[n]=make_pair(a[l],l);
		else if(l<r){
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(l==r)v[n].F=INF;
		else if(l<r){
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
signed main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%lld%lld",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%lld%lld",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.v.resize(N<<2);
	sg.init(0,0,n-1,a);
	//for(int i=0;i<n;i++)sg.ask(0,0,n-1,i,i);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%lld\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 40
memory_kb: 24800
final_rank: 7

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
#define int long long int
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(const pair<int,int> &a,const pair<int,int> &b){return a.F<=b.F?a:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
signed main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%lld%lld",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%lld%lld",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.v.resize(N<<2);
	sg.init(0,0,n-1,a);
	//for(int i=0;i<n;i++)sg.ask(0,0,n-1,i,i);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%lld\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 40
memory_kb: 24808
final_rank: 8

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(const pair<int,int> &a,const pair<int,int> &b){return a.F<=b.F?a:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.v.resize(N<<2);
	sg.init(0,0,n-1,a);
	//for(int i=0;i<n;i++)sg.ask(0,0,n-1,i,i);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 40
memory_kb: 22120
final_rank: 5

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(const pair<int,int> &a,const pair<int,int> &b){return a.F<=b.F?a:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.v.resize(N<<2);
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)sg.ask(0,0,n-1,i,i);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 40
memory_kb: 20852
final_rank: 4

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(const pair<int,int> &a,const pair<int,int> &b){return a.F<=b.F?a:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.v.resize(N<<2);
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 40
memory_kb: 20836
final_rank: 3

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(const pair<int,int> &a,const pair<int,int> &b){return a.F<=b.F?a:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.v.resize(N<<2);
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 50
memory_kb: 9728
final_rank: 11

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(const pair<int,int> &a,const pair<int,int> &b){return a.F<=b.F?a:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if((int)v.size()<=n)v.resize(N<<2);
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 50
memory_kb: 9744
final_rank: 12

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(const pair<int,int> &a,const pair<int,int> &b){return a.F<=b.F?a:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if((int)v.size()<=n)v.resize(n+1);
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 50
memory_kb: 7684
final_rank: 9

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(const pair<int,int> &a,const pair<int,int> &b){
	    if(a.F<=b.F)return a;
	    else return b;
	}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if((int)v.size()<=n)v.resize(n+1);
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 50
memory_kb: 7684
final_rank: 9

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(pair<int,int> a,pair<int,int> b){return a.F<=b.F?a:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if((int)v.size()<=n)v.resize(n+1);
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.4358974358974359
time_ms: 50
memory_kb: 63768
final_rank: 15

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(pair<int,int> a,pair<int,int> b){
	    if(a.F<b.F)return a;
	    else if(b.F<a.F)return b;
	    else return a.S<b.S?a:b;
	}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if((int)v.size()<=n)v.resize(n+1);
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 50
memory_kb: 23224
final_rank: 13

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(pair<int,int> a,pair<int,int> b){
	    if(a.F<b.F)return a;
	    else if(b.F<a.F)return b;
	    else return a.S<b.S?a:b;
	}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if((int)v.size()<=n)v.resize(n+1);
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.23076923076923078
time_ms: 40
memory_kb: 57104
final_rank: 17

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(pair<int,int> a,pair<int,int> b){return a.F<b.F?a:a.F==b.F?a.S<b.S?a:b:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if((int)v.size()<=n)v.resize(n+1);
		if(l==r)v[n]=make_pair(a[l],l);
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.23076923076923078
time_ms: 30
memory_kb: 60228
final_rank: 16

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(pair<int,int> a,pair<int,int> b){return a.F<b.F?a:a.F==b.F?a.S<b.S?a:b:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if((int)v.size()<=n)v.resize(n+1);
		if(l==r)v[n]={a[l],l};
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.23076923076923078
time_ms: 40
memory_kb: 60248
final_rank: 20

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(pair<int,int> a,pair<int,int> b){return a.F<b.F?a:a.F==b.F?a.S<b.S?a:b:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if((int)v.size()<=n)v.resize(n+1);
		if(l==r)v[n]={a[l],l};
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	//if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.23076923076923078
time_ms: 40
memory_kb: 59688
final_rank: 18

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(pair<int,int> a,pair<int,int> b){return a.F<b.F?a:a.F==b.F?a.S<b.S?a:b:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if((int)v.size()<=n)v.resize(n+1);
		if(l==r)v[n]={a[l],l};
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    //if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	//if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		//if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.23076923076923078
time_ms: 40
memory_kb: 59760
final_rank: 19

code_snippet: bool debug=false;
#include <queue>
#include <vector>
#include <stdio.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
#define PB push_back
const int N=(int)1e5+10;
const int INF=(int)1e9+10;
struct seg_tree{
	vector<pair<int,int>> v;
	pair<int,int> better(pair<int,int> a,pair<int,int> b){return a.F<b.F?a:a.F==b.F?a.S<b.S?a:b:b;}
	void pull(int n){v[n]=better(v[n*2+1],v[n*2+2]);}
	void init(int n,int l,int r,int a[]){
		if((int)v.size()<=n)v.resize(n+1);
		if(l==r)v[n]={a[l],l};
		else{
			int mid=(l+r)>>1;
			init(n*2+1,l,mid,a);
			init(n*2+2,mid+1,r,a);
			pull(n);
		}
	}
	void fix(int n,int l,int r,int pos){
		if(debug)printf("fix(%d,%d,%d,%d)\n",n,l,r,pos);
		if(l==r)v[n].F=INF;
		else {
			int mid=(l+r)>>1;
			if(pos>mid)fix(n*2+2,mid+1,r,pos);
			else fix(n*2+1,l,mid,pos);
			pull(n);
		}
		return ;
	}
	pair<int,int> ask(int n,int l,int r,int L,int R){
	    if(L>R)while(true);
		if(L<=l&&r<=R)return v[n];
		else if(L>r||l>R)return {INF,-1};
		else {
			int mid=(l+r)>>1;
			return better(ask(n*2+1,l,mid,L,R),ask(n*2+2,mid+1,r,L,R));
		}
	}
};
vector<pair<int,int>> v;
seg_tree sg;
int l[N],r[N],n,ans=0;
bool ok,went[N];
void dfs(int x){
	//if(x<0||x>=n)while(true);
	if(debug)printf("dfs(%d) v[%d]=(%d,%d)\n",x,x,v[x].F,v[x].S);
	ans++;
	went[x]=true;
	sg.fix(0,0,n-1,x);
	if(debug)printf("fixed\n");
	pair<int,int> temp;
	if(debug)printf("going down\n");
	if(l[x]>0)while(true){
		temp=sg.ask(0,0,n-1,0,l[x]-1);
		if(temp.F<INF)dfs(temp.S);
		else break;
	}
	if(debug)printf("going mid\n");
	while(true){
		temp=sg.ask(0,0,n-1,l[x],r[x]);
		if(debug)printf("temp=(%d,%d)\n",temp.F,temp.S);
		if(temp.F<v[x].S)dfs(temp.S);
		else break;
	}
	temp=sg.ask(0,0,n-1,l[x],r[x]);
	if(temp.F>v[x].S)ok=false;
	if(debug)printf("return\n");
	return ;
}
int main(){
	int c,a[N],can=0,lpos=0,rpos=0,npos=0;
	pair<int,int> temp;
	ok=true;
	scanf("%d%d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d%d",&temp.F,&temp.S);
		v.PB(temp);
	}
	sort(v.begin(),v.end());
	for(int i=0;i<n;i++)a[i]=v[i].S;
	sg.init(0,0,n-1,a);
	for(int i=0;i<n;i++)went[i]=false;
	for(int i=0;i<n;i++){
		while(v[lpos].F+c<v[i].F)lpos++;
		while(rpos<n){
			if(v[rpos].F-c<=v[i].F)rpos++;
			else break;	
		}
		l[i]=lpos;
		r[i]=rpos-1;
		//if(l[i]>r[i])while(true);
	}
	if(debug)printf("before dfs\n");
	while(ok){
		while(npos<n){
			if(went[npos])npos++;
			else break;
		}
		if(npos<n)can=sg.ask(0,0,n-1,l[npos],r[npos]).S;
		else break;
		dfs(can);
	}
	printf("%d\n",ans);
}
accuracy: 0.8846153846153846
time_ms: 50
memory_kb: 36000
final_rank: 14