#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair
const int MX = 100005;
template<int SZ> struct BIT {
int bit[SZ];
void upd(int i, int x) {
// cerr << "CHANGING: " << i << " " << x << '\n';
for (; i < SZ; i += (i & -i)) {
// cerr << "UPD: " << i << '\n';
bit[i] += x;
}
}
ll qry(int i) {
// cerr << "QUERY: " << i << '\n';
ll ret = 0;
for (; i > 0; i -= (i & -i)) {
// cerr << "GOTO: " << i << '\n';
ret += bit[i];
}
// cerr << ret << '\n';
return ret;
}
};
BIT<MX + 5> orz;
void upd(int l, int r) {
l++, r++;
orz.upd(l, 1);
orz.upd(r + 1, -1);
}
int get(int x) {
return orz.qry(x + 1);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
vector<pi> sails(n);
for (int i = 0; i < n; i++) {
cin >> sails[i].f >> sails[i].s;
}
sort(all(sails));
// cerr << '\n';
auto getl = [&](int val) {
int lo = 0;
int hi = MX - 1;
int ans = 0;
while(lo <= hi) {
int mid = (lo + hi) / 2;
int curr = get(mid);
if (curr < val) {
lo = mid + 1;
}
else {
ans = mid;
hi = mid - 1;
}
}
return ans;
};
auto getr = [&](int val) {
int lo = 0;
int hi = MX - 1;
int ans = 0;
while(lo <= hi) {
int mid = (lo + hi) / 2;
int curr = get(mid);
if (curr <= val) {
lo = mid + 1;
ans = mid;
}
else {
hi = mid - 1;
}
}
return ans;
};
for (int i = 0; i < n; i++) {
int cl = MX - sails[i].f, cr = cl + sails[i].s - 1;
cerr << "range: " << cl << " " << cr << '\n';
int val = get(cr);
cerr << "val: " << val << '\n';
int cf = max(cl, getl(val)), cs = getr(val);
cerr << "l, r: " << cf << " " << cs << '\n';
int rem = sails[i].s;
if (cf > cl) {
cerr << "updating: " << cl << " " << cf - 1 << '\n';
upd(cl, cf - 1);
rem -= (cf - cl);
}
cerr << rem << '\n';
cerr << "updating: " << cs - rem + 1 << " " << cs << '\n';
upd(cs - rem + 1, cs);
}
ll ans = 0;
for (int i = 0; i < MX; i++) {
ll cv = get(i);
if (cv > 0) {
cerr << "i, val: " << i << " " << get(i) << '\n';
}
ans += ((cv - 1) * cv) / 2;
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
384 KB |
Output is correct |
2 |
Correct |
43 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
632 KB |
Output is correct |
2 |
Execution timed out |
1092 ms |
2796 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
721 ms |
1912 KB |
Output is correct |
2 |
Execution timed out |
1087 ms |
2808 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1090 ms |
2552 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1080 ms |
2712 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
3196 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1085 ms |
3276 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1087 ms |
3192 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |