#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
struct ura {
ull h, k;
};
ull n, tam;
ura masts[100005];
ll ST[400005];
bool cmp(const ura &a, const ura &b) {
return a.h < b.h;
}
void actualizar(ull pos, int val, ull nodo=1, ull ini=1, ull fin=100000) {
if (pos < ini || pos > fin)
return;
if (ini == fin) {
ST[nodo] += val;
return;
}
ull 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(ull pos, ull nodo=1, ull ini=1, ull fin=100000) {
if (pos < ini)
return 0;
if (pos >= fin)
return ST[nodo];
ll s=0;
ull mid = (ini + fin) / 2;
s += suma(pos, nodo*2, ini, mid);
s += suma(pos, nodo*2+1, mid+1, fin);
return s;
}
ull posicion_menor(ull &num) {
ull izq=1, der=100000, mid, r=0;
while (izq <= der) {
mid = (izq+der) / 2;
if (suma(mid) < num) {
r = mid;
der = mid-1;
} else {
izq = mid+1;
}
}
return r;
}
ull posicion_igual(ull &num) {
ull izq=1, der=100000;
ull mid, r=0;
while (izq <= der) {
mid = (izq+der) / 2;
if (suma(mid) > num) {
izq = mid+1;
} else {
der = mid-1;
r = mid;
}
}
return r;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(ull i=0; i<n; i++) {
cin >> masts[i].h >> masts[i].k;
tam = max(tam, masts[i].h);
}
sort(masts, masts+n, cmp);
ull pos_menor, pos_igual, s;
for(ull i=0; i<n; i++) {
s = suma(masts[i].h - masts[i].k + 1);
pos_menor = posicion_menor(s);
pos_igual = posicion_igual(s);
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);
}
ull resp=0;
for(ull i=0; i<=masts[n-1].h; i++) {
s = suma(i);
resp += (s * (s - 1)) / 2;
}
cout << resp;
return 0;
}
Compilation message
sails.cpp: In function 'ull posicion_menor(ull&)':
sails.cpp:49:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'ull' {aka 'long long unsigned int'} [-Wsign-compare]
49 | if (suma(mid) < num) {
| ~~~~~~~~~~^~~~~
sails.cpp: In function 'ull posicion_igual(ull&)':
sails.cpp:64:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'ull' {aka 'long long unsigned int'} [-Wsign-compare]
64 | if (suma(mid) > num) {
| ~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 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 |
1 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 |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
468 KB |
Output is correct |
2 |
Correct |
8 ms |
2380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
1080 KB |
Output is correct |
2 |
Correct |
48 ms |
1168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
1684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
2612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
4412 KB |
Output is correct |
2 |
Incorrect |
139 ms |
4264 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
4776 KB |
Output is correct |
2 |
Correct |
142 ms |
4204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
4952 KB |
Output is correct |
2 |
Correct |
184 ms |
3520 KB |
Output is correct |