This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define NMAX 250001
#define int long long
#define MAX 100000
#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");
vector<int>delta;
class segtree{
private:
int n;
vector<int>before;
vector<int>after;
public:
void init(int sz)
{
n=sz+1;
before.resize(4*sz+1,-1);
after.resize(4*sz+1,-1);
update(0,n);
}
void update(int node,int st,int dr,int pos,int val)
{
if(st==dr)
{
delta[st]+=val;
if(delta[st])
before[node]=after[node]=pos;
else
before[node]=after[node]=-1;
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);
before[node]=(before[2*node+1]!=-1? before[2*node+1]:before[2*node]);
after[node]=(after[2*node]!=-1? after[2*node]:after[2*node+1]);
}
int first_after(int node,int st,int dr,int L,int R)
{
if(st>=R || dr<=L)
return -1;
if(L<=st && dr<=R)
{
return after[node];
}
int mid=(st+dr)/2;
int left=first_after(2*node,st,mid,L,R);
int right=first_after(2*node+1,mid+1,dr,L,R);
int ans=(left!=-1? left:right);
return ans;
}
int last_before(int node,int st,int dr,int L,int R)
{
if(st>=R || dr<=L)
return -1;
if(L<=st && dr<=R)
{
return before[node];
}
int mid=(st+dr)/2;
int left=last_before(2*node,st,mid,L,R);
int right=last_before(2*node+1,mid+1,dr,L,R);
int ans=(right!=-1? right:left);
return ans;
}
void update(int pos,int val)
{
update(1,0,n,pos,val);
}
int first(int L,int R)
{
return first_after(1,0,n,L,R);
}
int last(int L,int R)
{
return last_before(1,0,n,L,R);
}
};
int n,max_h;
vector<pii>v;
segtree st;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
v.resize(n);
for(int i=0;i<n;++i)
{
cin>>v[i].first>>v[i].second;
max_h=max(max_h,v[i].first);
}
delta.resize(max_h+1);
st.init(max_h);
auto cmp=[&](pii a,pii b)
{
// if(a.first==b.first)
// return a.second<b.second;
return a.first<b.first;
};
sort(v.begin(),v.end(),cmp);
for(int i=0;i<n;++i)
{
int h0=v[i].first-v[i].second;
int up=st.first(h0,max_h+1);
int down=st.last(0,h0+1);
if(up==-1)
up=v[i].first;
/*------------------DEBUG--------------------
cout<<v[i].first<<" "<<v[i].second<<": "<<nl;
cout<<"h0 0-h0+1 h0-maxh+1"<<nl;
cout<<h0<<" --- "<<down<<" --- "<<up<<nl<<nl;
------------------------------------------*/
st.update(v[i].first,1);
st.update(up,-1);
st.update(down+up-h0,1);
st.update(down,-1);
// for(int i=1;i<=max_h;++i)
// {
// cout<<delta[i]<<" ";
// }
// cout<<nl;
}
int ans=0,aux=0;
for(int i=max_h;i>0;--i)
{
// cout<<delta[i]<<" ";
aux+=delta[i];
ans+=(aux*(aux-1))/2;
}
cout<<ans;
return 0;
}
Compilation message (stderr)
sails.cpp:10: warning: "LLONG_MAX" redefined
10 | #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 sails.cpp:1:
/usr/include/limits.h:135: note: this is the location of the previous definition
135 | # define LLONG_MAX __LONG_LONG_MAX__
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |