Submission #954682

# Submission time Handle Problem Language Result Execution time Memory
954682 2024-03-28T10:43:33 Z codefox Pairs (IOI07_pairs) C++14
80 / 100
4000 ms 338124 KB
#include<bits/stdc++.h>

using namespace std;

#define pii pair<int, int>
#define ll long long
#define arr array<int, 2>

#pragma GCC optimize("O2")

int N = 1<<18;
int M = 1<<17;
int K = 17;

int go(int a, int b, int d, vector<vector<arr>> &bin, vector<int> &counter, vector<vector<int>> &upd)
{
    if (bin[a][b][d]==-1)
    {
        upd[a].push_back(0);
        bin[a].push_back({-1, -1});
        bin[a][b][d] = counter[a];
        counter[a]++;
    }
    return bin[a][b][d];
}

void update(int a, int b, vector<vector<arr>> &bin, vector<int> &counter, vector<vector<int>> &upd)
{
    a += N; 
    while (a)
    {
        int curr = 0;
        int bb = b;
        for (int i = K; i >= 0; i--)
        {
            upd[a][curr]++;
            if (bb>=(1<<i))
            {
                bb -= 1<<i;
                curr = go(a, curr, 1, bin, counter, upd);
            }
            else curr = go(a, curr, 0, bin, counter, upd);
        }
        upd[a][curr]++;
        a/=2;
    }
}

ll ac2d(int a, int curr, int l, int r, int x, int y, vector<vector<arr>> &bin, vector<vector<int>> &upd)
{
    if (r <=x || l >= y) return 0;
    if (l >= x && r <= y)
    {
        return upd[a][curr];
    }
    int m = (l+r)/2;
    ll sol = 0;
    if (bin[a][curr][0]!=-1) sol += ac2d(a, bin[a][curr][0], l, m, x, y, bin, upd);
    if (bin[a][curr][1]!=-1) sol += ac2d(a, bin[a][curr][1], m, r, x, y, bin, upd);
    return sol;
}

ll acc(int curr, int l, int r, int a, int b, int x, int y, vector<vector<arr>> &bin, vector<vector<int>> &upd)
{
    if (r <=a || l >= b) return 0;
    if (l >= a && r <= b)
    {
        return ac2d(curr, 0, 0, N, x, y, bin, upd);
    }
    int m = (l+r)/2;
    return acc(curr*2, l, m, a, b, x, y, bin, upd)+ acc(curr*2+1, m, r, a, b, x, y, bin, upd);
}

void update3(int a, int b, vector<vector<short>> &bin)
{
    a += N; 
    while (a)
    {
        int bb = b+N;
        while (bb)
        {
            bin[a][bb]++;
            bb/=2;
        }
        a/=2;
    }
}

ll ac2d3(int a, int curr, int l, int r, int x, int y, vector<vector<short>> &bin)
{
    if (r <=x || l >= y) return 0;
    if (l >= x && r <= y)
    {
        return bin[a][curr];
    }
    int m = (l+r)/2;
    return ac2d3(a, curr*2, l, m, x, y, bin) +
    ac2d3(a, curr*2+1, m, r, x, y, bin);
}

ll acc3(int curr, int l, int r, int a, int b, int x, int y, vector<vector<short>> &bin)
{
    if (r <=a || l >= b) return 0;
    if (l >= a && r <= b)
    {
        return ac2d3(curr, 1, 0, N, x, y, bin);
    }
    int m = (l+r)/2;
    return acc3(curr*2, l, m, a, b, x, y, bin)+ acc3(curr*2+1, m, r, a, b, x, y, bin);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int b, n, d, m;
    cin >> b >> n >> d >> m;

    if (b == 1)
    {
        vector<int> nums(n);
        for (int i =0; i < n; i++) cin >> nums[i];
        sort(nums.begin(), nums.end());

        ll sol = 0;
        queue<int> q;
        for (int i = 0; i < n; i++)
        {
            while (q.size() && nums[i]-q.front()> d) q.pop();
            sol += q.size();
            q.push(nums[i]);
        }
        cout << sol;
    }
    else if (b==2)
    {
        vector<vector<arr>> bin;
        vector<int> counter;
        vector<vector<int>> upd;
        bin.assign(2*N, vector<arr>(1, {-1, -1}));
        counter.assign(2*N, 1);
        upd.assign(2*N, vector<int>(1, 0));

        ll sol = 0;

        for (int i = 0; i < n; i++)
        {
            int x, y;
            cin >> x >> y;
            sol += acc(1, 0, N, x+y-d, x+y+d+1, y-x+M-d, y-x+M+d+1, bin, upd);
            update(x+y, y-x+M, bin, counter, upd);
        }
        cout << sol;
    }
    else if (b==3)
    {
        N = 1<<8;
        M = 1<<7;
        K = 7;

        vector<vector<vector<short>>> bin(76, vector<vector<short>>(2*N, vector<short>(2*N, 0)));

        ll sol = 0;
        for (int i =0; i < n; i++)
        {
            int x, y, z;
            cin >> x >> y >> z;
            for (int j = 0; j <= z; j++)
            {
                int h = d-(z-j);
                if (h <0) continue;
                sol +=acc3(1, 0, N, x+y-h, x+y+h+1, y-x+M-h, y-x+M+h+1, bin[j]);
            }
            for (int j = z+1; j < 76; j++)
            {
                int h = d-(j-z);
                if (h <0) break;
                sol +=acc3(1, 0, N, x+y-h, x+y+h+1, y-x+M-h, y-x+M+h+1, bin[j]);
            }
            update3(x+y, y-x+M, bin[z]);
        }
        cout << sol;
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 604 KB Output is correct
2 Correct 11 ms 1120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 604 KB Output is correct
2 Correct 14 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 860 KB Output is correct
2 Correct 15 ms 860 KB Output is correct
3 Correct 14 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 65104 KB Output is correct
2 Correct 68 ms 65108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 293 ms 61600 KB Output is correct
2 Correct 253 ms 61596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 744 ms 97736 KB Output is correct
2 Correct 720 ms 97736 KB Output is correct
3 Correct 527 ms 80644 KB Output is correct
4 Correct 577 ms 90672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2431 ms 337968 KB Output is correct
2 Correct 2219 ms 338124 KB Output is correct
3 Correct 641 ms 102212 KB Output is correct
4 Correct 598 ms 92844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 41308 KB Output is correct
2 Correct 30 ms 41308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 41308 KB Output is correct
2 Correct 566 ms 41480 KB Output is correct
3 Correct 1111 ms 41476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 969 ms 41480 KB Output is correct
2 Execution timed out 4030 ms 41308 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3957 ms 41480 KB Output is correct
2 Execution timed out 4075 ms 41980 KB Time limit exceeded
3 Halted 0 ms 0 KB -