답안 #442112

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
442112 2021-07-07T07:17:05 Z PixelCat Construction of Highway (JOI18_construction) C++14
100 / 100
361 ms 26984 KB
/* code by the cute PixelCat owo */
/*   as cute as nyaaacho neko!   */
//#pragma GCC optimize("O4,unroll-loops,no-stack-protector")
#include <bits/stdc++.h>
#define int ll //__int128
#define double long double
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;

#define For(i,a,b)    for(int i=a;i<=b;i++)
#define Fors(i,a,b,s) for(int i=a;i<=b;i+=s)
#define Forr(i,a,b)   for(int i=a;i>=b;i--)
#define F first
#define S second
#define L(id) (id*2+1)
#define R(id) (id*2+2)
#define LO(x) (x&(-x))

#define eb emplace_back
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define mkp make_pair

#define MOD (int)(1000000007)
#define INF (int)(1e17)
#define EPS (1e-6)

#ifdef NYAOWO
#define debug(...) do{\
	cerr << __LINE__ <<\
	" : ("#__VA_ARGS__ << ") = ";\
	_OUT(__VA_ARGS__);\
	cerr << flush;\
}while(0)
template<typename T> void _OUT(T _X) { cerr << _X << "\n"; }
template<typename T,typename...I>
void _OUT(T _X,I ...tail) { cerr << _X << ", "; _OUT(tail...); }
inline void USACO(const string &s) { cerr<<"USACO: "<<s<<"\n"; }
#else
#define debug(...)
inline void USACO(const string &s){
	freopen((s+".in").c_str(),"r",stdin);
	freopen((s+".out").c_str(),"w",stdout);
}
#endif
inline void NYA(){ ios::sync_with_stdio(false); cin.tie(0); }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int gcd(int a,int b) { return __gcd(a,b); }
inline int lcm(int a,int b) { return a/gcd(a,b)*b; }
int fpow(int b,int p,const int &mod){
	int ans=1;
	while(p){
		if(p&1) ans=ans*b%mod;
		p/=2; b=b*b%mod;
	}
	return ans;
}
int fpow(int b,int p) { return fpow(b,p,MOD); }
template<typename T> inline void chmin(T &_a,const T &_b) { if(_b<_a) _a=_b; }
template<typename T> inline void chmax(T &_a,const T &_b) { if(_b>_a) _a=_b; }

struct BIT{
	int n;
	int a[100010];
	void init(int _n){
		n=_n;
		memset(a,0,sizeof(a));
	}
	void add(int i,int x){
		while(i<=n){
			a[i]+=x;
			i+=LO(i);
		}
	}
	int ask(int i){
		int ans=0;
		while(i>0){
			ans+=a[i];
			i-=LO(i);
		}
		return ans;
	}
}bit;

struct Node{
	vector<int> adj;
	int c;

	int par;
	int nxt;
	int size;
	int dep;

	int head;
	int chainid;
}g[100010];

struct Chain{
	int head,tail;
	int val;
}; vector<Chain> ch[100010];

int dfs(int u,int d){
	g[u].size=1;
	g[u].dep=d;
	int mx=0,id=-1;
	for(auto &i:g[u].adj){
		g[u].size+=dfs(i,d+1);
		if(g[i].size>mx){
			mx=g[i].size; id=i;
		}
	}
	g[u].nxt=id;
	return g[u].size;
}
void build(int u,int h){
	static int chid=0;
	g[u].head=h;
	g[u].chainid=chid;
	if(g[u].nxt!=-1)
		build(g[u].nxt,h);
	for(auto &i:g[u].adj) if(i!=g[u].nxt){
		chid++;
		build(i,i);
	}
}

int link(int u,int v){
	//cerr<<"Query "<<u<<" "<<v<<"\n";

	vector<pii> chid; //chain id,depth
	while(u!=0){
		chid.eb(g[u].chainid,g[u].dep);
		u=g[g[u].head].par;	
	}
	vector<pii> a; //value,count
	Forr(_i,sz(chid)-1,0){
		int &id=chid[_i].F;
		int &d=chid[_i].S;
		Forr(_j,sz(ch[id])-1,0){
			auto &c=ch[id][_j];
			a.eb(c.val,min(c.tail,d)-c.head+1);
			if(c.tail>=d) break;
		}
	}

	int c=g[v].c;
	while(v!=0){
		int id=g[v].chainid;
		int h;
		if(sz(ch[id]))
			h=ch[id].back().head;
		else
			h=g[v].dep;
		while(sz(ch[id]) && ch[id].back().tail<=g[v].dep)
			ch[id].pop_back();
		if(sz(ch[id]) && ch[id].back().head<=g[v].dep)
			ch[id].back().head=g[v].dep+1;
		ch[id].push_back({h,g[v].dep,c});
		v=g[g[v].head].par;
	}

	int ans=0;
	int tot=0;
	for(auto &i:a){
		ans+=i.S*(tot-bit.ask(i.F));
		bit.add(i.F,i.S);
		tot+=i.S;
	}
	for(auto &i:a){
		bit.add(i.F,-i.S);
	}

	/*
	for(auto &i:a)
		cerr<<i.F<<"x"<<i.S<<" ";
	cerr<<"\n";
	For(i,0,1){
		cerr<<"chain "<<i<<" >> ";
		for(auto &j:ch[i])
			cerr<<j.head<<":"<<j.tail<<"="<<j.val<<"  ";
		cerr<<"\n";
	}
	cerr<<"\n";
	*/
	return ans;
}

int32_t main(){
	NYA();
	//nyaacho >/////<
	int n; cin>>n;

	vector<int> al;
	For(i,1,n){
		cin>>g[i].c;
		al.eb(g[i].c);
	}
	sort(all(al));
	al.erase(unique(all(al)),al.end());
	For(i,1,n){
		g[i].c=lower_bound(all(al),g[i].c)-al.begin()+1;
	}
	
	g[1].par=0;
	vector<pii> ed;
	For(i,1,n-1){
		int a,b; cin>>a>>b;
		g[a].adj.eb(b);
		g[b].par=a;
		ed.eb(a,b);
	}

	dfs(1,1);
	build(1,1);

	bit.init(n);
	ch[g[1].chainid].push_back({g[1].dep,g[1].dep,g[1].c});
	for(auto &i:ed){
		cout<<link(i.F,i.S)<<"\n";
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11212 KB Output is correct
2 Correct 7 ms 11164 KB Output is correct
3 Correct 6 ms 11212 KB Output is correct
4 Correct 7 ms 11208 KB Output is correct
5 Correct 8 ms 11264 KB Output is correct
6 Correct 7 ms 11212 KB Output is correct
7 Correct 7 ms 11264 KB Output is correct
8 Correct 7 ms 11340 KB Output is correct
9 Correct 7 ms 11340 KB Output is correct
10 Correct 7 ms 11340 KB Output is correct
11 Correct 6 ms 11272 KB Output is correct
12 Correct 6 ms 11296 KB Output is correct
13 Correct 7 ms 11340 KB Output is correct
14 Correct 7 ms 11212 KB Output is correct
15 Correct 7 ms 11316 KB Output is correct
16 Correct 7 ms 11212 KB Output is correct
17 Correct 7 ms 11212 KB Output is correct
18 Correct 7 ms 11344 KB Output is correct
19 Correct 7 ms 11296 KB Output is correct
20 Correct 7 ms 11268 KB Output is correct
21 Correct 7 ms 11212 KB Output is correct
22 Correct 9 ms 11212 KB Output is correct
23 Correct 8 ms 11212 KB Output is correct
24 Correct 8 ms 11212 KB Output is correct
25 Correct 8 ms 11240 KB Output is correct
26 Correct 7 ms 11268 KB Output is correct
27 Correct 8 ms 11340 KB Output is correct
28 Correct 8 ms 11232 KB Output is correct
29 Correct 7 ms 11212 KB Output is correct
30 Correct 8 ms 11248 KB Output is correct
31 Correct 8 ms 11212 KB Output is correct
32 Correct 8 ms 11264 KB Output is correct
33 Correct 7 ms 11272 KB Output is correct
34 Correct 7 ms 11212 KB Output is correct
35 Correct 8 ms 11272 KB Output is correct
36 Correct 7 ms 11212 KB Output is correct
37 Correct 7 ms 11244 KB Output is correct
38 Correct 7 ms 11212 KB Output is correct
39 Correct 8 ms 11340 KB Output is correct
40 Correct 8 ms 11312 KB Output is correct
41 Correct 7 ms 11252 KB Output is correct
42 Correct 7 ms 11308 KB Output is correct
43 Correct 8 ms 11340 KB Output is correct
44 Correct 7 ms 11212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11212 KB Output is correct
2 Correct 7 ms 11164 KB Output is correct
3 Correct 6 ms 11212 KB Output is correct
4 Correct 7 ms 11208 KB Output is correct
5 Correct 8 ms 11264 KB Output is correct
6 Correct 7 ms 11212 KB Output is correct
7 Correct 7 ms 11264 KB Output is correct
8 Correct 7 ms 11340 KB Output is correct
9 Correct 7 ms 11340 KB Output is correct
10 Correct 7 ms 11340 KB Output is correct
11 Correct 6 ms 11272 KB Output is correct
12 Correct 6 ms 11296 KB Output is correct
13 Correct 7 ms 11340 KB Output is correct
14 Correct 7 ms 11212 KB Output is correct
15 Correct 7 ms 11316 KB Output is correct
16 Correct 7 ms 11212 KB Output is correct
17 Correct 7 ms 11212 KB Output is correct
18 Correct 7 ms 11344 KB Output is correct
19 Correct 7 ms 11296 KB Output is correct
20 Correct 7 ms 11268 KB Output is correct
21 Correct 7 ms 11212 KB Output is correct
22 Correct 9 ms 11212 KB Output is correct
23 Correct 8 ms 11212 KB Output is correct
24 Correct 8 ms 11212 KB Output is correct
25 Correct 8 ms 11240 KB Output is correct
26 Correct 7 ms 11268 KB Output is correct
27 Correct 8 ms 11340 KB Output is correct
28 Correct 8 ms 11232 KB Output is correct
29 Correct 7 ms 11212 KB Output is correct
30 Correct 8 ms 11248 KB Output is correct
31 Correct 8 ms 11212 KB Output is correct
32 Correct 8 ms 11264 KB Output is correct
33 Correct 7 ms 11272 KB Output is correct
34 Correct 7 ms 11212 KB Output is correct
35 Correct 8 ms 11272 KB Output is correct
36 Correct 7 ms 11212 KB Output is correct
37 Correct 7 ms 11244 KB Output is correct
38 Correct 7 ms 11212 KB Output is correct
39 Correct 8 ms 11340 KB Output is correct
40 Correct 8 ms 11312 KB Output is correct
41 Correct 7 ms 11252 KB Output is correct
42 Correct 7 ms 11308 KB Output is correct
43 Correct 8 ms 11340 KB Output is correct
44 Correct 7 ms 11212 KB Output is correct
45 Correct 8 ms 11340 KB Output is correct
46 Correct 13 ms 11596 KB Output is correct
47 Correct 12 ms 11596 KB Output is correct
48 Correct 13 ms 11532 KB Output is correct
49 Correct 10 ms 11800 KB Output is correct
50 Correct 11 ms 11852 KB Output is correct
51 Correct 11 ms 11788 KB Output is correct
52 Correct 11 ms 11756 KB Output is correct
53 Correct 11 ms 11768 KB Output is correct
54 Correct 11 ms 11788 KB Output is correct
55 Correct 11 ms 11804 KB Output is correct
56 Correct 11 ms 11724 KB Output is correct
57 Correct 13 ms 11532 KB Output is correct
58 Correct 14 ms 11636 KB Output is correct
59 Correct 15 ms 11564 KB Output is correct
60 Correct 14 ms 11532 KB Output is correct
61 Correct 11 ms 11724 KB Output is correct
62 Correct 11 ms 11724 KB Output is correct
63 Correct 11 ms 11748 KB Output is correct
64 Correct 12 ms 11584 KB Output is correct
65 Correct 12 ms 11596 KB Output is correct
66 Correct 12 ms 11596 KB Output is correct
67 Correct 12 ms 11568 KB Output is correct
68 Correct 10 ms 11852 KB Output is correct
69 Correct 10 ms 11764 KB Output is correct
70 Correct 10 ms 11724 KB Output is correct
71 Correct 10 ms 11656 KB Output is correct
72 Correct 15 ms 11608 KB Output is correct
73 Correct 14 ms 11528 KB Output is correct
74 Correct 10 ms 11596 KB Output is correct
75 Correct 11 ms 11596 KB Output is correct
76 Correct 11 ms 11536 KB Output is correct
77 Correct 11 ms 11596 KB Output is correct
78 Correct 10 ms 11568 KB Output is correct
79 Correct 11 ms 11596 KB Output is correct
80 Correct 11 ms 11576 KB Output is correct
81 Correct 11 ms 11648 KB Output is correct
82 Correct 11 ms 11532 KB Output is correct
83 Correct 11 ms 11596 KB Output is correct
84 Correct 10 ms 11596 KB Output is correct
85 Correct 11 ms 11596 KB Output is correct
86 Correct 11 ms 11600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11212 KB Output is correct
2 Correct 7 ms 11164 KB Output is correct
3 Correct 6 ms 11212 KB Output is correct
4 Correct 7 ms 11208 KB Output is correct
5 Correct 8 ms 11264 KB Output is correct
6 Correct 7 ms 11212 KB Output is correct
7 Correct 7 ms 11264 KB Output is correct
8 Correct 7 ms 11340 KB Output is correct
9 Correct 7 ms 11340 KB Output is correct
10 Correct 7 ms 11340 KB Output is correct
11 Correct 6 ms 11272 KB Output is correct
12 Correct 6 ms 11296 KB Output is correct
13 Correct 7 ms 11340 KB Output is correct
14 Correct 7 ms 11212 KB Output is correct
15 Correct 7 ms 11316 KB Output is correct
16 Correct 7 ms 11212 KB Output is correct
17 Correct 7 ms 11212 KB Output is correct
18 Correct 7 ms 11344 KB Output is correct
19 Correct 7 ms 11296 KB Output is correct
20 Correct 7 ms 11268 KB Output is correct
21 Correct 7 ms 11212 KB Output is correct
22 Correct 9 ms 11212 KB Output is correct
23 Correct 8 ms 11212 KB Output is correct
24 Correct 8 ms 11212 KB Output is correct
25 Correct 8 ms 11240 KB Output is correct
26 Correct 7 ms 11268 KB Output is correct
27 Correct 8 ms 11340 KB Output is correct
28 Correct 8 ms 11232 KB Output is correct
29 Correct 7 ms 11212 KB Output is correct
30 Correct 8 ms 11248 KB Output is correct
31 Correct 8 ms 11212 KB Output is correct
32 Correct 8 ms 11264 KB Output is correct
33 Correct 7 ms 11272 KB Output is correct
34 Correct 7 ms 11212 KB Output is correct
35 Correct 8 ms 11272 KB Output is correct
36 Correct 7 ms 11212 KB Output is correct
37 Correct 7 ms 11244 KB Output is correct
38 Correct 7 ms 11212 KB Output is correct
39 Correct 8 ms 11340 KB Output is correct
40 Correct 8 ms 11312 KB Output is correct
41 Correct 7 ms 11252 KB Output is correct
42 Correct 7 ms 11308 KB Output is correct
43 Correct 8 ms 11340 KB Output is correct
44 Correct 7 ms 11212 KB Output is correct
45 Correct 8 ms 11340 KB Output is correct
46 Correct 13 ms 11596 KB Output is correct
47 Correct 12 ms 11596 KB Output is correct
48 Correct 13 ms 11532 KB Output is correct
49 Correct 10 ms 11800 KB Output is correct
50 Correct 11 ms 11852 KB Output is correct
51 Correct 11 ms 11788 KB Output is correct
52 Correct 11 ms 11756 KB Output is correct
53 Correct 11 ms 11768 KB Output is correct
54 Correct 11 ms 11788 KB Output is correct
55 Correct 11 ms 11804 KB Output is correct
56 Correct 11 ms 11724 KB Output is correct
57 Correct 13 ms 11532 KB Output is correct
58 Correct 14 ms 11636 KB Output is correct
59 Correct 15 ms 11564 KB Output is correct
60 Correct 14 ms 11532 KB Output is correct
61 Correct 11 ms 11724 KB Output is correct
62 Correct 11 ms 11724 KB Output is correct
63 Correct 11 ms 11748 KB Output is correct
64 Correct 12 ms 11584 KB Output is correct
65 Correct 12 ms 11596 KB Output is correct
66 Correct 12 ms 11596 KB Output is correct
67 Correct 12 ms 11568 KB Output is correct
68 Correct 10 ms 11852 KB Output is correct
69 Correct 10 ms 11764 KB Output is correct
70 Correct 10 ms 11724 KB Output is correct
71 Correct 10 ms 11656 KB Output is correct
72 Correct 15 ms 11608 KB Output is correct
73 Correct 14 ms 11528 KB Output is correct
74 Correct 10 ms 11596 KB Output is correct
75 Correct 11 ms 11596 KB Output is correct
76 Correct 11 ms 11536 KB Output is correct
77 Correct 11 ms 11596 KB Output is correct
78 Correct 10 ms 11568 KB Output is correct
79 Correct 11 ms 11596 KB Output is correct
80 Correct 11 ms 11576 KB Output is correct
81 Correct 11 ms 11648 KB Output is correct
82 Correct 11 ms 11532 KB Output is correct
83 Correct 11 ms 11596 KB Output is correct
84 Correct 10 ms 11596 KB Output is correct
85 Correct 11 ms 11596 KB Output is correct
86 Correct 11 ms 11600 KB Output is correct
87 Correct 24 ms 12104 KB Output is correct
88 Correct 60 ms 13836 KB Output is correct
89 Correct 255 ms 19984 KB Output is correct
90 Correct 256 ms 20072 KB Output is correct
91 Correct 257 ms 20100 KB Output is correct
92 Correct 147 ms 26984 KB Output is correct
93 Correct 139 ms 26808 KB Output is correct
94 Correct 140 ms 26808 KB Output is correct
95 Correct 137 ms 25612 KB Output is correct
96 Correct 151 ms 26040 KB Output is correct
97 Correct 144 ms 26072 KB Output is correct
98 Correct 141 ms 25980 KB Output is correct
99 Correct 155 ms 23240 KB Output is correct
100 Correct 326 ms 20148 KB Output is correct
101 Correct 358 ms 20236 KB Output is correct
102 Correct 361 ms 20312 KB Output is correct
103 Correct 346 ms 20220 KB Output is correct
104 Correct 164 ms 23116 KB Output is correct
105 Correct 171 ms 23236 KB Output is correct
106 Correct 168 ms 23096 KB Output is correct
107 Correct 226 ms 19340 KB Output is correct
108 Correct 253 ms 19332 KB Output is correct
109 Correct 263 ms 19588 KB Output is correct
110 Correct 128 ms 26172 KB Output is correct
111 Correct 132 ms 25580 KB Output is correct
112 Correct 126 ms 25396 KB Output is correct
113 Correct 127 ms 22460 KB Output is correct
114 Correct 303 ms 19956 KB Output is correct
115 Correct 337 ms 19704 KB Output is correct
116 Correct 142 ms 22504 KB Output is correct
117 Correct 150 ms 20792 KB Output is correct
118 Correct 149 ms 20080 KB Output is correct
119 Correct 148 ms 19724 KB Output is correct
120 Correct 136 ms 20352 KB Output is correct
121 Correct 133 ms 19784 KB Output is correct
122 Correct 137 ms 19460 KB Output is correct
123 Correct 174 ms 20872 KB Output is correct
124 Correct 166 ms 20124 KB Output is correct
125 Correct 166 ms 19836 KB Output is correct
126 Correct 151 ms 20528 KB Output is correct
127 Correct 154 ms 19852 KB Output is correct
128 Correct 154 ms 19488 KB Output is correct