結果

問題 No.1216 灯籠流し/Lanterns
ユーザー autumn-eelautumn-eel
提出日時 2020-08-02 16:11:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,681 ms / 4,500 ms
コード長 3,410 bytes
コンパイル時間 3,189 ms
コンパイル使用メモリ 189,940 KB
実行使用メモリ 424,788 KB
最終ジャッジ日時 2023-08-09 10:49:08
合計ジャッジ時間 34,207 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
21,160 KB
testcase_01 AC 8 ms
21,148 KB
testcase_02 AC 8 ms
21,104 KB
testcase_03 AC 8 ms
21,156 KB
testcase_04 AC 34 ms
23,656 KB
testcase_05 AC 585 ms
407,036 KB
testcase_06 AC 56 ms
64,636 KB
testcase_07 AC 421 ms
407,692 KB
testcase_08 AC 385 ms
383,048 KB
testcase_09 AC 200 ms
199,488 KB
testcase_10 AC 221 ms
218,684 KB
testcase_11 AC 108 ms
102,244 KB
testcase_12 AC 31 ms
23,708 KB
testcase_13 AC 126 ms
104,400 KB
testcase_14 AC 82 ms
56,072 KB
testcase_15 AC 151 ms
89,348 KB
testcase_16 AC 144 ms
68,148 KB
testcase_17 AC 687 ms
359,276 KB
testcase_18 AC 179 ms
104,788 KB
testcase_19 AC 1,002 ms
416,668 KB
testcase_20 AC 379 ms
201,704 KB
testcase_21 AC 233 ms
112,524 KB
testcase_22 AC 793 ms
364,004 KB
testcase_23 AC 44 ms
26,364 KB
testcase_24 AC 47 ms
58,224 KB
testcase_25 AC 30 ms
38,560 KB
testcase_26 AC 223 ms
230,476 KB
testcase_27 AC 82 ms
76,256 KB
testcase_28 AC 62 ms
44,652 KB
testcase_29 AC 123 ms
102,036 KB
testcase_30 AC 172 ms
162,636 KB
testcase_31 AC 302 ms
307,008 KB
testcase_32 AC 162 ms
140,616 KB
testcase_33 AC 55 ms
45,732 KB
testcase_34 AC 446 ms
422,596 KB
testcase_35 AC 1,653 ms
422,800 KB
testcase_36 AC 1,000 ms
424,788 KB
testcase_37 AC 117 ms
26,456 KB
testcase_38 AC 880 ms
421,556 KB
testcase_39 AC 1,681 ms
421,504 KB
testcase_40 AC 994 ms
424,284 KB
testcase_41 AC 963 ms
424,208 KB
testcase_42 AC 998 ms
424,092 KB
testcase_43 AC 1,005 ms
424,140 KB
testcase_44 AC 1,014 ms
424,480 KB
testcase_45 AC 804 ms
423,688 KB
testcase_46 AC 795 ms
424,120 KB
testcase_47 AC 794 ms
422,896 KB
testcase_48 AC 788 ms
422,912 KB
testcase_49 AC 809 ms
421,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll>P;

struct BIT2D{
	vector<vector<int>>bit;
	BIT2D(){}
	BIT2D(int n,int m){
		bit=vector<vector<int>>(n+10,vector<int>(m+10));
	}
	void add(int a,int b,int val){
		for(int x=a+1;x<bit.size();x+=x&-x){
			for(int y=b+1;y<bit[x].size();y+=y&-y){
				bit[x][y]+=val;
			}
		}
	}
	int sum(int a,int b){
		int res=0;
		for(int x=a+1;x;x-=x&-x){
			for(int y=b+1;y;y-=y&-y){
				res+=bit[x][y];
			}
		}
		return res;
	}
};

vector<P>E[60000];
ll d[60000];               //深さ

int vid[60000],rev[60000]; //Euler Tour上の頂点の順番
int cl[60000],cr[60000];   //部分木に対応する区間

int node_cnt;

void dfs(int v,int p){
	cl[v]=node_cnt;
	vid[v]=node_cnt++;
	rev[vid[v]]=v;
	for(auto u:E[v]){
		if(u.second==p)continue;
		d[u.second]=d[v]+u.first;
		dfs(u.second,v);
	}
	cr[v]=node_cnt;
}

int ty[200000],v[200000];
ll t[200000],l[200000];

struct st{
	ll v;	 //頂点のid
	P p;     //(出発時間、持続時間)
	int id;  //何番目に追加される?
	int X,Y; //bucketで使う情報
};

const int B=2000;

vector<st>vs; //全灯籠の列 (Euler Tour順序でソート)
int pos[200000];

struct Bucket{
	BIT2D bit;
	vector<ll>xs,ys;
	vector<int>idx;

	void pre_add(st&s){
		ll X=s.p.first+d[s.v],Y=d[s.v]-s.p.second;
		xs.push_back(X);
		ys.push_back(Y);
		idx.push_back(pos[s.id]);
	}
	
	void init(){
		sort(xs.begin(),xs.end());xs.erase(unique(xs.begin(),xs.end()),xs.end());
		sort(ys.begin(),ys.end());ys.erase(unique(ys.begin(),ys.end()),ys.end());
		for(int i:idx){
			vs[i].X=lower_bound(xs.begin(),xs.end(),vs[i].p.first+d[vs[i].v])-xs.begin();
			vs[i].Y=lower_bound(ys.begin(),ys.end(),d[vs[i].v]-vs[i].p.second)-ys.begin();
		}
		bit=BIT2D(xs.size(),ys.size());
	}

	void add(int id){
		bit.add(vs[id].X,vs[id].Y,1);
	}

	int query(ll x,ll y){
		x=upper_bound(xs.begin(),xs.end(),x)-xs.begin();
		y=upper_bound(ys.begin(),ys.end(),y)-ys.begin();
		return bit.sum(x-1,y-1);
	}
}bucket[100000];

int main(){
	int n,q;cin>>n>>q;
	assert(1<=n&&n<=50000);
	assert(1<=q&&q<=100000);
	rep(i,n-1){
		int a,b;ll c;scanf("%d%d%lld",&a,&b,&c);a--;b--;
		E[a].push_back(P(c,b));
		E[b].push_back(P(c,a));
	}
	dfs(0,-1);
	rep(i,q){
		scanf("%d%d%lld%lld",&ty[i],&v[i],&t[i],&l[i]);v[i]--;
		if(ty[i]==0)vs.push_back({v[i],P(t[i],l[i]),i});
	}
	sort(vs.begin(),vs.end(),[&](st a,st b){return vid[a.v]<vid[b.v];});
	rep(i,vs.size()){
		pos[vs[i].id]=i;
	}
	for(int i=0;i<vs.size();i+=B){
		for(int j=i;j<i+B&&j<vs.size();j++){
			bucket[i/B].pre_add(vs[j]);
		}
		bucket[i/B].init();
	}
	rep(i,n){
		int ok1=vs.size(),ng1=-1;
		while(ok1-ng1>1){
			int t=(ok1+ng1)/2;
			if(vid[vs[t].v]>=cl[i])ok1=t;
			else ng1=t;
		}
		int ok2=vs.size(),ng2=-1;
		while(ok2-ng2>1){
			int t=(ok2+ng2)/2;
			if(vid[vs[t].v]>=cr[i])ok2=t;
			else ng2=t;
		}
		cl[i]=ok1;
		cr[i]=ok2;
	}
	rep(i,q){
		if(ty[i]==0){
			bucket[pos[i]/B].add(pos[i]);
		}
		else{
			int L=cl[v[i]],R=cr[v[i]];
			int ans=0;
			ll X=d[v[i]]+t[i],Y=d[v[i]];
			while(L<R&&L%B!=0){
				if(vs[L].id<=i&&vs[L].p.first+d[vs[L].v]<=X
				&&d[vs[L].v]-vs[L].p.second<=Y)ans++;
				L++;
			}
			while(L<R&&R%B!=0){
				R--;
				if(vs[R].id<=i&&vs[R].p.first+d[vs[R].v]<=X&&
				d[vs[R].v]-vs[R].p.second<=Y)ans++;
			}
			for(int i=L;i<R;i+=B){
				ans+=bucket[i/B].query(X,Y);
			}
			printf("%d\n",ans);
		}
	}
}
0