結果

問題 No.1216 灯籠流し/Lanterns
ユーザー autumn-eelautumn-eel
提出日時 2020-08-02 18:18:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,017 ms / 4,500 ms
コード長 3,400 bytes
コンパイル時間 5,216 ms
コンパイル使用メモリ 186,112 KB
実行使用メモリ 239,952 KB
最終ジャッジ日時 2023-08-09 10:55:47
合計ジャッジ時間 23,608 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
28,972 KB
testcase_01 AC 10 ms
28,992 KB
testcase_02 AC 10 ms
28,996 KB
testcase_03 AC 10 ms
28,932 KB
testcase_04 AC 35 ms
31,272 KB
testcase_05 AC 289 ms
229,920 KB
testcase_06 AC 38 ms
50,400 KB
testcase_07 AC 277 ms
228,908 KB
testcase_08 AC 243 ms
215,212 KB
testcase_09 AC 134 ms
122,608 KB
testcase_10 AC 138 ms
130,732 KB
testcase_11 AC 76 ms
68,152 KB
testcase_12 AC 31 ms
30,868 KB
testcase_13 AC 99 ms
73,480 KB
testcase_14 AC 61 ms
48,356 KB
testcase_15 AC 103 ms
65,212 KB
testcase_16 AC 98 ms
54,760 KB
testcase_17 AC 484 ms
202,280 KB
testcase_18 AC 118 ms
73,144 KB
testcase_19 AC 504 ms
234,716 KB
testcase_20 AC 222 ms
124,928 KB
testcase_21 AC 124 ms
78,416 KB
testcase_22 AC 388 ms
207,476 KB
testcase_23 AC 45 ms
33,916 KB
testcase_24 AC 36 ms
50,244 KB
testcase_25 AC 23 ms
37,484 KB
testcase_26 AC 144 ms
139,396 KB
testcase_27 AC 63 ms
59,648 KB
testcase_28 AC 55 ms
43,648 KB
testcase_29 AC 95 ms
69,280 KB
testcase_30 AC 123 ms
102,176 KB
testcase_31 AC 193 ms
177,352 KB
testcase_32 AC 116 ms
90,260 KB
testcase_33 AC 47 ms
43,052 KB
testcase_34 AC 289 ms
234,176 KB
testcase_35 AC 893 ms
236,568 KB
testcase_36 AC 955 ms
236,540 KB
testcase_37 AC 117 ms
32,772 KB
testcase_38 AC 517 ms
234,168 KB
testcase_39 AC 963 ms
234,256 KB
testcase_40 AC 1,000 ms
235,812 KB
testcase_41 AC 987 ms
235,796 KB
testcase_42 AC 1,017 ms
235,724 KB
testcase_43 AC 1,010 ms
235,684 KB
testcase_44 AC 764 ms
239,952 KB
testcase_45 AC 496 ms
235,720 KB
testcase_46 AC 490 ms
235,820 KB
testcase_47 AC 493 ms
235,980 KB
testcase_48 AC 493 ms
235,728 KB
testcase_49 AC 496 ms
235,684 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>>10);i++){
		for(int j=i<<10;j<(i<<10)+B;j++){
			bucket[i].pre_add(vs[j]);
		}
		bucket[i].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>>10);i<(R>>10);i++){
				ans+=bucket[i].query(X,Y);
			}
			printf("%d\n",ans);
		}
	}
}
0