#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define fi first
#define se second
void setIO(string name) {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
const int N = 1e5+5;
const int LOG = 17;
int n;
ll ans, bit[2][N];
ii mast[N];
void modify(int t, int i, ll v) {
for(; i<=n; i+=i&-i) bit[t][i] += v;
}
ll get(int t, int i) {
ll ans = 0;
for(; i>=1; i-=i&-i) ans += bit[t][i];
return ans;
}
void inc(int l, int r, ll v) {
modify(0, l, v);
modify(0, r+1, -v);
modify(1, l, v * (l-1));
modify(1, r+1, - v * r);
}
ll sum_range(ll i) {
return get(0, i) * i - get(1, i);
}
ll sum_range(int l, int r) {
return sum_range(r) - sum_range(l-1);
}
int lst(int val) {
int cur = 0, till = 0;
for(int i=LOG-1; i>=0; --i)
if (cur+(1<<i) <= n && till+bit[0][cur+(1<<i)] <= val)
cur += 1 << i, till += bit[0][cur];
return cur;
}
int main() {
setIO("");
cin >> n;
foru(i, 1, n) {
cin >> mast[i].fi >> mast[i].se;
}
sort(mast+1, mast+n+1);
foru(i, 1, n) {
int hei = mast[i].fi, cnt = mast[i].se;
int lb = 100000-hei+1, alpha = get(0, min(n, lb+cnt-1));
int rb = lst(alpha), rx = max(lb-1, lst(alpha-1)), lx = max(rx+1, rb-(cnt-(rx-lb+1))+1);
ans += sum_range(lb, rx);
ans += sum_range(lx, rb);
inc(lb, rx, 1);
inc(lx, rb, 1);
}
cout << ans;
}
Compilation message
sails.cpp: In function 'void setIO(std::string)':
sails.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
2076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
2388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
2628 KB |
Output is correct |
2 |
Correct |
38 ms |
1524 KB |
Output is correct |