Submission #1012170

# Submission time Handle Problem Language Result Execution time Memory
1012170 2024-07-01T18:42:38 Z ttamx Truck Driver (IOI23_deliveries) C++17
100 / 100
1521 ms 46240 KB
#include "deliveries.h"
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N=1e5+5;
const int K=1<<18;

int n;
vector<pair<int,int>> adj[N];
int sz[N],hv[N],head[N],tail[N],par[N],tin[N],tout[N],ord[N];
ll w[N],dist[N];
int timer;
ll tot,ans;
ll val[N];
set<pair<ll,int>> ch[N];

struct Fenwick{
	ll t[N];
	void update(int i,ll v){
		if(i>0)for(;i<N;i+=i&-i)t[i]+=v;
	}
	ll query(int i){
		ll res=0;
		for(;i>0;i-=i&-i)res+=t[i];
		return res;
	}
	ll query(int l,int r){
		return query(r)-query(l-1);
	}
}f,fw;

struct SegTree{
	ll t[K],lz[K];
	void apply(int i,ll v){
		t[i]+=v;
		lz[i]+=v;
	}
	void push(int i){
		apply(i*2,lz[i]);
		apply(i*2+1,lz[i]);
		lz[i]=0;
	}
	void update(int l,int r,int i,int x,int y,ll v){
		if(y<l||r<x)return;
		if(x<=l&&r<=y)return apply(i,v);
		push(i);
		int m=(l+r)/2;
		update(l,m,i*2,x,y,v);
		update(m+1,r,i*2+1,x,y,v);
		t[i]=max(t[i*2],t[i*2+1]);
	}
	void update(int x,int y,ll v){
		update(1,n,1,x,y,v);
	}
	ll findlast(int l,int r,int i,int x,int y,ll v){
		if(y<l||r<x||t[i]<v)return 0;
		if(l==r)return l;
		push(i);
		int m=(l+r)/2;
		ll res=findlast(m+1,r,i*2+1,x,y,v);
		if(res==0)res=findlast(l,m,i*2,x,y,v);
		return res;
	}
	ll findlast(int x,int y,ll v){
		return findlast(1,n,1,x,y,v);
	}
}s;

void dfs(int u){
	sz[u]=1;
	for(auto [v,w]:adj[u])if(v!=par[u]){
		par[v]=u;
		dist[v]=dist[u]+w;
		dfs(v);
		sz[u]+=sz[v];
		if(sz[v]>sz[hv[u]])hv[u]=v;
	}
}

void hld(int u){
	tin[u]=++timer;
	ord[timer]=u;
	if(!head[u])head[u]=u;
	tail[head[u]]=u;
	if(hv[u])head[hv[u]]=head[u],hld(hv[u]);
	for(auto [v,w]:adj[u])if(v!=par[u]&&v!=hv[u])hld(v);
	tout[u]=timer;
}

void update(int u,ll v){
	tot+=v;
	ans+=v*dist[u];
	f.update(tin[u],v);
	fw.update(tin[u],v*dist[u]);
	while(u){
		s.update(tin[head[u]],tin[u],v);
		u=head[u];
		int p=par[u];
		fw.update(tin[p],v*dist[p]);
		ch[p].erase({val[u],u});
		val[u]+=v;
		ch[p].emplace(val[u],u);
		u=p;
	}
}

ll calc(){
	int u=1;
	ll half=(tot+1)/2;
	ll res=0;
	while(true){
		int x=u;
		u=ord[s.findlast(tin[u],tin[tail[u]],half)];
		if(u!=x)res+=fw.query(tin[x],tin[u]-1);
		if(ch[u].empty())break;
		auto [w,v]=*ch[u].rbegin();
		if(w<half)break;
		// cerr << x << " - " << u << "\n";
		res+=f.query(tin[u],tout[u])*dist[u];
		res-=f.query(tin[v],tout[v])*dist[u];
		u=v;
	}
	// for(int i=1;i<=n;i++)cerr << fw.query(tin[i],tin[i]) << " \n"[i==n];
	// cerr << u << " " << res << "\n";
	// cerr << ans << " " << dist[u] << " " << tot << " " << f.query(tin[u],tout[u]) << " " << res << "\n";
	return ans+dist[u]*(tot-2*f.query(tin[u],tout[u]))-2*res;
}

void init(int _n,vector<int> _u,vector<int> _v,vector<int> _t,vector<int> _w){
	n=_n;
	for(int i=0;i<n-1;i++){
		int u=_u[i]+1,v=_v[i]+1,w=_t[i];
		adj[u].emplace_back(v,w);
		adj[v].emplace_back(u,w);
	}
	for(int i=0;i<n;i++)w[i+1]=_w[i];
	w[1]++;
	dfs(1);
	hld(1);
	for(int i=1;i<=n;i++)update(i,w[i]);
	// for(int i=1;i<=n;i++){
	// 	cerr << i << " : ";
	// 	for(auto [w,v]:ch[i])cerr << "(" << w << "," << v << ") ";
	// 	cerr << "\n";
	// }
	// cerr << "-----\n";
}

ll max_time(int s,int x){
	s++;
	update(s,-w[s]);
	w[s]=x;
	if(s==1)w[s]++;
	update(s,w[s]);
	// for(int i=1;i<=n;i++){
	// 	cerr << i << " : ";
	// 	for(auto [w,v]:ch[i])cerr << "(" << w << "," << v << ") ";
	// 	cerr << "\n";
	// }
	// cerr << "-----\n";
	return calc()*2;
}
# Verdict Execution time Memory Grader output
1 Correct 103 ms 17476 KB Output is correct
2 Correct 100 ms 17372 KB Output is correct
3 Correct 107 ms 17492 KB Output is correct
4 Correct 103 ms 17364 KB Output is correct
5 Correct 106 ms 17488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7516 KB Output is correct
2 Correct 4 ms 7768 KB Output is correct
3 Correct 5 ms 7772 KB Output is correct
4 Correct 5 ms 7760 KB Output is correct
5 Correct 4 ms 7772 KB Output is correct
6 Correct 4 ms 7760 KB Output is correct
7 Correct 4 ms 7772 KB Output is correct
8 Correct 4 ms 7772 KB Output is correct
9 Correct 4 ms 7772 KB Output is correct
10 Correct 4 ms 7772 KB Output is correct
11 Correct 6 ms 7956 KB Output is correct
12 Correct 5 ms 7772 KB Output is correct
13 Correct 5 ms 7772 KB Output is correct
14 Correct 4 ms 7772 KB Output is correct
15 Correct 4 ms 7768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 17476 KB Output is correct
2 Correct 100 ms 17372 KB Output is correct
3 Correct 107 ms 17492 KB Output is correct
4 Correct 103 ms 17364 KB Output is correct
5 Correct 106 ms 17488 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 9 ms 7884 KB Output is correct
8 Correct 106 ms 10868 KB Output is correct
9 Correct 1521 ms 39484 KB Output is correct
10 Correct 1503 ms 39472 KB Output is correct
11 Correct 1424 ms 39468 KB Output is correct
12 Correct 1224 ms 42024 KB Output is correct
13 Correct 1273 ms 42032 KB Output is correct
14 Correct 1240 ms 42036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 17476 KB Output is correct
2 Correct 100 ms 17372 KB Output is correct
3 Correct 107 ms 17492 KB Output is correct
4 Correct 103 ms 17364 KB Output is correct
5 Correct 106 ms 17488 KB Output is correct
6 Correct 3 ms 7512 KB Output is correct
7 Correct 5 ms 7772 KB Output is correct
8 Correct 28 ms 11160 KB Output is correct
9 Correct 265 ms 43064 KB Output is correct
10 Correct 274 ms 43100 KB Output is correct
11 Correct 269 ms 43052 KB Output is correct
12 Correct 286 ms 46048 KB Output is correct
13 Correct 260 ms 46240 KB Output is correct
14 Correct 238 ms 42664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 17476 KB Output is correct
2 Correct 100 ms 17372 KB Output is correct
3 Correct 107 ms 17492 KB Output is correct
4 Correct 103 ms 17364 KB Output is correct
5 Correct 106 ms 17488 KB Output is correct
6 Correct 4 ms 7516 KB Output is correct
7 Correct 4 ms 7768 KB Output is correct
8 Correct 5 ms 7772 KB Output is correct
9 Correct 5 ms 7760 KB Output is correct
10 Correct 4 ms 7772 KB Output is correct
11 Correct 4 ms 7760 KB Output is correct
12 Correct 4 ms 7772 KB Output is correct
13 Correct 4 ms 7772 KB Output is correct
14 Correct 4 ms 7772 KB Output is correct
15 Correct 4 ms 7772 KB Output is correct
16 Correct 6 ms 7956 KB Output is correct
17 Correct 5 ms 7772 KB Output is correct
18 Correct 5 ms 7772 KB Output is correct
19 Correct 4 ms 7772 KB Output is correct
20 Correct 4 ms 7768 KB Output is correct
21 Correct 3 ms 7516 KB Output is correct
22 Correct 9 ms 7884 KB Output is correct
23 Correct 106 ms 10868 KB Output is correct
24 Correct 1521 ms 39484 KB Output is correct
25 Correct 1503 ms 39472 KB Output is correct
26 Correct 1424 ms 39468 KB Output is correct
27 Correct 1224 ms 42024 KB Output is correct
28 Correct 1273 ms 42032 KB Output is correct
29 Correct 1240 ms 42036 KB Output is correct
30 Correct 3 ms 7512 KB Output is correct
31 Correct 5 ms 7772 KB Output is correct
32 Correct 28 ms 11160 KB Output is correct
33 Correct 265 ms 43064 KB Output is correct
34 Correct 274 ms 43100 KB Output is correct
35 Correct 269 ms 43052 KB Output is correct
36 Correct 286 ms 46048 KB Output is correct
37 Correct 260 ms 46240 KB Output is correct
38 Correct 238 ms 42664 KB Output is correct
39 Correct 3 ms 7512 KB Output is correct
40 Correct 6 ms 7772 KB Output is correct
41 Correct 34 ms 11236 KB Output is correct
42 Correct 456 ms 42448 KB Output is correct
43 Correct 406 ms 42796 KB Output is correct
44 Correct 368 ms 43052 KB Output is correct
45 Correct 361 ms 43252 KB Output is correct
46 Correct 308 ms 43308 KB Output is correct
47 Correct 377 ms 44848 KB Output is correct
48 Correct 380 ms 45104 KB Output is correct
49 Correct 315 ms 45084 KB Output is correct
50 Correct 307 ms 45368 KB Output is correct
51 Correct 302 ms 45356 KB Output is correct
52 Correct 998 ms 41908 KB Output is correct
53 Correct 1086 ms 41940 KB Output is correct
54 Correct 982 ms 42024 KB Output is correct
55 Correct 292 ms 41260 KB Output is correct
56 Correct 310 ms 43052 KB Output is correct
57 Correct 305 ms 43056 KB Output is correct