Submission #157679

# Submission time Handle Problem Language Result Execution time Memory
157679 2019-10-12T17:28:27 Z stefdasca Pairs (IOI07_pairs) C++14
62 / 100
4000 ms 109852 KB
#include<bits/stdc++.h>
using namespace std;
int tip, n, d, m;
long long ans;


struct tp
{
    int x, y, z;
};
tp vv[10002];
void solvesmall()
{
    for(int i = 1; i <= n; ++i)
    {
        cin >> vv[i].x;
        if(tip >= 2)
            cin >> vv[i].y;
        if(tip >= 3)
            cin >> vv[i].z;
    }
    for(int i = 1; i <= n; ++i)
        for(int j = i+1; j <= n; ++j)
        {
            int dd = abs(vv[i].x - vv[j].x) + abs(vv[i].y - vv[j].y) + abs(vv[i].z - vv[j].z);
            if(dd <= d)
                ++ans;
        }
    cout << ans << '\n';
}

int v[100002];
void solve1()
{
    for(int i = 1; i <= n; ++i)
        cin >> v[i];
    sort(v+1, v+n+1);
    for(int i = 1; i <= n; ++i)
    {
        int sm = i;
        int st = 1;
        int dr = i-1;
        while(st <= dr)
        {
            int mid = (st + dr) / 2;
            if(v[i] - v[mid] <= d)
                sm = mid, dr = mid - 1;
            else
                st = mid + 1;
        }
        ans += (i - sm);
    }
    cout << ans << '\n';
}

pair<int, int> nr[100002];
int aib[400][200002];
vector<int>coord[150002];
void upd(int bu, int pz, int val)
{
    for(; pz <= 200000; pz += (pz & (-pz)))
        aib[bu][pz] += val;
}
int compute(int bu, int pz)
{
    int ans = 0;
    for(; pz > 0; pz -= (pz & (-pz)))
        ans += aib[bu][pz];
    return ans;
}
int cb(int linie, int col)
{
    int ans = 0;
    int st = 0;
    int dr = coord[linie].size() - 1;
    if(coord[linie].back() <= col)
        return coord[linie].size();
    if(coord[linie][0] > col)
        return 0;
    while(st <= dr)
    {
        int mid = (st + dr) / 2;
        if(coord[linie][mid] <= col)
            ans = (mid + 1), st = mid + 1;
        else
            dr = mid - 1;
    }
    return ans;
}
void solve2()
{
    int buk = 400;
    for(int i = 1; i <= n; ++i)
    {
        int x, y;
        cin >> x >> y;
        // x = min(75000, i), y = min(75000, i);
        nr[i].first = x + y;
        nr[i].second = x - y + 100000;
        coord[nr[i].first].push_back(nr[i].second);
        int nrbuk = nr[i].first / buk + (nr[i].first % buk > 0);
        upd(nrbuk, nr[i].second, 1);
    }
    for(int i = 0; i <= 150000; ++i)
        sort(coord[i].begin(), coord[i].end());
    for(int i = 1; i <= n; ++i)
    {
        int st = max(1, nr[i].first - d);
        int sf = min(150000, nr[i].first + d);
        int nrbuk = st / buk + (st % buk > 0);
        int nr2 = nr[i].first / buk + (nr[i].first % buk > 0);
        --ans;
        while(st <= sf && st <= nrbuk * buk)
        {
            if(!coord[st].empty())
                ans += cb(st, nr[i].second + d) - cb(st, nr[i].second - d - 1);
            ++st;
        }
        while(st + buk - 1 <= sf)
        {
            nrbuk = st / buk + (st % buk > 0);
            ans += compute(nrbuk, min(200000, nr[i].second + d)) - compute(nrbuk, max(0, nr[i].second - d - 1));
            st += buk;
        }
        while(st <= sf)
        {
            if(!coord[st].empty())
                ans += cb(st, nr[i].second + d) - cb(st, nr[i].second - d - 1);
            ++st;
        }
    }
    cout << ans / 2 << '\n';
}

void solve3()
{

}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> tip >> n >> d >> m;
    if(tip == 1)
        solve1();
    else
        if(n <= 20000)
            solvesmall();
        else
            if(tip == 2)
                solve2();
            else
                if(tip == 3)
                    solve3();
    return 0;
}

Compilation message

pairs.cpp: In function 'void solve2()':
pairs.cpp:111:13: warning: unused variable 'nr2' [-Wunused-variable]
         int nr2 = nr[i].first / buk + (nr[i].first % buk > 0);
             ^~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3832 KB Output is correct
2 Correct 5 ms 3832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 4728 KB Output is correct
2 Correct 24 ms 4620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 5240 KB Output is correct
2 Correct 30 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 5112 KB Output is correct
2 Correct 34 ms 5180 KB Output is correct
3 Correct 38 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3832 KB Output is correct
2 Correct 7 ms 3832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 217 ms 5888 KB Output is correct
2 Correct 224 ms 5888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1573 ms 6264 KB Output is correct
2 Correct 1594 ms 6304 KB Output is correct
3 Correct 2208 ms 6280 KB Output is correct
4 Correct 1831 ms 6264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1522 ms 109852 KB Output is correct
2 Execution timed out 4081 ms 109688 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3832 KB Output is correct
2 Correct 7 ms 3832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 3832 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 3936 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 3832 KB Output isn't correct
2 Halted 0 ms 0 KB -