Untitled

Anonymous
Plain Text
09/03/2025 3:53 PM
4.5 KB
17
Indexable
Never
#include <bits/stdc++.h>
 
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
 
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpii = vector<pii>;
 
const int mxn = 2e5;
const int mxd = 160;
 
vi dd(mxn + 1), ss(mxn + 1);
vector<vpii> b(mxd + 1);
vector<bool> check(mxd + 1);
 
template <typename S>
S op(const S &x, const S &y) {
    S ret;
    ret.reserve(x.size() + y.size());
    merge(x.begin(), x.end(), y.begin(), y.end(), back_inserter(ret));
    return ret;
}
 
template <typename S>
S e() {
    return S();
}
 
template <class S, S (*op)(const S &, const S &), S (*e)()>
struct mstree {
    int n, size, log;
    vector<S> d;
    vector<int> ys; // Stores the y-values (positions)
 
    mstree() : mstree(0) {}
    explicit mstree(const vector<int> &positions, const vector<S> &v) : n(int(v.size())), ys(positions) {
        log = 0;
        while ((1 << log) < n) log++;
        size = 1 << log;
        d = vector<S>(2 * size, e());
        for (int i = 0; i < n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) update(i);
    }
 
    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
 
    int prod(int l, int r, int lo, int hi) const {
        int ret = 0;
        l += size;
        r += size;
 
        while (l < r) {
            if (l & 1) ret += count(d[l++], lo, hi);
            if (r & 1) ret += count(d[--r], lo, hi);
            l >>= 1;
            r >>= 1;
        }
        return ret;
    }
 
    int count(const S &v, int lo, int hi) const {
        return upper_bound(v.begin(), v.end(), hi) -
               lower_bound(v.begin(), v.end(), lo);
    }
 
    // Find the range [l, r) in ys where y-values are within [yl, yh]
    pair<int, int> get_index_range(int yl, int yh) const {
        int l = lower_bound(ys.begin(), ys.end(), yl) - ys.begin();
        int r = upper_bound(ys.begin(), ys.end(), yh) - ys.begin();
        return {l, r};
    }
};
 
mstree<vi, op, e> *tree[mxd + 1];
 
void solve() {
    int n, m;
    cin >> n >> m;
 
    vi ans(m, 0);
    for (int i = 0; i < m; i++) {
        int x, A, B, C;
        cin >> x >> A >> B >> C;
 
        int l1 = max(1, x - A);
        int r1 = min(n, x + A);
        int l2 = max(1, dd[x] - B);
        int r2 = min(mxd, dd[x] + B);
        int l3 = max(1, ss[x] - C);
        int r3 = ss[x] + C;
 
        for (int d = l2; d <= r2; d++) {
            if (check[d]) {
                if (l1 <= r1 && l3 <= r3) {
                    auto [l_idx, r_idx] = tree[d]->get_index_range(l1, r1);
                    if (l_idx < r_idx) {
                        ans[i] += tree[d]->prod(l_idx, r_idx, l3, r3);
                    }
                }
            } else {
                for (const auto &[y, s] : b[d]) {
                    if (l1 <= y && y <= r1 && l3 <= s && s <= r3) {
                        ans[i]++;
                    }
                }
            }
        }
    }
 
    for (const auto &x : ans) {
        cout << x << " ";
    }
    cout << "\n";
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    // Precompute dd and ss
    for (int i = 1; i <= mxn; i++) {
        for (int j = i; j <= mxn; j += i) {
            dd[j]++;
            ss[j] += i;
        }
    }
 
    // Build data structures
    for (int i = 1; i <= mxn; i++) {
        int d = dd[i];
        int s = ss[i];
        b[d].emplace_back(i, s);
    }
 
    for (int d = 1; d <= mxd; d++) {
        if (b[d].size() < 700) continue;
        check[d] = true;
 
        // Collect unique y-values and their corresponding s-values
        vector<int> ys;
        vector<vi> s_list;
        for (const auto &[y, s] : b[d]) {
            ys.push_back(y);
        }
 
        // Sort ys and s-values together
        vector<pii> ys_s_pairs = b[d];
        sort(ys_s_pairs.begin(), ys_s_pairs.end());
 
        ys.clear();
        s_list.clear();
        for (size_t i = 0; i < ys_s_pairs.size(); i++) {
            ys.push_back(ys_s_pairs[i].first);
            s_list.emplace_back(1, ys_s_pairs[i].second);
        }
 
        tree[d] = new mstree<vi, op, e>(ys, s_list);
    }
 
    int tc;
    cin >> tc;
    while (tc--) {
        solve();
    }
 
    // // Clean up dynamically allocated trees
    // for (int d = 1; d <= mxd; d++) {
    //     if (check[d]) {
    //         delete tree[d];
    //     }
    // }
 
    return 0;
}
Editor is loading...
Leave a Comment