結果

問題 No.1216 灯籠流し/Lanterns
ユーザー mtsdmtsd
提出日時 2020-09-02 00:43:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 786 ms / 4,500 ms
コード長 17,135 bytes
コンパイル時間 2,217 ms
コンパイル使用メモリ 145,056 KB
実行使用メモリ 123,948 KB
最終ジャッジ日時 2024-11-21 01:26:45
合計ジャッジ時間 23,411 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,580 KB
testcase_01 AC 3 ms
9,456 KB
testcase_02 AC 3 ms
7,540 KB
testcase_03 AC 3 ms
7,544 KB
testcase_04 AC 30 ms
13,676 KB
testcase_05 AC 750 ms
104,428 KB
testcase_06 AC 52 ms
16,616 KB
testcase_07 AC 680 ms
97,212 KB
testcase_08 AC 517 ms
82,684 KB
testcase_09 AC 271 ms
48,604 KB
testcase_10 AC 281 ms
48,520 KB
testcase_11 AC 132 ms
28,980 KB
testcase_12 AC 27 ms
12,648 KB
testcase_13 AC 154 ms
33,480 KB
testcase_14 AC 56 ms
19,772 KB
testcase_15 AC 91 ms
23,808 KB
testcase_16 AC 94 ms
25,240 KB
testcase_17 AC 296 ms
43,252 KB
testcase_18 AC 100 ms
24,464 KB
testcase_19 AC 449 ms
64,056 KB
testcase_20 AC 213 ms
39,612 KB
testcase_21 AC 119 ms
27,112 KB
testcase_22 AC 335 ms
49,352 KB
testcase_23 AC 43 ms
17,784 KB
testcase_24 AC 58 ms
17,284 KB
testcase_25 AC 30 ms
13,108 KB
testcase_26 AC 344 ms
58,256 KB
testcase_27 AC 113 ms
27,704 KB
testcase_28 AC 70 ms
20,720 KB
testcase_29 AC 185 ms
37,100 KB
testcase_30 AC 297 ms
52,404 KB
testcase_31 AC 514 ms
81,812 KB
testcase_32 AC 271 ms
48,736 KB
testcase_33 AC 62 ms
18,836 KB
testcase_34 AC 786 ms
108,984 KB
testcase_35 AC 755 ms
108,368 KB
testcase_36 AC 279 ms
59,204 KB
testcase_37 AC 210 ms
26,484 KB
testcase_38 AC 684 ms
123,948 KB
testcase_39 AC 644 ms
108,864 KB
testcase_40 AC 660 ms
111,084 KB
testcase_41 AC 655 ms
111,280 KB
testcase_42 AC 652 ms
111,808 KB
testcase_43 AC 653 ms
111,120 KB
testcase_44 AC 655 ms
111,204 KB
testcase_45 AC 614 ms
79,308 KB
testcase_46 AC 640 ms
79,972 KB
testcase_47 AC 616 ms
80,100 KB
testcase_48 AC 675 ms
80,180 KB
testcase_49 AC 639 ms
80,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}

template<class _Key, class _Tp, class _Hash, bool DOWNSIZE> class UnorderedMapIterator;

template<class _Key, class _Tp, class _Hash = hash<_Key>, bool DOWNSIZE = false>
class UnorderedMap
{
private:
    using iterator = UnorderedMapIterator<_Key, _Tp, _Hash, DOWNSIZE>;
    using value_type = _Tp;
    using data_type = pair<_Key, _Tp>;
    using aligned_pointer = typename aligned_storage<sizeof(value_type), alignof(value_type)>::type;
    friend UnorderedMapIterator<_Key, _Tp, _Hash, DOWNSIZE>;
    struct bucket {
        _Key _key;
        short int _dist;
        bool _last, _end;
        aligned_pointer _value_ptr;
        bucket() noexcept : _dist(-1), _last(false), _end(false){}
        bucket& operator=(const bucket& another) noexcept {
            _key = another._key, _dist = another._dist, _last = another._last, _end = another._end;
            if(!another.empty()){
                new(&_value_ptr) value_type(*reinterpret_cast<const value_type*>(&another._value_ptr));
            }
            return *this;
        }
        ~bucket(){ if(!empty()) _delete(); }
        inline void clear() noexcept { _dist = -1; }
        inline void _delete(){ _dist = -1, value_ptr()->~value_type(); }
        inline bool empty() const noexcept { return (_dist == -1); }
        inline value_type& value() noexcept {
            return *reinterpret_cast<value_type*>(&_value_ptr);
        }
        inline value_type* value_ptr() noexcept {
            return reinterpret_cast<value_type*>(&_value_ptr);
        }
        inline void new_value(value_type&& value){
            new(&_value_ptr) value_type(move(value));
        }
    };
    inline static unsigned int ceilpow2(unsigned int u) noexcept {
        if(u == 0u) return 0u;
        --u, u |= u >> 1, u |= u >> 2, u |= u >> 4, u |= u >> 8;
        return (u | (u >> 16)) + 1u;
    }
    inline static bucket *increment(bucket *cur) noexcept {
        for(++cur; !cur->_end; ++cur){
            if(!cur->empty()) break;
        }
        return cur;
    }
    inline bucket *next_bucket(bucket *cur) const noexcept {
        return cur->_last ? _buckets : cur + 1;
    }
    inline unsigned int make_hash(const _Key& key) const noexcept {
        return _Hash()(key);
    }
    inline float load_rate() const noexcept {
        return (float)_data_count / _bucket_count;
    }
    bucket *insert(bucket *cur, _Key&& key, short int dist, value_type&& value){
        bucket *ret = cur;
        bool flag = false;
        while(true){
            if(cur->empty()){
                cur->_key = move(key), cur->_dist = dist, cur->new_value(move(value));
                if(!flag) ret = cur, flag = true;
                break;
            }else if(dist > cur->_dist){
                swap(key, cur->_key), swap(dist, cur->_dist), swap(value, cur->value());
                if(!flag) ret = cur, flag = true;
            }
            ++dist;
            cur = next_bucket(cur);
        }
        return ret;
    }
    template<class Key>
    bucket *_find(Key&& key, bool push = false){
        unsigned int hash = make_hash(key);
        bucket *cur = _buckets + (hash & _mask);
        short int dist = 0;
        while(dist <= cur->_dist){
            if(key == cur->_key) return cur;
            ++dist, cur = next_bucket(cur);
        }
        if(!push) return _buckets + _bucket_count;
        ++_data_count;
        if(rehash_check()){
            cur = _buckets + (hash & _mask), dist = 0;
        }
        value_type new_value = value_type();
        _Key new_key = forward<Key>(key);
        return insert(cur, move(new_key), dist, move(new_value));
    }
    template<class Data>
    bucket *find_insert(Data&& data){
        const _Key& key = data.first;
        unsigned int hash = make_hash(key);
        bucket *cur = _buckets + (hash & _mask);
        short int dist = 0;
        while(dist <= cur->_dist){
            if(key == cur->_key) return cur;
            ++dist, cur = next_bucket(cur);
        }
        ++_data_count;
        if(rehash_check()){
            cur = _buckets + (hash & _mask), dist = 0;
        }
        data_type new_data = forward<Data>(data);
        return insert(cur, move(new_data.first), dist, move(new_data.second));
    }
    template<typename... Args>
    bucket *emplace(Args&&... args){
        return find_insert(data_type(forward<Args>(args)...));
    }
    bucket *backward_shift(bucket *cur, bool next_ret){
        bucket *next = next_bucket(cur), *ret = cur;
        if(next->_dist < 1) return next_ret ? increment(cur) : cur;
        do {
            cur->_key = next->_key, cur->_dist = next->_dist - 1;
            cur->new_value(move(next->value()));
            cur = next, next = next_bucket(cur);
        }while(next->_dist >= 1);
        cur->clear();
        return ret;
    }
    bucket *erase_impl(bucket *cur, bool next_ret){
        assert(static_cast<size_t>(cur - _buckets) != _bucket_count);
        cur->_delete();
        --_data_count;
        return backward_shift(cur, next_ret);
    }
    bucket *erase_itr(bucket *cur, bool next_ret = true){
        const _Key key = cur->_key;
        return erase_impl(rehash_check() ? _find(key) : cur, next_ret);
    }
    size_t erase_key(const _Key& key){
        rehash_check();
        bucket *cur = _find(key);
        if(static_cast<size_t>(cur - _buckets) == _bucket_count){
            return 0;
        }else{
            erase_impl(_find(key), false);
            return 1;
        }
    }
    bool rehash_check(){
        if(_bucket_count == 0){
            rehash(1u);
            return true;
        }else if(load_rate() >= MAX_LOAD_RATE){
            rehash(_bucket_count * 2u);
            return true;
        }else if(DOWNSIZE){
            if(load_rate() <= MIN_LOAD_RATE && _bucket_count >= DOWNSIZE_THRESHOLD){
                rehash(_bucket_count / 2u);
                return true;
            }
        }
        return false;
    }
    void move_data(bucket *cur){
        insert(_buckets + (make_hash(cur->_key) & _mask), move(cur->_key), 0, move(cur->value()));
    }
    void rehash(unsigned int new_bucket_count){
        UnorderedMap new_unordered_map(new_bucket_count);
        new_unordered_map._data_count = _data_count;
        for(bucket *cur = _buckets; !cur->_end; ++cur){
            if(!cur->empty()){
                new_unordered_map.move_data(cur);
            }
        }
        swap(*this, new_unordered_map);
    }
    friend void swap(UnorderedMap& ump1, UnorderedMap& ump2){
        swap(ump1._bucket_count, ump2._bucket_count);
        swap(ump1._mask, ump2._mask);
        swap(ump1._data_count, ump2._data_count);
        swap(ump1._buckets, ump2._buckets);
    }

private:
    unsigned int _bucket_count, _mask, _data_count;
    bucket *_buckets;
public:
    const float MAX_LOAD_RATE = 0.5f;
    const float MIN_LOAD_RATE = 0.1f;
    const unsigned int DOWNSIZE_THRESHOLD = 16u;
    UnorderedMap(unsigned int bucket_size = 0u)
     : _bucket_count(ceilpow2(bucket_size)), _mask(_bucket_count - 1),
        _data_count(0u), _buckets(new bucket[_bucket_count + 1]){
        if(_bucket_count > 0) _buckets[_bucket_count - 1]._last = true;
        else _mask = 0;
        _buckets[_bucket_count]._end = true;
    }
    UnorderedMap(const UnorderedMap& another)
        : _bucket_count(another._bucket_count), _mask(another._mask), _data_count(another._data_count){
        _buckets = new bucket[_bucket_count + 1u];
        for(unsigned int i = 0u; i <= _bucket_count; ++i){
            _buckets[i] = another._buckets[i];
        }
    }
    UnorderedMap(UnorderedMap&& another)
        : _bucket_count(move(another._bucket_count)), _mask(move(another._mask)),
            _data_count(move(another._data_count)), _buckets(another._buckets){
        another._buckets = nullptr;
    }
    UnorderedMap& operator=(const UnorderedMap& another){
        delete[] _buckets;
        _bucket_count = another._bucket_count;
        _mask = another._mask;
        _data_count = another._data_count;
        _buckets = new bucket[_bucket_count + 1u];
        for(unsigned int i = 0u; i <= _bucket_count; ++i){
            _buckets[i] = another._buckets[i];
        }
        return *this;
    }
    UnorderedMap& operator=(UnorderedMap&& another){
        delete[] _buckets;
        _bucket_count = move(another._bucket_count);
        _mask = move(another._mask);
        _data_count = move(another._data_count);
        _buckets = another._buckets;
        another._buckets = nullptr;
        return *this;
    }
    void allocate(unsigned int element_size){
        rehash(ceilpow2(ceil(element_size / MAX_LOAD_RATE) + 1));
    }
    ~UnorderedMap(){ delete[] _buckets; }
    friend ostream& operator<< (ostream& os, UnorderedMap& ump) noexcept {
        for(auto val : ump) os << '{' << val.first << ',' << val.second << "} ";
        return os;
    }
    _Tp& operator[](const _Key& key){ return _find(key, true)->value(); }
    _Tp& operator[](_Key&& key){ return _find(move(key), true)->value(); }
    const _Tp& at(const _Key& key){
        bucket *res = _find(key);
        if(res == _buckets + _bucket_count) __throw_out_of_range("Unordered_Map::at");
        return res->value();
    }
    void clear(){
        UnorderedMap new_unordered_map(0u);
        swap(*this, new_unordered_map);
    }
    size_t size() const noexcept { return _data_count; }
    size_t bucket_count() const noexcept { return _bucket_count; }
    bool empty() const noexcept { return (_data_count == 0); }
    iterator begin() noexcept {
        return (_buckets->empty() && _bucket_count > 0) ? iterator(increment(_buckets)) : iterator(_buckets);
    }
    iterator end() noexcept { return iterator(_buckets + _bucket_count); }
    iterator find(const _Key& key){ return iterator(_find(key)); }
    iterator insert(const data_type& data){ return iterator(find_insert(data)); }
    iterator insert(data_type&& data){ return iterator(find_insert(move(data))); }
    template<typename... Args>
    iterator emplace(Args&&... args){ return iterator(_emplace(forward<Args>(args)...)); }
    size_t erase(const _Key& key){ return erase_key(key); }
    iterator erase(const iterator& itr){ return iterator(erase_itr(itr.bucket_ptr)); }
    void simple_erase(const _Key& key){ erase_key(key); }
    void simple_erase(const iterator& itr){ erase_itr(itr.bucket_ptr, false); }

    // DEBUG 用
    short int maximum_distance() const noexcept {
        short int ret = -1;
        for(bucket *cur = _buckets; !cur->_end; ++cur){
            ret = max(ret, cur->_dist);
        }
        return ret;
    }
};

template<class _Key, class _Tp, class _Hash, bool DOWNSIZE>
class UnorderedMapIterator {
private:
    friend UnorderedMap<_Key, _Tp, _Hash, DOWNSIZE>;
    typename UnorderedMap<_Key, _Tp, _Hash, DOWNSIZE>::bucket *bucket_ptr;
    using iterator_category = forward_iterator_tag;
    using value_type = pair<const _Key, _Tp>;
    using difference_type = ptrdiff_t;
    using reference = pair<const _Key&, _Tp&>;

private:
    UnorderedMapIterator(typename UnorderedMap<_Key, _Tp, _Hash, DOWNSIZE>::bucket *_bucket_ptr)
        noexcept : bucket_ptr(_bucket_ptr){}
public:
    UnorderedMapIterator() noexcept : bucket_ptr(){}
    UnorderedMapIterator(const UnorderedMapIterator& itr) noexcept : bucket_ptr(itr.bucket_ptr){}
    UnorderedMapIterator& operator=(const UnorderedMapIterator& itr)
        & noexcept { return bucket_ptr = itr.bucket_ptr, *this; }
    UnorderedMapIterator& operator=(const UnorderedMapIterator&& itr)
        & noexcept { return bucket_ptr = itr.bucket_ptr, *this; }
    reference operator*() const noexcept { return {bucket_ptr->_key, bucket_ptr->value()}; }
    UnorderedMapIterator& operator++() noexcept {
        return bucket_ptr = UnorderedMap<_Key, _Tp, _Hash, DOWNSIZE>::increment(bucket_ptr), *this;
    }
    UnorderedMapIterator operator++(int) const noexcept {
        return UnorderedMapIterator(UnorderedMap<_Key, _Tp, _Hash, DOWNSIZE>::increment(this->bucket_ptr));
    }
    bool operator==(const UnorderedMapIterator& itr) const noexcept { return !(*this != itr); };
    bool operator!=(const UnorderedMapIterator& itr) const noexcept { return bucket_ptr != itr.bucket_ptr; }
};


template<typename T> class BIT {
private:
    int n;ll m; vector<UnorderedMap<T,int> > bit;
public:
    void add(int i, ll j, T val){
        chmax(m,j+2);
        for(int i_ = i+1; i_ < n; i_ += i_ & -i_)
            for(ll j_ = j+1; j_ < m; j_ += j_ & -j_)
                bit[i_][j_] += val;
    }
    // [0,i]×[0,j]の範囲の和を求める
    T sum(int i, ll j){
        T s = 0;
        for(int i_ = i+1; i_ > 0; i_ -= i_ & -i_)
            for(ll j_ = j+1; j_ > 0; j_ -= j_ & -j_)
                if(bit[i_].find(j_)!=bit[i_].end())s += bit[i_][j_];
        return s;
    }
    // [lx, rx)×[ly, ry)の範囲の和を求める
    // T sum(int lx, int rx, ll ly, ll ry){
    //     return sum(rx-1, ry-1) - sum(lx-1, ry-1) - sum(rx-1, ly-1) + sum(lx-1, ly-1);   
    // }
    BIT(int sz1,int sz2){
        n = sz1 + 1;
        m = sz2 + 1;
        bit.resize(n);
    }
    
    void print(){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                cout<< sum(i-1, i, j-1, j) << " ";
            }
            cout << "\n";
        }
    }
    void print_sum(){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                cout<< sum(i-1, j-1) << " ";
            }
            cout << "\n";
        }
    }
};
 
vector<vector<pair<int,ll> > > g;

ll parent[17][50000];
ll dep[50000];
int in[50000];
int out[50000];
int te;
void init(int id,int pre){
    in[id] = te;
    te++;
    for(auto x:g[id]){
        if(x.first == pre)continue;
        dep[x.first] = dep[id] + x.second;
        parent[0][x.first] = id;
        init(x.first,id);
    }
    out[id] = te;
    te++;
}



int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,Q;
    cin >> n >> Q;
    g.resize(n);
    rep(i,n-1){
        int a,b;
        ll c;
        cin >> a >> b >> c;
        a--;b--;
        g[a].push_back({b,c});
        g[b].push_back({a,c});
    }
    init(0,-1);
    for(int i=1;i<17;i++){
        for(int j=0;j<n;j++){
            parent[i][j] = parent[i-1][parent[i-1][j]];
        }
    }
    vector<pair<pair<ll,ll>,int> > p;
    vector<ll> cand;
    cand.push_back(0);
    rep(tt,Q){
        int type,V;
        ll T,L;
        cin >> type >> V >> T >> L;
        V--;
        if(type==0){
            p.push_back({{T+dep[V],in[V]},1});cand.push_back(T+dep[V]);
            ll TT = T+dep[V];
            for(int i=16;i>=0;i--){
                int P = parent[i][V];
                if(dep[V]-dep[P] <= L){
                    L -= dep[V]-dep[P];
                    V = P;
                }
            }
            
            if(V!=0){
                p.push_back({{TT,in[parent[0][V]]},-1});
            }
        }else{
            p.push_back({{T+dep[V],V},0});
            cand.push_back(T+dep[V]);
        }
    }
    sort(all(cand));
    cand.erase(unique(all(cand)),cand.end());
    UnorderedMap<ll,int> mp;
    for(int i=0;i<(int)cand.size();i++){
        mp[cand[i]] = i;
    }
    
    BIT<int> bit((int)cand.size(),2*n+2);
    for(auto&x:p){
        if(x.second!=0){
            // cerr <<"ADD:" <<  x.first.first << " " <<  mp[x.first.first] << " " << x.first.second << " " << x.second << endl;
            bit.add(mp[x.first.first],x.first.second,x.second);
        }else{
            if(in[x.first.second]!=0){
                cout << bit.sum(mp[x.first.first],out[x.first.second]) - bit.sum(mp[x.first.first],in[x.first.second]-1)   << "\n";
                // cerr << "Q: " <<  mp[x.first.first] << " " << out[x.first.second] << " " << mp[x.first.first] << " " << in[x.first.second]-1 << endl;
            }else{
                cout << bit.sum(mp[x.first.first],out[x.first.second]) << "\n";    
                // cerr << "Q: " <<  mp[x.first.first] << " " << out[x.first.second] << endl;
            }
        }
    }
    return 0;
}
0