#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORU(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define IOS cin.tie(0)->sync_with_stdio(false);
#define PROB "IOI07_sails"
void Fi(){
if(fopen(PROB".inp", "r")){
freopen(PROB".inp", "r", stdin);
freopen(PROB".out", "w", stdout);
}
}
const int N = 1e5 + 1;
int n;
using pi = pair<int, int>;
vector<pi> a;
ll bit[2][N];
void update(const int &t, int i, const ll &v){
while(i < N){
bit[t][i] += v;
i += i & -i;
}
}
ll getSum(const int &t, int i){
ll ans = 0;
while(1 <= i){
ans += bit[t][i];
i -= i & -i;
}
return ans;
}
void updRange(const int &i, const ll &v){
update(0, 1, v);
update(0, i + 1, -v);
update(1, i + 1, v * i);
}
void updRange(const int &l, const int &r, const ll &v){
updRange(l - 1, -v);
updRange(r, v);
}
ll query(const int &i){
return getSum(0, i) * i + getSum(1, i);
}
ll query(const int &l, const int &r){
return query(r) - query(l - 1);
}
int main(){
IOS;
Fi();
cin >> n; a.resize(n);
for(pi &x: a) cin >> x.fi >> x.se;
sort(ALL(a));
for(auto [H, K]: a){
int P = H - K + 1;
int l, r, A = P, B = P;
for(l = 2, r = P; l <= r; ){
int mid = (l + r) / 2;
if(query(mid, P) == 0){
A = mid - 1;
r = mid - 1;
} else l = mid + 1;
}
for(l = P + 1, r = H; l <= r; ){
int mid = (l + r) / 2;
if(query(P + 1, mid) == 0){
B = mid;
l = mid + 1;
} else r = mid - 1;
}
if(A == P){
updRange(A, H, 1);
} else{
updRange(B + 1, H, 1);
updRange(A, B - P + A, 1);
}
}
ll ans = 0;
FORU(i, 1, a.back().fi){
ll tmp = query(i);
ans += tmp * (tmp - 1) / 2;
}
cout << ans;
return 0;
}
Compilation message
sails.cpp: In function 'void Fi()':
sails.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen(PROB".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen(PROB".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
1304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
2056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
126 ms |
3316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
98 ms |
3516 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
142 ms |
3668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |