#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct ura {
ll h, k;
};
ll n, tam;
ura masts[100005];
ll ST[400009];
bool cmp(const ura &a, const ura &b) {
return a.h < b.h;
}
void actualizar(ll pos, int val, ll nodo=1, ll ini=1, ll fin=100001) {
if (pos < ini || pos > fin)
return;
if (ini == fin) {
ST[nodo] += val;
return;
}
ll mid = (ini+fin) / 2;
actualizar(pos, val, nodo*2, ini, mid);
actualizar(pos, val, nodo*2+1, mid+1, fin);
ST[nodo] = ST[nodo*2] + ST[nodo*2+1];
}
ll suma(ll pos, ll nodo=1, ll ini=1, ll fin=100001) {
if (pos < ini)
return 0;
if (pos >= fin)
return ST[nodo];
ll s=0;
ll mid = (ini + fin) / 2;
s += suma(pos, nodo*2, ini, mid);
s += suma(pos, nodo*2+1, mid+1, fin);
return s;
}
ll posicion_menor(ll num, ll sum=0, int nodo=1, int ini=1, int fin=100001) {
if (ini == fin)
return ini;
ll mid = (ini+fin) / 2;
if (sum+ST[nodo*2] >= num) {
return posicion_menor(num, sum+ST[nodo*2], nodo*2+1, mid+1, fin);
}
return posicion_menor(num, sum, nodo*2, ini, mid);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(ll i=0; i<n; i++) {
cin >> masts[i].h >> masts[i].k;
tam = max(tam, masts[i].h);
}
sort(masts, masts+n, cmp);
ll pos_menor, pos_igual, s;
for(ll i=0; i<n; i++) {
s = suma(masts[i].h - masts[i].k + 1);
pos_menor = posicion_menor(s);
pos_igual = posicion_menor(s+1);
if (s != 0) {
///para pos_menor:
actualizar(pos_menor, 1);
actualizar(masts[i].h + 1, -1);
masts[i].k -= masts[i].h + 1 - pos_menor;
}
///para pos_igual:
actualizar(pos_igual, 1);
actualizar(pos_igual + masts[i].k, -1);
}
ll resp=0;
for(ll i=0; i<=masts[n-1].h; i++) {
s = suma(i);
resp += (s * (s - 1)) / 2;
}
cout << resp;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
9 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
980 KB |
Output is correct |
2 |
Correct |
24 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
1444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
2160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
3532 KB |
Output is correct |
2 |
Correct |
73 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
3660 KB |
Output is correct |
2 |
Correct |
58 ms |
3544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
3924 KB |
Output is correct |
2 |
Correct |
73 ms |
2436 KB |
Output is correct |