結果

問題 No.1216 灯籠流し/Lanterns
ユーザー autumn-eelautumn-eel
提出日時 2020-08-02 16:13:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 3,410 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 188,816 KB
実行使用メモリ 615,256 KB
最終ジャッジ日時 2023-09-25 20:14:01
合計ジャッジ時間 35,947 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
21,128 KB
testcase_01 AC 7 ms
21,152 KB
testcase_02 AC 8 ms
21,284 KB
testcase_03 AC 8 ms
21,152 KB
testcase_04 AC 34 ms
23,516 KB
testcase_05 MLE -
testcase_06 AC 72 ms
85,760 KB
testcase_07 MLE -
testcase_08 MLE -
testcase_09 AC 267 ms
283,152 KB
testcase_10 AC 294 ms
312,320 KB
testcase_11 AC 136 ms
135,092 KB
testcase_12 AC 30 ms
23,636 KB
testcase_13 AC 157 ms
139,772 KB
testcase_14 AC 107 ms
64,628 KB
testcase_15 AC 256 ms
123,736 KB
testcase_16 AC 182 ms
88,640 KB
testcase_17 MLE -
testcase_18 AC 272 ms
141,440 KB
testcase_19 MLE -
testcase_20 AC 607 ms
285,632 KB
testcase_21 AC 343 ms
155,364 KB
testcase_22 MLE -
testcase_23 AC 46 ms
26,284 KB
testcase_24 AC 63 ms
74,828 KB
testcase_25 AC 36 ms
46,296 KB
testcase_26 AC 308 ms
334,148 KB
testcase_27 AC 101 ms
99,624 KB
testcase_28 AC 72 ms
59,128 KB
testcase_29 AC 151 ms
133,896 KB
testcase_30 AC 233 ms
231,276 KB
testcase_31 AC 419 ms
446,584 KB
testcase_32 AC 216 ms
202,032 KB
testcase_33 AC 65 ms
58,908 KB
testcase_34 MLE -
testcase_35 MLE -
testcase_36 MLE -
testcase_37 AC 121 ms
26,712 KB
testcase_38 MLE -
testcase_39 MLE -
testcase_40 MLE -
testcase_41 MLE -
testcase_42 MLE -
testcase_43 MLE -
testcase_44 MLE -
testcase_45 MLE -
testcase_46 MLE -
testcase_47 MLE -
testcase_48 MLE -
testcase_49 MLE -
権限があれば一括ダウンロードができます

ソースコード

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=3000;

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