結果

問題 No.1216 灯籠流し/Lanterns
ユーザー autumn-eelautumn-eel
提出日時 2020-08-02 16:58:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,135 ms / 4,500 ms
コード長 3,404 bytes
コンパイル時間 2,359 ms
コンパイル使用メモリ 185,828 KB
実行使用メモリ 240,576 KB
最終ジャッジ日時 2023-08-09 10:54:11
合計ジャッジ時間 21,404 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
28,944 KB
testcase_01 AC 11 ms
28,964 KB
testcase_02 AC 10 ms
28,972 KB
testcase_03 AC 10 ms
28,952 KB
testcase_04 AC 39 ms
31,236 KB
testcase_05 AC 294 ms
230,428 KB
testcase_06 AC 41 ms
51,844 KB
testcase_07 AC 276 ms
230,820 KB
testcase_08 AC 244 ms
216,920 KB
testcase_09 AC 132 ms
123,020 KB
testcase_10 AC 141 ms
132,672 KB
testcase_11 AC 82 ms
72,092 KB
testcase_12 AC 34 ms
31,432 KB
testcase_13 AC 101 ms
74,080 KB
testcase_14 AC 65 ms
48,736 KB
testcase_15 AC 103 ms
66,564 KB
testcase_16 AC 102 ms
56,040 KB
testcase_17 AC 464 ms
205,128 KB
testcase_18 AC 114 ms
74,520 KB
testcase_19 AC 471 ms
235,956 KB
testcase_20 AC 211 ms
125,276 KB
testcase_21 AC 124 ms
78,956 KB
testcase_22 AC 376 ms
208,240 KB
testcase_23 AC 53 ms
34,100 KB
testcase_24 AC 39 ms
50,164 KB
testcase_25 AC 26 ms
38,804 KB
testcase_26 AC 146 ms
139,100 KB
testcase_27 AC 67 ms
60,180 KB
testcase_28 AC 58 ms
43,904 KB
testcase_29 AC 100 ms
71,524 KB
testcase_30 AC 123 ms
103,140 KB
testcase_31 AC 196 ms
177,824 KB
testcase_32 AC 124 ms
93,212 KB
testcase_33 AC 48 ms
43,608 KB
testcase_34 AC 304 ms
238,328 KB
testcase_35 AC 943 ms
238,960 KB
testcase_36 AC 1,034 ms
240,576 KB
testcase_37 AC 123 ms
32,636 KB
testcase_38 AC 500 ms
236,524 KB
testcase_39 AC 1,059 ms
236,524 KB
testcase_40 AC 1,119 ms
239,152 KB
testcase_41 AC 1,082 ms
239,192 KB
testcase_42 AC 1,099 ms
239,256 KB
testcase_43 AC 1,135 ms
239,868 KB
testcase_44 AC 793 ms
240,492 KB
testcase_45 AC 502 ms
238,684 KB
testcase_46 AC 501 ms
239,712 KB
testcase_47 AC 500 ms
238,528 KB
testcase_48 AC 507 ms
238,184 KB
testcase_49 AC 498 ms
238,168 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で使う情報
}vs[200000]; //全灯籠の列 (Euler Tour順序でソート)

int pos[200000];

const int B=1024;

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);
	int vcnt=0;
	rep(i,q){
		scanf("%d%d%lld%lld",&ty[i],&v[i],&t[i],&l[i]);v[i]--;
		if(ty[i]==0)vs[vcnt++]={v[i],P(t[i],l[i]),i};
	}
	sort(vs,vs+vcnt,[&](st a,st b){return vid[a.v]<vid[b.v];});
	rep(i,vcnt){
		pos[vs[i].id]=i;
	}
	for(int i=0;i<vcnt;i+=B){
		int id=i>>10;
		for(int j=i;j<i+B&&j<vcnt;j++){
			bucket[id].pre_add(vs[j]);
		}
		bucket[id].init();
	}
	rep(i,n){
		int ok1=vcnt,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=vcnt,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]>>10].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&1023)){
				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&1023)){
				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>>10].query(X,Y);
			}
			printf("%d\n",ans);
		}
	}
}
0