Procon

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub K-Yoshizawa/Procon

:heavy_check_mark: verify/LC-StaticRangeSumWithUpperBound.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum_with_upper_bound"

#include "../Library/Template.hpp"
#include "../Library/DataStructure/MergeSortTree.hpp"

int main(){
    cin.tie(0)->sync_with_stdio(false);

    int N, Q; cin >> N >> Q;
    vector<ll> a(N); cin >> a;

    MergeSortTree<ll> mst(a, true);
    while(Q--){
        ll l, r, x; cin >> l >> r >> x;
        cout << mst.CountAtMost(l, r, x) << ' ' << mst.SumAtMost(l, r, x) << '\n';
    }
}
#line 1 "verify/LC-StaticRangeSumWithUpperBound.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum_with_upper_bound"

#line 2 "Library/Template.hpp"

#line 2 "Library/Common.hpp"

/**
 * @file Common.hpp
 */

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cstdint>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using ll = int64_t;
using ull = uint64_t;

constexpr const ll INF = (1LL << 62) - (3LL << 30) - 1;
#line 4 "Library/Template.hpp"

inline bool YnPrint(bool flag){cout << (flag ? "Yes" : "No") << '\n'; return flag;}
inline bool YNPrint(bool flag){cout << (flag ? "YES" : "NO") << '\n'; return flag;}
template<typename Container>
inline void Sort(Container &container){sort(container.begin(), container.end());}
template<typename Container>
inline void ReverseSort(Container &container){sort(container.rbegin(), container.rend());}
template<typename Container>
inline void Reverse(Container &container){reverse(container.begin(), container.end());}
template<typename Value>
inline int PopCount(const Value &value){return __builtin_popcount(value);}
template<typename Value>
inline Value Floor(Value numerator, Value denominator){if(denominator < 0) numerator *= -1, denominator *= -1; return numerator < 0 ? (numerator + 1) / denominator - 1 : numerator / denominator;}
template<typename Value>
inline Value Ceil(Value numerator, Value denominator){if(denominator < 0) numerator *= -1, denominator *= -1; return numerator > 0 ? (numerator - 1) / denominator + 1 : numerator / denominator;}
template<typename Value>
inline int LowerBoundIndex(const vector<Value> &container, const Value &value){return distance(container.begin(), lower_bound(container.begin(), container.end(), value));}
template<typename Value>
inline int UpperBoundIndex(const vector<Value> &container, const Value &value){return distance(container.begin(), upper_bound(container.begin(), container.end(), value));}
template<typename Value>
inline bool Between(const Value &lower, const Value &x, const Value &higher){return lower <= x && x <= higher;}
template<typename Value>
inline bool InGrid(const Value &y, const Value &x, const Value &ymax, const Value &xmax){return Between(0, y, ymax - 1) && Between(0, x, xmax - 1);}
template<typename Value>
inline Value Median(const Value &a, const Value &b, const Value &c){return Between(b, a, c) || Between(c, a, b) ? a : (Between(a, b, c) || Between(c, b, a) ? b : c);}
template<typename Value>
inline Value Except(Value &src, Value &cond, Value &excp){return (src == cond ? excp : src);}

template<class Value>
bool chmin(Value &src, const Value &cmp){if(src > cmp){src = cmp; return true;} return false;}
template<class Value>
bool chmax(Value &src, const Value &cmp){if(src < cmp){src = cmp; return true;} return false;}
template<typename Value>
inline Value min(vector<Value> &v){return *min_element((v).begin(), (v).end());}
template<typename Value>
inline Value max(vector<Value> &v){return *max_element((v).begin(), (v).end());}

const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, -1, 0, 1};
const int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy8[8] = {0, -1, -1, -1, 0, 1, 1, 1};

vector<pair<int, int>> adjacent(int current_y, int current_x, int max_y, int max_x, bool dir_8 = false){
    vector<pair<int, int>> ret;
    for(int d = 0; d < 4 * (1 + dir_8); ++d){
        int next_y = current_y + (dir_8 ? dy8[d] : dy4[d]);
        int next_x = current_x + (dir_8 ? dx8[d] : dx4[d]);
        if(InGrid(next_y, next_x, max_y, max_x)){
            ret.emplace_back(next_y, next_x);
        }
    }
    return ret;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p){
    os << p.first << " " << p.second;
    return os;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p){
    is >> p.first >> p.second;
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, vector<T> &v){
    for (int i = 0; i < v.size(); ++i){
        os << v[i] << (i + 1 != v.size() ? " " : "");
    }
    return os;
}

template <typename T>
ostream &operator<<(ostream &os, vector<vector<T>> &v){
    for (int i = 0; i < v.size(); ++i){
        os << v[i] << (i + 1 != v.size() ? "\n" : "");
    }
    return os;
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v){
    for (int i = 0; i < v.size(); ++i) is >> v[i];
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, set<T> &v){
    for (auto &u : v){
        os << u << " ";
    }
    return os;
}

template<typename T1, typename T2>
vector<pair<T1, T2>> AssembleVectorPair(vector<T1> &v1, vector<T2> &v2){
    assert(v1.size() == v2.size());
    vector<pair<T1, T2>> v;
    for(int i = 0; i < v1.size(); ++i) v.push_back({v1[i], v2[i]});
    return v;
}

template<typename T1, typename T2>
pair<vector<T1>, vector<T2>> DisassembleVectorPair(vector<pair<T1, T2>> &v){
    vector<T1> v1;
    vector<T2> v2;
    transform(v.begin(), v.end(), back_inserter(v1), [](auto p){return p.first;});
    transform(v.begin(), v.end(), back_inserter(v2), [](auto p){return p.second;});
    return {v1, v2};
}

template<typename T1, typename T2, typename T3>
tuple<vector<T1>, vector<T2>, vector<T3>> DisassembleVectorTuple(vector<tuple<T1, T2, T3>> &v){
    vector<T1> v1;
    vector<T2> v2;
    vector<T3> v3;
    transform(v.begin(), v.end(), back_inserter(v1), [](auto p){return get<0>(p);});
    transform(v.begin(), v.end(), back_inserter(v2), [](auto p){return get<1>(p);});
    transform(v.begin(), v.end(), back_inserter(v3), [](auto p){return get<2>(p);});
    return {v1, v2, v3};
}

template<typename T1 = int, typename T2 = T1>
pair<vector<T1>, vector<T2>> InputVectorPair(int size){
    vector<pair<T1, T2>> v(size);
    for(auto &[p, q] : v) cin >> p >> q;
    return DisassembleVectorPair(v);
}

template<typename T1 = int, typename T2 = T1, typename T3 = T1>
tuple<vector<T1>, vector<T2>, vector<T3>> InputVectorTuple(int size){
    vector<tuple<T1, T2, T3>> v(size);
    for(auto &[p, q, r] : v) cin >> p >> q >> r;
    return DisassembleVectorTuple(v);
}
#line 2 "Library/DataStructure/MergeSortTree.hpp"

template <typename DataType, typename WeightType = DataType>
class MergeSortTree{
    public:
    MergeSortTree(
        const vector<DataType> &A,
        const vector<WeightType> &B,
        bool zero_index = false
    ) : zero_index_(zero_index){
        Build(A, B);
    }
    
    MergeSortTree(
        const vector<DataType> &A,
        bool zero_index = false
    ) : zero_index_(zero_index){
        Build(A, A);
    }

    int CountAtMost(int l, int r, DataType x) const {
        if(l >= r) return 0;
        Validate(l + zero_index_);
        Validate(r + zero_index_ - 1);
        int lh = l + zero_index_ + offset_, rh = r + zero_index_ + offset_;
        int lcnt = 0, rcnt = 0;
        while(lh < rh){
            if(lh & 1){
                lcnt += distance(data_[lh].begin(), upper_bound(data_[lh].begin(), data_[lh].end(), x));
                ++lh;
            }
            if(rh & 1){
                --rh;
                rcnt += distance(data_[rh].begin(), upper_bound(data_[rh].begin(), data_[rh].end(), x));
            }
            lh >>= 1, rh >>= 1;
        }
        return lcnt + rcnt;
    }

    int CountAtLeast(int l, int r, DataType x) const {
        if(l >= r) return 0;
        return r - l - CountAtMost(l, r, x - 1);
    }

    int CountBetween(int l, int r, DataType p, DataType q) const {
        return CountAtMost(l, r, q) - CountAtMost(l, r, p - 1);
    }

    WeightType SumAtMost(int l, int r, DataType x) const {
        if(l >= r) return 0;
        Validate(l + zero_index_);
        Validate(r + zero_index_ - 1);
        int lh = l + zero_index_ + offset_, rh = r + zero_index_ + offset_;
        WeightType lval = 0, rval = 0;
        while(lh < rh){
            if(lh & 1){
                int idx = distance(data_[lh].begin(), upper_bound(data_[lh].begin(), data_[lh].end(), x));
                lval += prefix_sum_[lh][idx];
                ++lh;
            }
            if(rh & 1){
                --rh;
                int idx = distance(data_[rh].begin(), upper_bound(data_[rh].begin(), data_[rh].end(), x));
                rval += prefix_sum_[rh][idx];
            }
            lh >>= 1, rh >>= 1;
        }
        return lval + rval;
    }

    WeightType SumAtLeast(int l, int r, DataType x) const {
        if(l >= r) return 0;
        Validate(l + zero_index_);
        Validate(r + zero_index_ - 1);
        int lh = l + zero_index_ + offset_, rh = r + zero_index_ + offset_;
        WeightType lval = 0, rval = 0;
        while(lh < rh){
            if(lh & 1){
                int idx = distance(data_[lh].begin(), lower_bound(data_[lh].begin(), data_[lh].end(), x));
                lval += prefix_sum_[lh].back() - prefix_sum_[lh][idx];
                ++lh;
            }
            if(rh & 1){
                --rh;
                int idx = distance(data_[rh].begin(), lower_bound(data_[rh].begin(), data_[rh].end(), x));
                rval += prefix_sum_[rh].back() - prefix_sum_[rh][idx];
            }
            lh >>= 1, rh >>= 1;
        }
        return lval + rval;
    }

    WeightType SumBetween(int l, int r, DataType p, DataType q) const {
        return SumAtMost(l, r, q) - SumAtMost(l, r, p - 1);
    }
    
    private:
    int n_, offset_, zero_index_;
    vector<vector<DataType>> data_;
    vector<vector<WeightType>> weight_, prefix_sum_;

    void Build(const vector<DataType> &data, const vector<WeightType> &weight){
        n_ = 1;
        while(n_ < (int)data.size()) n_ <<= 1;
        offset_ = n_ - 1;
        data_.resize(2 * n_);
        weight_.resize(2 * n_);
        prefix_sum_.resize(2 * n_);
        for(int i = 0; i < (int)data.size(); ++i){
            data_[n_ + i].emplace_back(data[i]);
            weight_[n_ + i].emplace_back(weight[i]);
            prefix_sum_[n_ + i].emplace_back(0);
            prefix_sum_[n_ + i].emplace_back(weight[i]);
        }
        for(int i = offset_; i >= 1; --i){
            int l = i * 2 + 0, r = i * 2 + 1, li = 0, ri = 0, j = 0;
            int ls = (int)data_[l].size(), rs = (int)data_[r].size();
            data_[i].resize(ls + rs);
            weight_[i].resize(ls + rs);
            prefix_sum_[i].resize(ls + rs + 1);
            while(li < ls || ri < rs){
                if(ri == rs || li != ls && data_[l][li] < data_[r][ri]){
                    data_[i][j] = data_[l][li];
                    weight_[i][j] = weight_[l][li];
                    prefix_sum_[i][j + 1] = prefix_sum_[i][j] + weight_[i][j];
                    ++j, ++li;
                }
                else{
                    data_[i][j] = data_[r][ri];
                    weight_[i][j] = weight_[r][ri];
                    prefix_sum_[i][j + 1] = prefix_sum_[i][j] + weight_[i][j];
                    ++j, ++ri;
                }
            }
        }
    }
    
    inline void Validate(int x) const {
        assert(1 <= x && x <= n_);
    }
};
#line 5 "verify/LC-StaticRangeSumWithUpperBound.test.cpp"

int main(){
    cin.tie(0)->sync_with_stdio(false);

    int N, Q; cin >> N >> Q;
    vector<ll> a(N); cin >> a;

    MergeSortTree<ll> mst(a, true);
    while(Q--){
        ll l, r, x; cin >> l >> r >> x;
        cout << mst.CountAtMost(l, r, x) << ' ' << mst.SumAtMost(l, r, x) << '\n';
    }
}
Back to top page