RT 手打 hash map WA#3
#include<iostream>
using namespace std;
int n, m;
template<typename Ta, typename Tb>
class map
{
public:
struct value;
private:
int _size;
struct node
{
Ta pos;
value val;
node* next;
};
struct head
{
node* lead = nullptr;
};
head* h;
const unsigned _hash(Ta ta) { return (unsigned)ta; }
public:
struct value
{
Tb val;
value* from;
void root()
{
if (from->from == from)
return;
from->root();
val = from->val;
from = from->from;
}
operator Tb() const
{
return val;
}
void operator = (Tb tb)
{
root();
from->val = tb;
}
value()
{
from = this;
}
value(Tb tb) :val(tb)
{
val = tb;
from = this;
}
};
map()
{
h = new head[_size];
}
map(unsigned size = 0x10000)
{
_size = size;
h = new head[_size];
}
value at(Ta pos)
{
int p = _hash(pos) % _size;
for (node* i = h[p].lead; i != nullptr; i = i->next)
{
if (i->pos == pos)
return i->val;
}
node* nw; nw = new node;
nw->next = h[p].lead;
h[p].lead = nw;
nw->pos = pos;
nw->val = NULL;
return nw->val;
}
value operator [] (Ta pos)
{
return at(pos);
}
value operator + (Ta pos)
{
return at(pos);
}
};
map<int, int> mp(1000000);
int main()
{
int n, c, a, ans=0;
cin >> n >> c;
for (int i = 1; i <= n; i++)
{
cin >> a;
ans += mp[a - c];
ans += mp[a + c];
mp[a]=mp[a]+1;
}
cout << ans;
}