結果

問題 No.1216 灯籠流し/Lanterns
ユーザー kaagekaage
提出日時 2020-08-01 15:51:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4,472 ms / 4,500 ms
コード長 7,081 bytes
コンパイル時間 2,144 ms
コンパイル使用メモリ 149,760 KB
実行使用メモリ 393,636 KB
最終ジャッジ日時 2023-08-09 04:54:37
合計ジャッジ時間 83,035 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,400 KB
testcase_01 AC 4 ms
10,108 KB
testcase_02 AC 4 ms
10,236 KB
testcase_03 AC 4 ms
10,312 KB
testcase_04 AC 51 ms
14,876 KB
testcase_05 AC 4,136 ms
30,568 KB
testcase_06 AC 128 ms
12,084 KB
testcase_07 AC 3,447 ms
27,116 KB
testcase_08 AC 2,432 ms
27,400 KB
testcase_09 AC 1,046 ms
17,700 KB
testcase_10 AC 1,054 ms
19,372 KB
testcase_11 AC 539 ms
14,852 KB
testcase_12 AC 80 ms
13,884 KB
testcase_13 AC 842 ms
17,004 KB
testcase_14 AC 189 ms
15,904 KB
testcase_15 AC 281 ms
16,164 KB
testcase_16 AC 453 ms
20,188 KB
testcase_17 AC 241 ms
16,168 KB
testcase_18 AC 259 ms
15,556 KB
testcase_19 AC 927 ms
21,764 KB
testcase_20 AC 546 ms
18,464 KB
testcase_21 AC 377 ms
17,336 KB
testcase_22 AC 409 ms
17,740 KB
testcase_23 AC 112 ms
19,544 KB
testcase_24 AC 51 ms
11,244 KB
testcase_25 AC 37 ms
11,176 KB
testcase_26 AC 211 ms
13,788 KB
testcase_27 AC 279 ms
14,020 KB
testcase_28 AC 260 ms
15,144 KB
testcase_29 AC 574 ms
16,192 KB
testcase_30 AC 521 ms
15,156 KB
testcase_31 AC 388 ms
15,248 KB
testcase_32 AC 779 ms
16,660 KB
testcase_33 AC 196 ms
13,848 KB
testcase_34 AC 4,412 ms
28,364 KB
testcase_35 AC 3,683 ms
393,636 KB
testcase_36 AC 3,249 ms
24,164 KB
testcase_37 AC 1,677 ms
20,424 KB
testcase_38 AC 912 ms
17,592 KB
testcase_39 AC 3,899 ms
20,152 KB
testcase_40 AC 4,377 ms
22,844 KB
testcase_41 AC 4,472 ms
22,776 KB
testcase_42 AC 4,407 ms
23,064 KB
testcase_43 AC 4,345 ms
22,928 KB
testcase_44 AC 4,273 ms
22,860 KB
testcase_45 AC 2,188 ms
24,196 KB
testcase_46 AC 2,145 ms
24,040 KB
testcase_47 AC 1,997 ms
23,984 KB
testcase_48 AC 1,837 ms
23,928 KB
testcase_49 AC 2,172 ms
23,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/Users/kaage/Desktop/ProgrammingWorkspace/library/other/template.hpp"
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i,n) for(int i=0;i<(lint)(n);i++)
#define REP(i,n) for(int i=1;i<=(lint)(n);i++)
#define all(V) V.begin(),V.end()
typedef long long lint;
typedef unsigned long long ulint;
typedef std::pair<int, int> P;
typedef std::pair<lint, lint> LP;
constexpr int INF = INT_MAX/2;
constexpr lint LINF = LLONG_MAX/2;
constexpr double eps = DBL_EPSILON;
constexpr double PI=3.141592653589793238462643383279;
template<class T>
class prique :public std::priority_queue<T, std::vector<T>, std::greater<T>> {};
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
	if (lhs < rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
	if (lhs > rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
inline lint gcd(lint a, lint b) {
	while (b) {
		lint c = a;
		a = b; b = c % b;
	}
	return a;
}
inline lint lcm(lint a, lint b) {
	return a / gcd(a, b) * b;
}
bool isprime(lint n) {
	if (n == 1)return false;
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0)return false;
	}
	return true;
}
template<typename T>
T mypow(T a, lint b) {
	if (!b)return T(1);
	if (b & 1)return mypow(a, b - 1) * a;
	T memo = mypow(a, b >> 1);
	return memo * memo;
}
lint modpow(lint a, lint b, lint m) {
	if (!b)return 1;
	if (b & 1)return modpow(a, b - 1, m) * a % m;
	lint memo = modpow(a, b >> 1, m);
	return memo * memo % m;
}
template<typename T>
void printArray(std::vector<T>& vec) {
	rep(i, vec.size() - 1)std::cout << vec[i] << " ";
	std::cout << vec.back() << std::endl;
}
template<typename T>
void printArray(T l, T r) {
	T rprev = r;
	rprev--;
	for (T i = l; i != rprev; i++) {
		std::cout << *i << " ";
	}
	std::cout << *rprev << std::endl;
}
#line 2 "main.cpp"
std::vector<lint> bit;
class BIT {
	int n;
public:
	BIT(unsigned int n) :n(n) {}
	void add(int a, lint x) {
		while (a <= n) {
			bit[a] += x;
			a += a & -a;
		}
	}
	lint query(int a) {
		lint cnt = 0;
		while (a > 0) {
			cnt += bit[a];
			a -= a & -a;
		}
		return cnt;
	}
	void clear() {
		bit.assign(n + 1, 0);
	}
	unsigned int lower_bound(int x){
		int p=0,k=1;
		while(k*2<=n)k*=2;
		while(k>0){
			if(p+k<=n&&bit[p+k]<x){
				x-=bit[p+k];
				p+=k;
			}
			k/=2;
		}
		return p+1;
	}
	unsigned int upper_bound(int x){
		int p=0,k=1;
		while(k*2<=n)k*=2;
		while(k>0){
			if(p+k<=n&&bit[p+k]<=x){
				x-=bit[p+k];
				p+=k;
			}
			k/=2;
		}
		return p+1;
	}
};
int N,Q;
int type[100010],v[100010];
lint t[100010],l[100010];
int ans[100010];
std::vector<std::pair<int,lint>> vec[50010],children[50010],comped[50010];
std::vector<std::pair<std::pair<lint,int>,std::pair<int,lint>>> queries[50010];
std::pair<int,lint> newpar[50010],repr[50010];
bool used[50010],use[50010];
std::vector<int> verset;
void dfs(int node){
    used[node]=true;
    for(const auto& i:vec[node]){
        if(!used[i.first]){
            children[node].emplace_back(i);
            dfs(i.first);
        }
    }
}
void compress(int node,int par,lint dist){
    used[node]=true;
    use[node]=std::binary_search(all(verset),node);
	newpar[node]={par,dist};
    if(use[node]){
        comped[par].emplace_back(node,dist);
        repr[node]={node,0};
    }
    else repr[node]=newpar[node];
    for(const auto& i:children[node]){
        if(!used[i.first]){
            if(use[node])compress(i.first,node,i.second);
            else compress(i.first,par,dist+i.second);
        }
    }
}
int main(){
    scanf("%d %d",&N,&Q);
    rep(i,N-1){
        int a,b;
        lint c;
        scanf("%d %d %lld",&a,&b,&c);
        vec[a].emplace_back(b,c);
        vec[b].emplace_back(a,c);
    }
    rep(i,Q)scanf("%d %d %lld %lld",type+i,v+i,t+i,l+i);
    dfs(1);
    std::vector<lint> cords;
    std::queue<std::pair<std::pair<int,lint>,int>> que;
    int B=sqrt(Q);
    bit.resize(2*Q);
    for(int i=0;i<Q;i+=B){
        verset.clear();
        verset.emplace_back(1);
        for(int j=i;j<std::min(Q,i+B);j++){
            if(type[j]==1)verset.emplace_back(v[j]);
        }
        std::sort(all(verset));
        memset(used,false,sizeof(used));
        memset(use,false,sizeof(use));
        REP(j,N){
            comped[j].clear();
            queries[j].clear();
        }
        compress(1,0,0);
        rep(j,i){
            if(type[j]==0&&l[j]-repr[v[j]].second>=0){
                queries[repr[v[j]].first].push_back({{t[j]+repr[v[j]].second,0},{0,l[j]-repr[v[j]].second}});
            }
        }
        for(int j=i;j<std::min(Q,i+B);j++){
            if(type[j]==1){
                que.push({{v[j],0},0});
                while(!que.empty()){
                    auto p=que.front();
					que.pop();
                    queries[p.first.first].push_back({{t[j]-p.first.second,1},{j,p.first.second}});
                    for(const auto& k:comped[p.first.first]){
                        if(k.first!=p.second&&p.first.second+k.second<=t[j]){
                            que.push({{k.first,p.first.second+k.second},p.first.first});
                        }
                    }
                }
            }
        }
        REP(j,N){
            if(use[j]){
            	std::sort(all(queries[j]));
            	cords.clear();
            	for(const auto& k:queries[j])cords.emplace_back(k.second.second);
            	std::sort(all(cords));
            	cords.erase(std::unique(all(cords)),cords.end());
            	for(auto& k:queries[j])k.second.second=std::lower_bound(all(cords),k.second.second)-cords.begin()+1;
                BIT bit(cords.size()+1);
            	for(const auto& k:queries[j]){
            	    if(k.first.second==0)bit.add(k.second.second,1);
            	    else {
						ans[k.second.first]+=bit.query(cords.size()+1);
						if(k.second.second!=1)ans[k.second.first]-=bit.query(k.second.second-1);
					}
				}
                for(const auto& k:queries[j]){
                    if(k.first.second==0)bit.add(k.second.second,-1);
                }
				queries[j].clear();
            }
        }
		for(int j=std::min(Q,i+B)-1;j>=i;j--){
			if(type[j]==0){
				int node=v[j];
                lint time=t[j];
				while(time-t[j]<=l[j]){
					for(const auto& k:queries[node]){
						if(time<=k.first.first)ans[k.first.second]++;
					}
					if(node==1)break;
					time+=newpar[node].second;
					node=newpar[node].first;
				}
			}
			else queries[v[j]].push_back({{t[j],j},{0,0}});
		}
    }
	rep(i,Q){
		if(type[i]==1)printf("%d\n",ans[i]);
	}
}
0