답안 #531160

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
531160 2022-02-27T21:51:03 Z Evang Pairs (IOI07_pairs) C++17
100 / 100
212 ms 27612 KB
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;

#ifdef _DEBUG
#define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el
#else
#define dout(x)
#endif

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define uid(a,b) uniform_int_distribution<int>(a,b)(rng)

#define ins insert
#define ssize(x) (int((x).size()))
#define bs(args...) binary_search(args)
#define lb(args...) lower_bound(args)
#define ub(args...) upper_bound(args)
#define all(x) (x).begin(),(x).end()
#define mp(a, b) make_pair(a, b)
#define mt(args...) make_tuple(args)
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define die exit(0)

template<typename T>
using vc = vector<T>;
template<typename T>
using uset = unordered_set<T>;
template<typename A, typename B>
using umap = unordered_map<A, B>;
template<typename T, typename Comp>
using pq = std::priority_queue<T, vc<T>, Comp>;
template<typename T>
using maxpq = pq<T, less<T>>;
template<typename T>
using minpq = pq<T, greater<T>>;
template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using db = double;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vc<int>;
using vll = vc<ll>;
using vpi = vc<pi>;
using vpll = vc<pll>;
using str = string;

constexpr char el = '\n';
constexpr char sp = ' ';
constexpr int inf = 0x3f3f3f3f;
constexpr ll llinf = 0x3f3f3f3f3f3f3f3fLL;
// ---------------------------------------------------------------------


template <class T, int ...Ns> struct BIT {
    T val = 0; void upd(T v) { val += v; }
    T query() { return val; }
};
template <class T, int N, int... Ns> struct BIT<T, N, Ns...> {
    BIT<T,Ns...> bit[N+1];
    template<typename... Args> void upd(int pos, Args... args) { assert(pos > 0);
        for (; pos<=N; pos+=pos&-pos) bit[pos].upd(args...); }
    template<typename... Args> T sum(int r, Args... args) {
        T res=0; for (;r;r-=r&-r) res += bit[r].query(args...);
        return res; }
    template<typename... Args> T query(int l, int r, Args...
            args) { return sum(r,args...)-sum(l-1,args...); }
};

const int N = 1e5+5;
int b, n, d, m;
struct point {
    int x, y, z;
    bool operator<(point o)const{
        return mt(x, y, z) < mt(o.x, o.y, o.z);
    }
} a[N];

void one() {
    for(int i = 0; i < n; ++i)
        cin >> a[i].x;
    sort(a,a+n);
    ll ans = 0;
    for(int i = 0; i < n; ++i){
        int j = lb(a,a+n, point{a[i].x-d, -inf, -inf})-a;
        ans += i-j;
    }
    cout << ans;
}

void two(){
    for(int i = 0; i < n; ++i){
        cin >> a[i].x >> a[i].y;
        tie(a[i].x, a[i].y) = mp(a[i].x+a[i].y, a[i].y-a[i].x+75'000);
    }
    sort(a,a+n);

    BIT<int, 150'005> bit;
    ll ans = 0;
    int j = 0;
    for(int i = 0; i < n; ++i){
        while(a[i].x-a[j].x>d){
            bit.upd(a[j].y, -1);
            ++j;
        }
        ans += bit.query(max(1, a[i].y-d), min(150'004, a[i].y+d));
        bit.upd(a[i].y, 1);
    }
    cout << ans << el;
}

BIT<int, 250, 250, 250> bit2;
void three() {
    vc<array<int, 4>> a(n);
    for(int i = 0; i < n; ++i){
        int x, y, z;
        cin >> x >> y >> z;
        a[i] = {x+y+z, x+y-z+74, x-y+z+74, x-y-z+150};
    }
    sort(all(a));

#ifdef _DEBUG
    clog << "Line " << __LINE__ << ":\n";
    for(int i = 0; i < n; ++i)
        clog << a[i][0] << sp << a[i][1] << sp << a[i][2] << sp << a[i][3] << el;
    clog << el;
#endif

    ll ans = 0;
    int j = 0;
    for(int i = 0; i < n; ++i){
        while(a[i][0] - a[j][0] > d){
            bit2.upd(a[j][1], a[j][2], a[j][3], -1);
            ++j;
        }

        ans += bit2.query(max(1, a[i][1]-d), min(249, a[i][1]+d),
                          max(1, a[i][2]-d), min(249, a[i][2]+d),
                          max(1, a[i][3]-d), min(249, a[i][3]+d));
//                          max(1, a[i][2]-d), max(1, a[i][3]-d),
//                         min(249, a[i][1]+d), min(249, a[i][2]+d), min(249, a[i][3]+d));

        bit2.upd(a[i][1], a[i][2], a[i][3], 1);
//        for(int j = a[i].x; j > 0 && a[i].x-j <= d; --j){
//            int r = d - (a[i].x-j);
//            ans += bitg2[j].query(max(a[i].y-r, 1), max(a[i].z-r, 1),
//                    min(154, a[i].y+r), min(154, a[i].y+r));
//        }
//        bitg2[a[i].x].upd(a[i].y, a[i].z, 1);
    }

    cout << ans;
}

signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cout << fixed; clog << fixed; clog << unitbuf;
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#else
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
#endif

    cin >> b >> n >> d >> m;
    if(b==1)
        one();
    else if(b==2)
        two();
    else
        three();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 1864 KB Output is correct
2 Correct 22 ms 1876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 2232 KB Output is correct
2 Correct 19 ms 2232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 2276 KB Output is correct
2 Correct 24 ms 2280 KB Output is correct
3 Correct 20 ms 2380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 848 KB Output is correct
2 Correct 1 ms 848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 2640 KB Output is correct
2 Correct 24 ms 2640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 2768 KB Output is correct
2 Correct 28 ms 2724 KB Output is correct
3 Correct 29 ms 2716 KB Output is correct
4 Correct 33 ms 2760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 3216 KB Output is correct
2 Correct 29 ms 3220 KB Output is correct
3 Correct 29 ms 3196 KB Output is correct
4 Correct 34 ms 3152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 12752 KB Output is correct
2 Correct 6 ms 12752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 3324 KB Output is correct
2 Correct 92 ms 3256 KB Output is correct
3 Correct 97 ms 3272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 154 ms 19424 KB Output is correct
2 Correct 159 ms 19404 KB Output is correct
3 Correct 79 ms 19368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 212 ms 27612 KB Output is correct
2 Correct 200 ms 27600 KB Output is correct
3 Correct 97 ms 27564 KB Output is correct