답안 #54003

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
54003 2018-07-02T07:52:55 Z Costin Andrei Oncescu(#1302) Pairs (IOI07_pairs) C++11
100 / 100
286 ms 24828 KB
#include<bits/stdc++.h>

using namespace std;

int B, N, D, M, x[100009], y[100009], z[100009];
pair < int, int > v[100009];
pair < pair < int, int >, pair < int, int > > h[100009];

int aibSZ, aib[150009];
void update (int pos, int val)
{
    while (pos <= aibSZ)
        aib[pos] += val,
        pos += pos - (pos & (pos - 1));
}

int query (int pos)
{
    int ans = 0;
    while (pos)
        ans += aib[pos],
        pos &= pos - 1;
    return ans;
}

int query (int l, int r)
{
    if (r > aibSZ)
        r = aibSZ;
    if (r < 0 || r < l) return 0;
    int ans = query (r);
    if (l > 0) ans -= query (l - 1);
    return ans;
}

namespace aib3D {
    int aib[227][227][227], aibSZ;
    void update (int x, int y, int z, int val)
    {
        for (int i=x; i<=aibSZ; i+=i-(i&(i-1)))
            for (int j=y; j<=aibSZ; j+=j-(j&(j-1)))
                for (int k=z; k<=aibSZ; k+=k-(k&(k-1)))
                    aib[i][j][k] += val;
    }

    int query (int x, int y, int z)
    {
        int ans = 0;
        for (int i=x; i > 0; i &= i-1)
            for (int j=y; j > 0; j &= j - 1)
                for (int k=z; k > 0; k &= k - 1)
                    ans += aib[i][j][k];
        return ans;
    }

    int query (int a1, int b1, int c1, int a2, int b2, int c2)
    {
        if (a2 > aibSZ) a2 = aibSZ;
        if (b2 > aibSZ) b2 = aibSZ;
        if (c2 > aibSZ) c2 = aibSZ;
        if (a1 < 1) a1 = 1;
        if (b1 < 1) b1 = 1;
        if (c1 < 1) c1 = 1;
        if (a1 > a2 || b1 > b2 || c1 > c2) return 0;
        int ans = query (a2, b2, c2) - query (a1 - 1, b2, c2) - query (a2, b1 - 1, c2) - query (a2, b2, c1 - 1) +
                  query (a1 - 1, b1 - 1, c2) + query (a1 - 1, b2, c1 - 1) + query (a2, b1 - 1, c1 - 1) -
                  query (a1 - 1, b1 - 1, c1 - 1);
        return ans;
    }
}

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

scanf ("%d %d %d %d", &B, &N, &D, &M);
if (B == 1)
{
    for (int i=1; i<=N; i++)
        scanf ("%d", &x[i]);
    sort (x + 1, x + N + 1);
    long long ans = 1LL * N * (N - 1) / 2;
    int j = 0;
    for (int i=1; i<=N; i++)
    {
        while (x[i] - x[j + 1] > D)
            j ++;
        ans -= j;
    }
    printf ("%lld\n", ans);
    return 0;
}
if (B == 2)
{
    for (int i=1; i<=N; i++)
    {
        int a, b;
        scanf ("%d %d", &a, &b);
        v[i] = {a - b, a + b};
    }
    sort (v + 1, v + N + 1), aibSZ = 2 * M;
    int j = 1;
    long long ans = 0;
    update (v[1].second, +1);
    for (int i=2; i<=N; i++)
    {
        while (v[i].first - v[j].first > D)
            update (v[j].second, -1), j ++;
        ans += query (v[i].second - D, v[i].second + D);
        update (v[i].second, +1);
    }
    printf ("%lld\n", ans);
    return 0;
}
aib3D::aibSZ = 3 * M;
for (int i=1; i<=N; i++)
{
    int a, b, c;
    scanf ("%d %d %d", &a, &b, &c);
    h[i].first.first = a - b - c;
    h[i].first.second = a + b - c + M;
    h[i].second.first = a - b + c + M;
    h[i].second.second = a + b + c;
}
sort (h + 1, h + N + 1);
int j = 1;
long long ans = 0;
aib3D::update (h[1].first.second, h[1].second.first, h[1].second.second, +1);
for (int i=2; i<=N; i++)
{
    while (h[i].first.first - h[j].first.first > D)
        aib3D::update (h[j].first.second, h[j].second.first, h[j].second.second, -1), j ++;
    ans += aib3D::query (h[i].first.second - D, h[i].second.first - D, h[i].second.second - D,
                         h[i].first.second + D, h[i].second.first + D, h[i].second.second + D);
    aib3D::update (h[i].first.second, h[i].second.first, h[i].second.second, +1);
}
printf ("%lld\n", ans);
return 0;
}

Compilation message

pairs.cpp: In function 'int main()':
pairs.cpp:77:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d %d %d %d", &B, &N, &D, &M);
 ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp:81:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%d", &x[i]);
         ~~~~~~^~~~~~~~~~~~~
pairs.cpp:99:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%d %d", &a, &b);
         ~~~~~~^~~~~~~~~~~~~~~~~
pairs.cpp:120:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d %d", &a, &b, &c);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 856 KB Output is correct
2 Correct 20 ms 880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 928 KB Output is correct
2 Correct 40 ms 928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 928 KB Output is correct
2 Correct 27 ms 928 KB Output is correct
3 Correct 26 ms 928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1056 KB Output is correct
2 Correct 3 ms 1056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 1360 KB Output is correct
2 Correct 32 ms 1360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 1368 KB Output is correct
2 Correct 40 ms 1368 KB Output is correct
3 Correct 40 ms 1368 KB Output is correct
4 Correct 39 ms 1368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 1872 KB Output is correct
2 Correct 46 ms 1872 KB Output is correct
3 Correct 44 ms 1896 KB Output is correct
4 Correct 44 ms 2020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 12268 KB Output is correct
2 Correct 11 ms 12308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 12308 KB Output is correct
2 Correct 110 ms 12308 KB Output is correct
3 Correct 54 ms 12308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 194 ms 16796 KB Output is correct
2 Correct 176 ms 16796 KB Output is correct
3 Correct 90 ms 16796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 257 ms 24816 KB Output is correct
2 Correct 286 ms 24816 KB Output is correct
3 Correct 117 ms 24828 KB Output is correct