Submission #362300

# Submission time Handle Problem Language Result Execution time Memory
362300 2021-02-02T14:54:27 Z ngpin04 Sails (IOI07_sails) C++14
100 / 100
59 ms 2412 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 1e5;

pair <int, int> a[N + 5];

int bit[N + 5];
int n;

void update(int pos, int val)
{
    for (; pos <= N; pos += pos & -pos)
        bit[pos] += val;
}

void rngupd(int l, int r)
{
    update(l, 1);
    update(r + 1, -1);
}

int getval(int pos)
{
    int res = 0;
    for (; pos > 0; pos -= pos & -pos)
        res += bit[pos];
    return res;
}

int getpos(int x)
{
    int cur = 0;
    int val = 0;
    for (int i = 20; i >= 0; i--) if (cur + (1 << i) <= N)
    {
        if (val + bit[cur + (1 << i)] < x)
        {
            cur += (1 << i);
            val += bit[cur];
        }
    }
    return cur + 1;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("file.inp","r",stdin);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i].fi >> a[i].se;

    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++)
        a[i].fi = N - a[i].fi + 1;

    for (int i = 1; i <= n; i++)
    {
        int pos = a[i].fi;
        int cnt = a[i].se;

        int las = getval(pos + cnt - 1);


        int nxt = getpos(las) - 1;
        if (pos <= nxt)
        {
            rngupd(pos, nxt);
            cnt -= (nxt - pos + 1);
        }

        int laspos = getpos(las + 1) - 1;
        rngupd(laspos - cnt + 1, laspos);
    }

    long long ans = 0;
    for (int i = 1; i <= N; i++)
    {
        int cnt = getval(i);
        if (cnt > 0)
            ans += (long long) cnt * (cnt - 1) >> 1;
    }
    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 3 ms 400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 3 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 364 KB Output is correct
2 Correct 3 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 492 KB Output is correct
2 Correct 15 ms 876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 1260 KB Output is correct
2 Correct 41 ms 2284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 1516 KB Output is correct
2 Correct 45 ms 2156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 1516 KB Output is correct
2 Correct 47 ms 2412 KB Output is correct