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>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 1e9+7;
const int nax = 2e5+5;
const int MAX_VAL = 1e6+1;
double PI=3.14159265359;
int arx[8]={1,0,0,-1,-1,-1, 1, 1};
int ary[8]={0,1,-1, 0, 1,-1,-1, 1};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
vector<ii> tab;
ii segtree[nax*4];
ii lazy[nax*4];
void build(int pos,int l,int r)
{
if (l==r){
segtree[pos]={0,l};
return;
}
int mid=(r+l)/2;
build(pos*2+1,l,mid);
build(pos*2+2,mid+1,r);
if (segtree[pos*2+2].fi<=segtree[pos*2+1].fi) segtree[pos]=segtree[pos*2+2];
else segtree[pos]=segtree[pos*2+1];
return;
}
void expand(int pos)
{
if (lazy[pos].fi==0) return;
segtree[pos*2+1].fi=lazy[pos].fi;
segtree[pos*2+2].fi=lazy[pos].fi;
lazy[pos*2+1].fi=lazy[pos].fi;
lazy[pos*2+2].fi=lazy[pos].fi;
lazy[pos].fi=lazy[pos].se=0;
}
void update(int pos,int l,int r,int left,int right)
{
if (l>r||r<left||l>right) return;
if (l>=left&&r<=right){
segtree[pos].fi++;
lazy[pos].fi=segtree[pos].fi;
lazy[pos].se=segtree[pos].se;
return;
}
expand(pos);
int mid=(r+l)/2;
update(pos*2+1,l,mid,left,right);
update(pos*2+2,mid+1,r,left,right);
//cout <<segtree[pos*2+2].fi<<" "<<segtree[pos*2+1].fi<<endl;
if (segtree[pos*2+2].fi<=segtree[pos*2+1].fi) segtree[pos]=segtree[pos*2+2];
else segtree[pos]=segtree[pos*2+1];
return;
}
ii query(int pos,int l,int r,int left,int right)
{
if(l>r||l>right||r<left) return {1e9,1e9};
if(l>=left&&r<=right) return segtree[pos];
expand(pos);
int mid=(r+l)/2;
ii a=query(pos*2+1,l,mid,left,right);
ii b=query(pos*2+2,mid+1,r,left,right);
//cout <<l<<" "<<r<<endl;
if (b.fi<=a.fi) return b;
else return a;
}
int lower(int pos,int l,int r,int index)
{
if (l==r){
return segtree[pos].fi;
}
int mid=(r+l)/2;
expand(pos);
if (index<=mid) return lower(pos*2+1,l,mid,index);
else return lower(pos*2+2,mid+1,r,index);
}
int main(){
optimise;
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
//setIO("boards");
int n;
cin>>n;
tab.resize(n);
int h=0;
for (int i = 0; i < n; ++i)
{
cin>>tab[i].fi>>tab[i].se;
h=max(h,tab[i].fi);
}
build(0,0,h-1);
sort(tab.begin(),tab.end(),greater<>());
for (int i = 0; i < n; ++i)
{
ii cur=tab[i];
//cout <<cur.fi<<" "<<cur.se<<endl;
ii x=query(0,0,h-1,0,cur.fi-1);
//cout <<"hey"<<x.fi<<" "<<x.se<<endl;
int ne=min(x.se,cur.fi-1);
if (ne>=cur.se-1){
//cout <<"1"<<" "<<ne-cur.se+1<<" "<<ne<<endl;
update(0,0,h-1,ne-cur.se+1,ne);
}else{
//cout <<"2"<<" "<<0<<" "<<ne<<endl;
//cout <<"2"<<" "<<cur.fi-cur.se+x.se+1<<" "<<cur.fi-1<<endl;
update(0,0,h-1,0,ne);
update(0,0,h-1,cur.fi-cur.se+x.se+1,cur.fi-1);
}
}
ll ans=0;
for (int i = 0; i <= h-1; ++i)
{
int x=lower(0,0,h-1,i);
//cout <<x<<" ";
ans+=1ll*(x*(x-1))/2;
}
cout <<ans<<endl;
}
Compilation message (stderr)
sails.cpp: In function 'void setIO(std::string)':
sails.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |