#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename num_t>
using ordered_set = tree<num_t, null_type, less<num_t>, rb_tree_tag, tree_order_statistics_node_update>;
#ifdef pikachu
#include "welcome_to_python_is_slower_than_c++_club.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 69
#endif
template<typename T1, typename T2>istream& operator>>(istream& in, pair<T1, T2>& a) { return in >> a.first >> a.second; }
template<typename T1, typename T2>ostream& operator<<(ostream& out, pair<T1, T2> a) { return out << a.first << " " << a.second;}
template<typename T> void print(T t) { cout << t <<' '; }
template<typename T, typename... Args> void print(T t, Args... args) { print(t);print(args...); }
string operator*=(string& s, int cnt) { string t = s;for (size_t i = 1; i < cnt; i++)s += t;return s; }
string operator*(string s, int cnt) { return s *= cnt; }
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define len(x) (int)((x).size())
#define bk back()
#define elif else if
#define add insert
#define append push_back
#define pop pop_back
#define str string
#define in :
#define fr first
#define sc second
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int,int>
#define mii map<pii,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>b;i--)
#define el '\n'
#define printl(arg) cout << arg << endl
// #define print(arg) cout << arg
#define inputa(arg) for (auto& e : arg) cin >> e
#define printa(arg) for (auto& e : arg) print(e);
#define printr(arg) { printl(arg);return; }
#define printd(arg) printf("%0.3lf\n", arg)
const int mod=1e9+7;
const int INF=1e18;
const int MAX_N= 1e5+10;
int n,m,k,x,y,z,t,q,counter;
int s_tree[MAX_N*4][2],lazy[MAX_N*4];
int lazy_def=0;
vi a;
int ans;
bool bol;
void solve_lazy(int i_tree,int left ,int right,int vl){
s_tree[i_tree][0]+=vl;
s_tree[i_tree][1]+=vl*(right-left+1);
if(left!=right){
lazy[i_tree*2]+=vl;
lazy[i_tree*2+1]+=vl;
}
lazy[i_tree]=lazy_def;
}
void update(int i_tree,int left,int right,int l,int r,int vl){
if(lazy[i_tree]!=lazy_def){
solve_lazy(i_tree,left,right,lazy[i_tree]);
}
if(right<l or left>r) return;
if(l<=left and right<=r){
solve_lazy(i_tree,left,right,vl);
return;
}
int mid=(left+right)/2;
update(i_tree*2,left,mid,l,r,vl);
update(i_tree*2+1,mid+1,right,l,r,vl);
s_tree[i_tree][0]=max(s_tree[i_tree*2][0],s_tree[i_tree*2+1][0]);
s_tree[i_tree][1]=s_tree[i_tree*2][1]+s_tree[i_tree*2+1][1];
}
void query(int i_tree,int left,int right,int vl){
if(lazy[i_tree]!=lazy_def){
solve_lazy(i_tree,left,right,lazy[i_tree]);
}
if (s_tree[i_tree][0]<vl) return;
if(left==right){
ans=left;
if(s_tree[i_tree][0]==vl) bol=true;
return;
}
int mid=(left+right)/2;
if(lazy[i_tree*2 +1]!=lazy_def){
solve_lazy(i_tree*2 + 1,mid+1,right,lazy[i_tree*2 + 1]);
}
if(s_tree[i_tree*2+1][0]>vl){
query(i_tree*2+1,mid+1,right,vl);
return;
}
if(lazy[i_tree*2]!=lazy_def){
solve_lazy(i_tree*2,left,mid,lazy[i_tree*2]);
}
query(i_tree*2,left,mid,vl);
}
void get(int i_tree,int left,int right,int i){
if(i<left or i>right) return;
if(lazy[i_tree]!=lazy_def){
solve_lazy(i_tree,left,right,lazy[i_tree]);
}
if(left==right){
ans=s_tree[i_tree][0];
return;
}
int mid=(left+right)/2;
get(i_tree*2,left,mid,i);
get(i_tree*2+1,mid+1,right,i);
}
void get_sum(int i_tree,int left,int right,int l,int r){
if(lazy[i_tree]!=lazy_def){
solve_lazy(i_tree,left,right,lazy[i_tree]);
}
if(right<l or left>r) return;
if(l<=left and right<=r){
ans+=s_tree[i_tree][1];
return;
}
int mid=(left+right)/2;
get_sum(i_tree*2,left,mid,l,r);
get_sum(i_tree*2+1,mid+1,right,l,r);
}
void code(){
cin>>z;
n=1e5+10;
vector<vector<int>>a;
rep(i,0,z){
cin>>x>>y;
a.append({x,y});
}
sort(all(a));
int tot=0;
for(auto& i in a){
if(i[0]==i[1]){
ans=0;
get_sum(1,0,n-1,0,i[0]-1);
tot+=ans;
update(1,0,n-1,0,i[0]-1,1);
continue;
}else{
int vl,f,l;
get(1,0,n-1,i[0]-i[1]);
vl=ans;
bol=false;
query(1,0,n-1,vl);
f=ans;
if(not bol) f++;
if(not vl) l=i[0]-1;
else{
query(1,0,n-1,vl-1);
l=min(i[0]-1,ans);
}
if(l<i[0]-1){
ans=0;
get_sum(1,0,n-1,l+1,i[0]-1);
tot+=ans;
update(1,0,n-1,l+1,i[0]-1,1);
i[1]-=i[0]-l-1;
}
ans=0;
get_sum(1,0,n-1,f,f+i[1]-1);
tot+=ans;
update(1,0,n-1,f,f+i[1]-1,1);
}
}
cout<<tot<<el;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("cardgame.in", "r", stdin);
// freopen("cardgame.out", "w", stdout);
int t = 1;
// cin>>t;
while(t--) code();
return 0;
}
Compilation message
sails.cpp: In function 'std::string operator*=(std::string&, int)':
sails.cpp:24:75: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
24 | string operator*=(string& s, int cnt) { string t = s;for (size_t i = 1; i < cnt; i++)s += t;return s; }
| ~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
356 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
752 KB |
Output is correct |
2 |
Correct |
6 ms |
6412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
2544 KB |
Output is correct |
2 |
Correct |
66 ms |
2240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
3876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
6204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
10616 KB |
Output is correct |
2 |
Correct |
191 ms |
10476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
11392 KB |
Output is correct |
2 |
Correct |
105 ms |
10248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
299 ms |
11956 KB |
Output is correct |
2 |
Correct |
210 ms |
7644 KB |
Output is correct |