結果

問題 No.2849 Birthday Donuts
ユーザー torisasami4torisasami4
提出日時 2024-07-16 23:57:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 8,970 bytes
コンパイル時間 3,194 ms
コンパイル使用メモリ 229,036 KB
実行使用メモリ 814,656 KB
最終ジャッジ日時 2024-07-16 23:57:27
合計ジャッジ時間 14,172 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG
// #pragma GCC optimize("O2,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define per(i, n) for (int i = (n)-1; 0 <= i; i--)
#define rep2(i, l, r) for (int i = (l); i < int(r); i++)
#define per2(i, l, r) for (int i = (r)-1; int(l) <= i; i--)
#define each(e, v) for (auto &e : v)
#define MM << " " <<
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)x.size()
template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}
template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}
template <class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using maxheap = std::priority_queue<T>;
template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}
template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}
template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

// __int128_t gcd(__int128_t a, __int128_t b) {
//     if (a == 0)
//         return b;
//     if (b == 0)
//         return a;
//     __int128_t cnt = a % b;
//     while (cnt != 0) {
//         a = b;
//         b = cnt;
//         cnt = a % b;
//     }
//     return b;
// }

struct Union_Find_Tree {
    vector<int> data;
    const int n;
    int cnt;

    Union_Find_Tree(int n) : data(n, -1), n(n), cnt(n) {}

    int root(int x) {
        if (data[x] < 0) return x;
        return data[x] = root(data[x]);
    }

    int operator[](int i) { return root(i); }

    bool unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return false;
        // if (data[x] > data[y]) swap(x, y);
        data[x] += data[y], data[y] = x;
        cnt--;
        return true;
    }

    int size(int x) { return -data[root(x)]; }

    int count() { return cnt; };

    bool same(int x, int y) { return root(x) == root(y); }

    void clear() {
        cnt = n;
        fill(begin(data), end(data), -1);
    }
};

template <int mod>
struct Mod_Int {
    int x;

    Mod_Int() : x(0) {}

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    static int get_mod() { return mod; }

    Mod_Int &operator+=(const Mod_Int &p) {
        if ((x += p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator-=(const Mod_Int &p) {
        if ((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator*=(const Mod_Int &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator/=(const Mod_Int &p) {
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator++() { return *this += Mod_Int(1); }

    Mod_Int operator++(int) {
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator--() { return *this -= Mod_Int(1); }

    Mod_Int operator--(int) {
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator-() const { return Mod_Int(-x); }

    Mod_Int operator+(const Mod_Int &p) const { return Mod_Int(*this) += p; }

    Mod_Int operator-(const Mod_Int &p) const { return Mod_Int(*this) -= p; }

    Mod_Int operator*(const Mod_Int &p) const { return Mod_Int(*this) *= p; }

    Mod_Int operator/(const Mod_Int &p) const { return Mod_Int(*this) /= p; }

    bool operator==(const Mod_Int &p) const { return x == p.x; }

    bool operator!=(const Mod_Int &p) const { return x != p.x; }

    Mod_Int inverse() const {
        assert(*this != Mod_Int(0));
        return pow(mod - 2);
    }

    Mod_Int pow(long long k) const {
        Mod_Int now = *this, ret = 1;
        for (; k > 0; k >>= 1, now *= now) {
            if (k & 1) ret *= now;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Mod_Int &p) { return os << p.x; }

    friend istream &operator>>(istream &is, Mod_Int &p) {
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

ll mpow(ll x, ll n, ll mod) {
    ll ans = 1;
    x %= mod;
    while (n != 0) {
        if (n & 1) ans = ans * x % mod;
        x = x * x % mod;
        n = n >> 1;
    }
    ans %= mod;
    return ans;
}

template <typename T>
T modinv(T a, const T &m) {
    T b = m, u = 1, v = 0;
    while (b > 0) {
        T t = a / b;
        swap(a -= t * b, b);
        swap(u -= t * v, v);
    }
    return u >= 0 ? u % m : (m - (-u) % m) % m;
}

ll divide_int(ll a, ll b) {
    if (b < 0) a = -a, b = -b;
    return (a >= 0 ? a / b : (a - b + 1) / b);
}

// const int MOD = 1000000007;
const int MOD = 998244353;
using mint = Mod_Int<MOD>;
const int inf = 1e9;

// ----- library -------
template <typename T>
T Euler_totient(T m) {
    T ret = m;
    for (T i = 2; i * i <= m; i++) {
        if (m % i == 0) ret /= i, ret *= i - 1;
        while (m % i == 0) m /= i;
    }
    if (m > 1) ret /= m, ret *= m - 1;
    return ret;
}

template <typename Monoid, bool del = false>
struct Persistent_Segment_Tree {
    using M = typename Monoid::V;

    struct Node {
        Node *lch, *rch;
        M x;

        Node(Node *lch, Node *rch, const M &x) : lch(lch), rch(rch), x(x) {}
    };

    int n;
    unordered_map<int, Node *> root;
    vector<Node *> used;

    Persistent_Segment_Tree(const vector<M> &v, int init_id = 0) { resize(v, init_id); }

    Persistent_Segment_Tree(int n, const M &x, int init_id = 0) { resize(n, x, init_id); }

    ~Persistent_Segment_Tree() {
        for (int i = 0; i < (int)used.size(); i++) delete used[i];
    }

    Node *make_node(Node *lch, Node *rch, const M &x) {
        Node *ret = new Node(lch, rch, x);
        if (del) used.push_back(ret);
        return ret;
    }

    Node *make_node(const M &x) { return make_node(NULL, NULL, x); }

    void resize(const vector<M> &v, int init_id = 0) {
        n = v.size();
        root[init_id] = build(v, 0, n);
    }

    void resize(int n, const M &x, int init_id = 0) { resize(vector<M>(n, x), init_id); }

    Node *merge(Node *lch, Node *rch) { return make_node(lch, rch, Monoid::merge(lch->x, rch->x)); }

    Node *build(const vector<M> &v, int l, int r) {
        if (r - l == 1) return make_node(v[l]);
        int m = (l + r) >> 1;
        return merge(build(v, l, m), build(v, m, r));
    }

    void copy(int ref_id, int new_id) {
        assert(root.count(ref_id));
        root[new_id] = root[ref_id];
    }

    Node *update(int i, const M &x, int l, int r, Node *pre) {
        if (r - l == 1) return make_node(x);
        int m = (l + r) >> 1;
        if (i < m) return merge(update(i, x, l, m, pre->lch), pre->rch);
        return merge(pre->lch, update(i, x, m, r, pre->rch));
    }

    // ref_id に対応するデータから派生して new_id に対応する新しいデータを作る
    void update(int ref_id, int new_id, int i, const M &x, bool apply = false) {
        assert(root.count(ref_id));
        root[new_id] = update(i, apply ? Monoid::merge(query(ref_id, i, i + 1), x) : x, 0, n, root[ref_id]);
    }

    M query(int a, int b, int l, int r, Node *now) const {
        if (a >= b || b <= l || r <= a) return Monoid::id;
        if (a <= l && r <= b) return now->x;
        int m = (l + r) >> 1;
        return Monoid::merge(query(a, b, l, m, now->lch), query(a, b, m, r, now->rch));
    }

    M query(int ref_id, int l, int r) {
        assert(root.count(ref_id));
        return query(l, r, 0, n, root[ref_id]);
    }
};

template <typename T>
struct Plus_Monoid {
    using V = T;
    static constexpr V merge(const V &a, const V &b) { return a + b; };
    static const V id;
};

template <typename T>
const T Plus_Monoid<T>::id = 0;
// ----- library -------

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(15);

    const int M = 2e5 + 10;
    vector<int> tos(M);
    rep2(i, 2, M) tos[i] = Euler_totient(i);
    Persistent_Segment_Tree<Plus_Monoid<ll>> seg(M, 0, M);
    per2(i, 2, M) {
        seg.copy(i + 1, i);
        for (int j = i; j <= M; j += i) seg.update(i, i, j, tos[i], true);
    }
    vector<ll> sum(M, 0);
    rep2(i, 2, M) sum[i] = sum[i - 1] + tos[i];
    ll prev = 0;
    int T;
    cin >> T;
    while (T--) {
        ll a, b;
        cin >> a >> b;
        ll l = a ^ prev, r = b ^ prev;
        r++;
        ll ans = sum[r - l] + seg.query(r - l + 1, l, r);
        cout << ans << '\n';
        prev = ans;
    }
}
0