Submission #509516

# Submission time Handle Problem Language Result Execution time Memory
509516 2022-01-14T06:20:05 Z blue Pairs (IOI07_pairs) C++17
100 / 100
1632 ms 9952 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
#define sz(x) int(x.size())






















void solve_1()
{
    int N, D, M;
    cin >> N >> D >> M;

    int X[N];
    for(int i = 0; i < N; i++) cin >> X[i];

    ll ans = 0;
    sort(X, X+N);

    int l = 0;
    for(int r = 0; r < N; r++)
    {
        while(X[r] - X[l] > D) l++;
        ans += r-l;
    }

    cout << ans << '\n';
}







struct info2
{
    int xpy;
    int xmy;
    ll mul;
};

bool operator < (info2 A, info2 B)
{
    if(A.xpy == B.xpy) return (A.mul == 0);
    return A.xpy < B.xpy;
}



void solve_2()
{
    int N, D, M;
    cin >> N >> D >> M;

    int X[1+N], Y[1+N];
    for(int i = 1; i <= N; i++) cin >> X[i] >> Y[i];

    ll ans = 0;

    vector<info2> I;
    for(int i = 1; i <= N; i++)
    {
        I.push_back({X[i] + Y[i], X[i] - Y[i] + M, 0});

        I.push_back({X[i] + Y[i] - D - 1, X[i] - Y[i] + D + M, -1});
        I.push_back({X[i] + Y[i] - D - 1, X[i] - Y[i] - D - 1 + M, +1});

        I.push_back({X[i] + Y[i] + D, X[i] - Y[i] + D + M, +1});
        I.push_back({X[i] + Y[i] + D, X[i] - Y[i] - D - 1 + M, -1});
    }

    sort(I.begin(), I.end());

    vll BIT(1 + 2*M, 0);

    for(info2 i: I)
    {
        if(i.mul == 0)
        {
            for(int j = i.xmy; j <= 2*M; j += j&-j)
                BIT[j]++;
        }
        else
        {
            // if(i.xmy < 1) continue;

            for(int j = min(i.xmy, 2*M); j >= 1; j -= j&-j)
                ans += BIT[j] * i.mul;
        }
    }

    ans -= N;
    ans >>= 1;

    cout << ans << '\n';
}
















void solve_3()
{
    int N, D, M;
    cin >> N >> D >> M;

    int X[1+N], Y[1+N], Z[1+N];
    for(int i = 1; i <= N; i++) cin >> X[i] >> Y[i] >> Z[i];

    vi occZ[1+M];
    for(int i = 1; i <= N; i++)
    {
        occZ[Z[i]].push_back(i);
    }

    // vector<info2> upd[1+M], qr[1+M];
    ll ans = 0;

    for(int z1 = 1; z1 <= M; z1++)
    {
        if(occZ[z1].empty()) continue;
        for(int z2 = z1+1; z2 <= min(M, z1+D); z2++)
        {
            if(occZ[z2].empty()) continue;

            vll BIT(1+2*M, 0);

            vector<info2> I;

            int ND = D - (z2-z1);

            for(int i : occZ[z1])
            {
                I.push_back({X[i] + Y[i], X[i] - Y[i] + M, 0});
            }

            for(int i: occZ[z2])
            {
                I.push_back({X[i] + Y[i] - ND - 1, X[i] - Y[i] + ND + M, -1});
                I.push_back({X[i] + Y[i] - ND - 1, X[i] - Y[i] - ND - 1 + M, +1});

                I.push_back({X[i] + Y[i] + ND, X[i] - Y[i] + ND + M, +1});
                I.push_back({X[i] + Y[i] + ND, X[i] - Y[i] - ND - 1 + M, -1});
            }

            sort(I.begin(), I.end());

            for(info2 i: I)
            {
                if(i.mul == 0)
                {
                    for(int j = i.xmy; j <= 2*M; j += j&-j)
                        BIT[j]++;
                }
                else
                {
                    // if(i.xmy < 1) continue;

                    for(int j = min(i.xmy, 2*M); j >= 1; j -= j&-j)
                        ans += BIT[j] * i.mul;
                }
            }
        }









        ll sameans = 0;

        vector<info2> I;
        for(int i: occZ[z1])
        {
            I.push_back({X[i] + Y[i], X[i] - Y[i] + M, 0});

            I.push_back({X[i] + Y[i] - D - 1, X[i] - Y[i] + D + M, -1});
            I.push_back({X[i] + Y[i] - D - 1, X[i] - Y[i] - D - 1 + M, +1});

            I.push_back({X[i] + Y[i] + D, X[i] - Y[i] + D + M, +1});
            I.push_back({X[i] + Y[i] + D, X[i] - Y[i] - D - 1 + M, -1});
        }

        sort(I.begin(), I.end());

        vll BIT(1 + 2*M, 0);

        for(info2 i: I)
        {
            if(i.mul == 0)
            {
                for(int j = i.xmy; j <= 2*M; j += j&-j)
                    BIT[j]++;
            }
            else
            {
                // if(i.xmy < 1) continue;

                for(int j = min(i.xmy, 2*M); j >= 1; j -= j&-j)
                    sameans += BIT[j] * i.mul;
            }
        }

        sameans -= sz(occZ[z1]);
        sameans >>= 1;

        ans += sameans;





    }

    cout << ans << '\n';
}


















int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int B;
    cin >> B;
    if(B == 1) solve_1();
    else if(B == 2) solve_2();
    else solve_3();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 704 KB Output is correct
2 Correct 14 ms 700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 680 KB Output is correct
2 Correct 17 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 588 KB Output is correct
2 Correct 16 ms 692 KB Output is correct
3 Correct 17 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1616 KB Output is correct
2 Correct 2 ms 1676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 9392 KB Output is correct
2 Correct 97 ms 9396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 9408 KB Output is correct
2 Correct 79 ms 9404 KB Output is correct
3 Correct 85 ms 9336 KB Output is correct
4 Correct 85 ms 9388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 9940 KB Output is correct
2 Correct 130 ms 9932 KB Output is correct
3 Correct 86 ms 9940 KB Output is correct
4 Correct 102 ms 9952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 332 KB Output is correct
2 Correct 8 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 4376 KB Output is correct
2 Correct 495 ms 4504 KB Output is correct
3 Correct 401 ms 4400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 461 ms 3420 KB Output is correct
2 Correct 1391 ms 3588 KB Output is correct
3 Correct 1333 ms 3444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1051 ms 3388 KB Output is correct
2 Correct 1632 ms 3380 KB Output is correct
3 Correct 1520 ms 3404 KB Output is correct