Submission #1354492

#TimeUsernameProblemLanguageResultExecution timeMemory
1354492adscodingPairs (IOI07_pairs)C++20
60 / 100
101 ms41468 KiB
#include <bits/stdc++.h>
#define pb emplace_back
#define SZ(x) ((int)x.size())
#define fi first
#define se second
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __prine_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        cerr << "  ,  ";
        ++s;
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = {0, (__prine_one(s, args), 0)...};
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, const X &b)
{
    if (b > a)
    {
        a = b;
        return true;
    }
    return false;
}

template<class X>
bool minimize(X &a, const X &b)
{
    if (b < a)
    {
        a = b;
        return true;
    }
    return false;
}

#define inline inline __attribute__ ((always_inline))

// --------------------------------------------------------------------------------------------

constexpr int maxn = 1e5 + 3;
int n, B, M;
ll D;

// --------------------------------------------------------------------------------------------


void readInput()
{
    cin >> B >> n >> D >> M;
}

namespace sub1
{
    int a[maxn];
    ll res;

    void solve()
    {
        FOR(i, 1, n)
            cin >> a[i];
        
        sort(a + 1, a + n + 1);

        int j = 1;
        FOR(i, 1, n)
        {
            while (j <= i && a[i] - a[j] > D)
                ++j;
            res += i - j;
        }

        cout << res;
    }
}

namespace sub2
{
    ll res;

    struct Point
    {
        ll x, y;
        bool operator < (const Point &other) const
        {
            if (x != other.x) return x < other.x;
            return y < other.y;
        }
    } a[maxn];
    vector<int> idsy;

    int lower(ll x) {return lower_bound(all(idsy), x) - idsy.begin() + 1;}
    int upper(ll x) {return upper_bound(all(idsy), x) - idsy.begin() + 1;}

    struct FenTree
    {
        vector<int> fw;
        void init(int n)
        {
            fw.assign(n + 3, 0);
        }

        void upd(int pos, int val)
        {
            for (; pos <= SZ(fw); pos += pos & -pos)
                fw[pos] += val;
        }

        int get(int pos)
        {
            int res = 0;
            for (; pos; pos &= pos - 1)
                res += fw[pos];
            return res;
        }

        int get(int l, int r)
        {
            if (l > r) return 0;
            return get(r) - get(l - 1);
        }
    } fw;

    void solve()
    {
        FOR(i, 1, n)
        {
            ll curx, cury; cin >> curx >> cury;
            a[i].x = curx + cury;
            a[i].y = curx - cury;
            idsy.push_back(a[i].y);
        }

        sort(a + 1, a + n + 1);
        uni(idsy);

        fw.init(SZ(idsy));

        int j = 1;
        FOR(i, 1, n)
        {
            while (a[i].x - a[j].x > D)
            {
                fw.upd(lower(a[j].y), -1);
                ++j;
            }
            res += fw.get(lower(a[i].y - D), upper(a[i].y + D) - 1);
            fw.upd(lower(a[i].y), 1);
        }

        cout << res;
    }
}

namespace sub3
{
    ll res;

    struct Point
    {
        ll x, y, z, t;
        int idy, idz, idt;
    } a[maxn];

    vector<int> yids, zids, tids;

    inline int lower(vector<int> &ids, ll x) { return lower_bound(all(ids), x) - ids.begin() + 1; }
    inline int upper(vector<int> &ids, ll x) { return upper_bound(all(ids), x) - ids.begin() + 1; }

    struct FenTree2D
    {
        vector<vector<int>> ids;
        vector<vector<int>> fw;

        inline void init(int n)
        {
            ids.resize(n + 3);
            fw.resize(n + 3);

            FOR(i, 1, n)
                ids[i].push_back(int(-1e9));
        }

        inline void upd(int x, int y, int val)
        {
            for (int curx = x; curx <= SZ(zids); curx += curx & -curx)
                for (int cury = upper(ids[curx], y) - 1; cury < SZ(fw[curx]); cury += cury & -cury)
                    fw[curx][cury] += val;
        }

        inline int get(int x, int y)
        {
            int res = 0;
            for (int curx = x; curx; curx &= curx - 1)
                for (int cury = upper(ids[curx], y) - 1; cury; cury &= cury - 1)
                    res += fw[curx][cury];
            return res;
        }

        inline int get(int x, int y, int u, int v)
        {
            return get(u, v) - get(u, y - 1) - get(x - 1, v) + get(x - 1, y - 1);
        }

    } fw[305];

    inline void updPoint(Point &a, int val)
    {
        fw[a.idy].upd(a.idz, a.idt, val);
    }

    void solve()
    {
        FOR(i, 1, n)
        {
            ll x, y, z; cin >> x >> y >> z;
            a[i].x = x + y + z;
            a[i].y = x + y - z;
            a[i].z = x - y + z;
            a[i].t = x - y - z;

            yids.push_back(a[i].y);
            zids.push_back(a[i].z);
            tids.push_back(a[i].t);
        }

        sort(a + 1, a + n + 1, [&](const Point &a, const Point &b)
        {
            if (a.x != b.x) return a.x < b.x;
            if (a.y != b.y) return a.y < b.y;
            if (a.z != b.z) return a.z < b.z;
            return a.t < b.t;
        });

        uni(yids);
        uni(zids);
        uni(tids);

        FOR(i, 1, n)
        {
            a[i].idy = lower(yids, a[i].y);
            a[i].idz = lower(zids, a[i].z);
            a[i].idt = lower(tids, a[i].t);
        }

        fw[1].init(SZ(zids));

        FOR(i, 1, n)
        {
            for (int id = a[i].idz; id <= SZ(zids); id += id & -id)
            {
                // dbg(id, a[i].idt);
                fw[1].ids[id].push_back(a[i].idt);
            }
        }

        FOR(i, 1, SZ(zids))
        {
            uni(fw[1].ids[i]);
            fw[1].ids[i].push_back(1e9);
            fw[1].ids[i].push_back(1e9);
            fw[1].fw[i].assign(SZ(fw[1].ids[i]), 0);
        }

        FOR(i, 2, SZ(yids))
            fw[i] = fw[i - 1];

        int j = 1;
        FOR(i, 1, n)
        {
            while (a[i].x - a[j].x > D)
            {
                updPoint(a[j], -1);
                ++j;
            }


            updPoint(a[i], 1);
        }

        cout << res;
    }
}

void solve()
{
    if (B == 1) return sub1 :: solve(), void();
    if (B == 2) return sub2 :: solve(), void();
    sub3 :: solve();
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    readInput();
    solve();
    return 0;
}

Compilation message (stderr)

pairs.cpp: In function 'int main()':
pairs.cpp:333:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  333 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp:334:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  334 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...