Submission #314020

# Submission time Handle Problem Language Result Execution time Memory
314020 2020-10-17T21:36:23 Z ant101 Pairs (IOI07_pairs) C++14
70 / 100
4000 ms 99448 KB
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
#include <complex>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
#define mp make_pair
#define f first
#define s second

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e2+5;
const ll INF = 1e18;
const ld PI = 4*atan((ld)1);
const int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};


namespace io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
        // cin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int
        if (sz(s)) {
            setIn(s+".in");
            setOut(s+".out");
        } // for USACO
    }
}

using namespace io;

namespace input {
    template<class T> void re(complex<T>& x);
    template<class T1, class T2> void re(pair<T1,T2>& p);
    template<class T> void re(vector<T>& a);
    template<class T, size_t SZ> void re(array<T,SZ>& a);
    
    template<class T> void re(T& x) { cin >> x; }
    void re(double& x) { string t; re(t); x = stod(t); }
    void re(ld& x) { string t; re(t); x = stold(t); }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
        re(first); re(rest...);
    }
    
    template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
    template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
    template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
    template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}

namespace output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);
    
    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
        pr(first); pr(rest...);
    }
    
    template<class T1, class T2> void pr(const pair<T1,T2>& x) {
        pr("{",x.f,", ",x.s,"}");
    }
    template<class T> void prContain(const T& x) {
        pr("{");
        bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
        pr("}");
    }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
    
    void ps() { pr("\n"); }
    template<class Arg> void ps(const Arg& first) {
        pr(first); ps(); // no space at end of line
    }
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
        pr(first," "); ps(rest...); // print w/ spaces
    }
}

using namespace output;

using namespace input;

ll add(ll a, ll b) {
    a += b;
    if(a >= MOD) {
        a -= MOD;
    }
    return a;
}
ll sub(ll a, ll b) {
    a -= b;
    if(a < 0) {
        a += MOD;
    }
    return a;
}

ll mul(ll a, ll b) {
    return (a * b)%MOD;
}

void add_self(ll& a, ll b) {
    a = add(a, b);
}
void sub_self(ll& a, ll b) {
    a = sub(a, b);
}
void mul_self(ll& a, ll b) {
    a = mul(a, b);
}


vector<vector<vector<ll>>> bit;
ll n, m, l;
ll B, N, D, M;
pair<pl, pl> pts[100010];

// init(...) { ... }

void init() {
    if (B == 2) {
        vector<vector<ll>> v(1, vector<ll>(76000*3));
        bit.pb(v);
    } else {
        F0R (i, 76*3) {
            vector<vector<ll>> v(76*3, vector<ll>(76*3));
            bit.push_back(v);
        }
    }
}

ll sum(ll x, ll y, ll z) {
//    ps(x, y, z);
    ll ret = 0;
    if (x == -1 || y == -1 || z == -1) {
        return 0;
    }
    for (ll i = x; i >= 0; i = (i & (i + 1)) - 1)
        for (ll j = y; j >= 0; j = (j & (j + 1)) - 1)
            for (ll k = z; k >= 0; k = (k & (k + 1)) - 1)
                ret += bit[i][j][k];
    return ret;
}

void add(ll x, ll y, ll z, ll delta) {
//    ps(x, y, z, delta);
    for (ll i = x; i < n; i = i | (i + 1))
        for (ll j = y; j < m; j = j | (j + 1))
            for (ll k = z; k < l; k = k | (k + 1))
                bit[i][j][k] += delta;
}


int main() {
    setIO();
    re(B, N, D, M);
    if (B == 1) {
        F0R (i, N) {
            re(pts[i].f.f);
        }
        sort(pts, pts+N);
        ll curr = 0;
        ll ans = 0;
        F0R (i, N) {
            while (curr+1 < N && pts[curr+1].f.f-pts[i].f.f <= D) {
                curr++;
            }
            ans+=curr-i;
        }
        ps(ans);
        return 0;
    }
    if (B == 2) {
        n = 1;
        m = 1;
        l = 76000*3;
        F0R (i, N) {
            ll a, b;
            re(a, b);
            pts[i].f.f = a+b;
            pts[i].f.s = a-b;
        }
        init();
    } else {
        n = 76*3;
        m = 76*3;
        l = 76*3;
        F0R (i, N) {
            ll a, b, c;
            re(a, b, c);
            pts[i].f.f = a+b+c;
            pts[i].f.s = a+b-c;
            pts[i].s.f = a-b+c;
            pts[i].s.s = a-b-c;
        }
        init();
    }
    ll curr = 0;
    ll ans = 0;
    F0R(i, N) {
        if (B == 2) {
            pts[i].f.f+=76000;
            pts[i].f.s+=76000;
        } else {
            pts[i].f.f+=76;
            pts[i].f.s+=76;
            pts[i].s.f+=76;
            pts[i].s.s+=76;
        }
    }
    sort(pts, pts+N);
    if (B == 2) {
        add(0, 0, pts[0].f.s, 1);
    } else {
        add(pts[0].f.s, pts[0].s.f, pts[0].s.s, 1);
    }
    F0R (i, N) {
        while (curr+1 < N && pts[curr+1].f.f-pts[i].f.f <= D) {
            curr++;
            if (B == 2) {
                add(0, 0, pts[curr].f.s, 1);
            } else {
                add(pts[curr].f.s, pts[curr].s.f, pts[curr].s.s, 1);
            }
        }
        if (B == 2) {
            ll s2 = (pts[i].f.s-D-1 < 0 ? 0 : sum(0, 0, pts[i].f.s-D-1));
            ans+=sum(0, 0, min(pts[i].f.s+D, (ll)76000*3-1))-s2;
        } else {
            ll x2 = min(pts[i].f.s+D, (ll)76*3-1), y2 = min(pts[i].s.f+D, (ll)76*3-1), z2 = min(pts[i].s.s+D, (ll)76*3-1);
            ll x1 = max(pts[i].f.s-D, 0ll), y1 = max(pts[i].s.f-D, 0ll), z1 = max(pts[i].s.s-D, 0ll);
            ans+=sum(x2, y2, z2)-sum(x2, y2, z1-1)-sum(x2, y1-1, z2)-sum(x1-1, y2, z2)+sum(x1-1, y1-1, z2)+sum(x1-1, y2, z1-1)+sum(x2, y1-1, z1-1)-sum(x1-1, y1-1, z1-1);
        }
        if (B == 2) {
            add(0, 0, pts[i].f.s, -1);
        } else {
            add(pts[i].f.s, pts[i].s.f, pts[i].s.s, -1);
        }
    }
    ps(ans-N);
    return 0;
}

Compilation message

pairs.cpp: In function 'void io::setIn(std::string)':
pairs.cpp:67:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   67 |     void setIn(string s) { freopen(s.c_str(),"r",stdin); }
      |                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
pairs.cpp: In function 'void io::setOut(std::string)':
pairs.cpp:68:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   68 |     void setOut(string s) { freopen(s.c_str(),"w",stdout); }
      |                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 3456 KB Output is correct
2 Correct 18 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 3456 KB Output is correct
2 Correct 22 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 3528 KB Output is correct
2 Correct 23 ms 3456 KB Output is correct
3 Correct 22 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3976 KB Output is correct
2 Correct 4 ms 3976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 7000 KB Output is correct
2 Correct 40 ms 7048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 7040 KB Output is correct
2 Correct 40 ms 7048 KB Output is correct
3 Correct 48 ms 7048 KB Output is correct
4 Correct 41 ms 7048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 7096 KB Output is correct
2 Correct 45 ms 7040 KB Output is correct
3 Correct 45 ms 7040 KB Output is correct
4 Correct 43 ms 7048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4089 ms 95736 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 186 ms 98680 KB Output is correct
2 Correct 205 ms 99320 KB Output is correct
3 Correct 241 ms 99448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 4066 ms 98704 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 4080 ms 98680 KB Time limit exceeded
2 Halted 0 ms 0 KB -