//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, b, a) for (int i = b; i >= a; --i)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define lsb(x) (x)&(-x)
void setIO(string name = "") {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
ll fexp(ll a, ll b, ll m) {
if (b == 0) return 1LL;
ll p = a;
ll ans = 1;
while (b > 0) {
if (b % 2 != 0) ans = (ans*p)%m;
p = (p*p)%m;
b >>= 1;
}
return ans;
}
const int MAXN = 1e5 + 10;
const int INF = 1e18+5;
const int MOD = 1e9+7;
int a[MAXN],n;
struct Node {
int v;
};
struct Seg3 {
Node seg3[4*MAXN];
int lazy[4*MAXN];
void flush(int i, int l, int r) {
seg3[i].v += lazy[i];
if (l != r) {
lazy[2*i] += lazy[i];
lazy[2*i+1] += lazy[i];
}
lazy[i] = 0;
}
Node join(Node a, Node b) {
Node r;
r.v = a.v + b.v;
return r;
}
void build(int i, int l, int r) {
if (l == r) {
Node tmp;
tmp.v = a[l];
seg3[i] = tmp;
return;
}
int mid =(l+r)>>1;
build(2*i,l,mid);
build(2*i+1,mid+1,r);
seg3[i] = join(seg3[2*i],seg3[2*i+1]);
}
Seg3(int l, int r) {
build(1,l,r);
}
void update(int i, int l, int r, int tl, int tr, int x) {
flush(i,l,r);
if (tr < l || tl > r) return;
if (l>=tl && r <= tr) {
lazy[i] += x;
return;
}
int mid =(l+r)>>1;
update(2*i,l,mid,tl,tr,x);
update(2*i+1,mid+1,r,tl,tr,x);
seg3[i] = join(seg3[2*i],seg3[2*i+1]);
}
int query(int i, int l, int r, int tl, int tr) {
flush(i,l,r);
if (l==r) return seg3[i].v;
int mid =(l+r)>>1;
if (mid >= tl) return query(2*i,l,mid,tl,tr);
else return query(2*i+1,mid+1,r,tl,tr);
}
int upperbound(int x) {
int lo = 1, hi = n, ans=0;
while (lo <= hi) {
int mid = (lo+hi)/2;
if (query(1,1,n,mid,mid) <= x) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
};
void solve() {
int m,cnt = 0;cin >> m;
rep(i,1,m) {
int h,x;cin >> h >> x;
a[1]++;
a[h+1]--;
cnt+=x;
n = max(h,n);
}
rep(i,1,n) a[i] +=a[i-1];
reverse(a+1,a+n+1);
int ans = 0, i = 0;
Seg3 seg(1,n);
while (cnt > 0) {
int k = seg.upperbound(0)+1;
if (n-k+1 > cnt) {
ans += cnt*i;
break;
}
ans += (n-k+1)*i;
cnt-=n-k+1;
seg.update(1,1,n,k,n,-1);
i++;
}
cout << ans << '\n';
}
int32_t main() {
setIO();
int tt = 1;
//cin >> tt;
while (tt-- > 0) solve();
}
Compilation message (stderr)
sails.cpp: In function 'void setIO(std::string)':
sails.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen((name + ".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... |