This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
array<ll, MAXN> bit;
void update(int id, int val)
{
while (id < MAXN)
{
bit[id] += val;
id += id&-id;
}
}
ll query(int id)
{
ll resp = 0;
while (id > 0)
{
resp += bit[id];
id -= id&-id;
}
return resp;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
vector<pair<int, int>> v(N);
for (auto &[H, K] : v) cin >> H >> K;
sort(v.begin(), v.end());
int bitFirstId = MAXN-1;
for (auto [H, K] : v)
{
bitFirstId = MAXN-H;
int biggestUpd = query(bitFirstId+K-1);
if (query(bitFirstId) != biggestUpd)
{
int ini = bitFirstId, fim = MAXN-1;
while (ini < fim)
{
int m = (ini + fim + 1) >> 1;
if (query(m) < biggestUpd) ini = m;
else fim = m-1;
}
update(bitFirstId, +1); update(ini+1, -1);
K -= (ini - bitFirstId + 1);
}
int ini = bitFirstId, fim = MAXN-1;
if (query(MAXN-1) == biggestUpd) ini = fim = MAXN;
while (ini < fim)
{
int m = (ini + fim) >> 1;
if (query(m) > biggestUpd) fim = m;
else ini = m+1;
}
update(fim-K, +1); update(fim, -1);
}
ll resp = 0;
for (int i = 1; i < MAXN; i++)
{
ll x = query(i);
resp += (x * (x-1LL))/2LL;
}
cout << resp << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |