#include <bits/stdc++.h>
#define NMAX 100001
#define int long long
#define pb push_back
#define eb emplace_back
#define MOD 1000000007
#define nl '\n'
#define INF 1000000007
#define LLONG_MAX 9223372036854775807
#define pii pair<int,int>
#define tpl tuple<int,int,int,int>
//#pragma GCC optimize("O3")
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
/*
*
*
----------------DEMONSTRATION-------------------
---------------------END------------------------
*/
/*-------------Initialize------------*/
class segtree{
private:
int n;
vector<int>tree;
public:
void init(int sz)
{
n=sz;
tree.resize(4*sz+1);
}
void update(int node,int st,int dr,int pos,int val)
{
if(st==dr)
{
tree[node]+=val;
return;
}
int mid=(st+dr)/2;
if(pos<=mid)
update(2*node,st,mid,pos,val);
else
update(2*node+1,mid+1,dr,pos,val);
tree[node]=tree[2*node]+tree[2*node+1];
}
int query(int node,int st,int dr,int L,int R)
{
if(R<st || dr<L)
return 0;
if(L<=st && dr<=R)
{
return tree[node];
}
int mid=(st+dr)/2;
int left=query(2*node,st,mid,L,R);
int right=query(2*node+1,mid+1,dr,L,R);
return left+right;
}
void update(int pos,int val)
{
update(1,0,n,pos,val);
}
int query(int L,int R)
{
return query(1,0,n,L,R);
}
};
int b,n,d,m;
int res;
const int M=150001;
const int Md=170;
struct point{
int d1,d2;
};
struct Point{
int d1,d2,d3,d4;
};
/*---------subtask1-----------*/
void subtask1()
{
vector<int>v(n+1);
for(int i=1;i<=n;++i)
{
cin>>v[i];
}
sort(v.begin()+1,v.end());
for(int i=1;i<=n;++i)
{
int st=i+1;
int dr=n;
int ans=-1;
while(st<=dr)
{
int mid=(st+dr)/2;
if(abs(v[i]-v[mid])<=d)
{
ans=mid;
st=mid+1;
}
else
dr=mid-1;
}
if(ans==-1)
continue;
res+=(ans-i);
}
cout<<res;
}
/*---------subtask2-----------*/
segtree seg;
void subtask2()
{
vector<point>v(n+1);
for(int i=1;i<=n;++i)
{
int x,y;
cin>>x>>y;
v[i]={x+y,(x-y)+(75000)};
}
auto cmp=[&](point a,point b)
{
return a.d1<b.d1;
};
sort(v.begin()+1,v.end(),cmp);
int ans=0;
seg.init(M);
int last=1;
for(int i=1;i<=n;++i)
{
while(abs(v[i].d1-v[last].d1)>d)
{
seg.update(v[last].d2,-1);
last++;
}
ans+=seg.query(v[i].d2-d,v[i].d2+d);
seg.update(v[i].d2,1);
}
cout<<ans;
}
/*---------subtask3-----------*/
void subtask3()
{
vector<Point>v(n+1);
for(int i=1;i<=n;++i)
{
int x,y,z;
cin>>x>>y>>z;
int d1=(x+y+z)+75;
int d2=(x+y-z)+75;
int d3=(x-y+z)+75;
int d4=(x-y-z)+75;
v[i]={d1,d2,d3,d4};
}
auto cmp=[&](Point a,Point b)
{
return a.d1<b.d1;
};
sort(v.begin()+1,v.end(),cmp);
segtree st2,st3,st4;
st2.init(Md);
st3.init(Md);
st4.init(Md);
int j=1;
int ans=0;
for(int i=1;i<=n;++i)
{
while(abs(v[i].d1-v[j].d1)>d)
{
st2.update(v[j].d2,-1);
st3.update(v[j].d3,-1);
st4.update(v[j].d4,-1);
j++;
}
int count2=0,count3=0,count4=0;
count2=st2.query(v[i].d2-d,v[i].d2+d);
count3=st3.query(v[i].d3-d,v[i].d3+d);
count4=st4.query(v[i].d4-d,v[i].d4+d);
// cout<<count2<<" "<<count3<<" "<<count4<<nl;
ans+=min({count2,count3,count4});
st2.update(v[i].d2,1);
st3.update(v[i].d3,1);
st4.update(v[i].d4,1);
}
cout<<ans;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>b>>n>>d>>m;
if(b==1)
{
subtask1();
}
else if(b==2)
{
subtask2();
}
else
{
subtask3();
}
return 0;
}
Compilation message
pairs.cpp:9: warning: "LLONG_MAX" redefined
9 | #define LLONG_MAX 9223372036854775807
|
In file included from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:195,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/syslimits.h:7,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:34,
from /usr/include/c++/10/climits:42,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:39,
from pairs.cpp:1:
/usr/include/limits.h:135: note: this is the location of the previous definition
135 | # define LLONG_MAX __LONG_LONG_MAX__
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
1628 KB |
Output is correct |
2 |
Correct |
9 ms |
1500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1884 KB |
Output is correct |
2 |
Correct |
14 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1884 KB |
Output is correct |
2 |
Correct |
16 ms |
1884 KB |
Output is correct |
3 |
Correct |
15 ms |
1880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7276 KB |
Output is correct |
2 |
Correct |
22 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
7260 KB |
Output is correct |
2 |
Correct |
39 ms |
7256 KB |
Output is correct |
3 |
Correct |
30 ms |
7260 KB |
Output is correct |
4 |
Correct |
37 ms |
7400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
7860 KB |
Output is correct |
2 |
Correct |
47 ms |
7856 KB |
Output is correct |
3 |
Correct |
34 ms |
7772 KB |
Output is correct |
4 |
Correct |
34 ms |
7852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
4236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |