#include <stdio.h>
#define N 100000
#define H_ 17 /* H_ = ceil(log2(100000)) */
#define N_ (1 << H_)
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int hh[N], kk[N];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (hh[ii[j]] == hh[i_])
j++;
else if (hh[ii[j]] < hh[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
long long st[N_ * 2]; int lz[N_ * 2], mn[N_ * 2], sz[N_ * 2];
void build() {
int i;
for (i = 0; i < N_; i++)
sz[N_ + i] = 1;
for (i = N_ - 1; i > 0; i--)
sz[i] = sz[i << 1 | 0] + sz[i << 1 | 1];
}
void put(int i, int x) {
st[i] += (long long) sz[i] * x, mn[i] += x;
if (i < N_)
lz[i] += x;
}
void pus(int i) {
if (lz[i])
put(i << 1 | 0, lz[i]), put(i << 1 | 1, lz[i]), lz[i] = 0;
}
void pul(int i) {
if (!lz[i]) {
int l = i << 1, r = l | 1;
st[i] = st[l] + st[r], mn[i] = mn[l];
}
}
void push(int i) {
int h;
for (h = H_; h > 0; h--)
pus(i >> h);
}
void pull(int i) {
while (i > 1)
pul(i >>= 1);
}
long long query(int l, int r) {
long long x;
push(l += N_), push(r += N_);
x = 0;
for ( ; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
x += st[l++];
if ((r & 1) == 0)
x += st[r--];
}
return x;
}
void update(int l, int r) {
int l_ = l += N_, r_ = r += N_;
if (l > r)
return;
push(l_), push(r_);
for ( ; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
put(l++, 1);
if ((r & 1) == 0)
put(r--, 1);
}
pull(l_), pull(r_);
}
int last(int a) {
int i;
if (mn[1] > a)
return -1;
i = 1;
while (i < N_) {
pus(i);
i = mn[i << 1 | 1] <= a ? i << 1 | 1 : i << 1 | 0;
}
return i - N_;
}
int main() {
static int ii[N];
int n, i;
long long ans;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d%d", &hh[i], &kk[i]);
ii[i] = i;
}
sort(ii, 0, n);
build();
ans = 0;
for (i = 0; i < n; i++) {
int i_ = ii[i], l = N_ - hh[i_], r = N_ - hh[i_] + kk[i_] - 1, a, r1, r2;
ans += query(l, r);
a = query(r, r), r1 = max(last(a - 1), l - 1), r2 = last(a);
update(l, r1), update(r2 - (kk[i_] - (r1 - l + 1)) + 1, r2);
}
printf("%lld\n", ans);
return 0;
}
Compilation message
sails.c: In function 'main':
sails.c:125:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
sails.c:127:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d%d", &hh[i], &kk[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1356 KB |
Output is correct |
2 |
Correct |
1 ms |
1308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1356 KB |
Output is correct |
2 |
Correct |
1 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1356 KB |
Output is correct |
2 |
Correct |
1 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1356 KB |
Output is correct |
2 |
Correct |
2 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1580 KB |
Output is correct |
2 |
Correct |
4 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
2432 KB |
Output is correct |
2 |
Correct |
58 ms |
2160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
5828 KB |
Output is correct |
2 |
Correct |
130 ms |
5916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
6180 KB |
Output is correct |
2 |
Correct |
62 ms |
5408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
6416 KB |
Output is correct |
2 |
Correct |
100 ms |
4420 KB |
Output is correct |