Submission #795662

# Submission time Handle Problem Language Result Execution time Memory
795662 2023-07-27T12:31:37 Z Antekb Reconstruction Project (JOI22_reconstruction) C++17
100 / 100
1102 ms 50048 KB
#include<bits/stdc++.h>

#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
 
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
 
using namespace std;
 
///~~~~~~~~~~~~~~~~~~~~~~~~~~
 
template <typename H, typename T> 
ostream& operator<<(ostream& os, pair<H, T> m){
	return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H> 
ostream& operator<<(ostream& os, vector<H> V){
	os<<"{";
	for(int i=0; i<V.size(); i++){
		if(i)os<<" ";
		os<<V[i];
	}
	os<<"}";
	return os;
}
 
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
//#define deb(x...) ;
 
///~~~~~~~~~~~~~~~~~~~~~~~~~
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

 
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int K=17, N=1<<K, INF=1e9+5, mod2=1e9+7, mod=998244353;

struct modint{
	int n=0;
	modint(){}
	modint(ll x){
		n=x%mod;
		if(n<0)n+=mod;
	}
	operator int(){
		return n;
	}
	modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;}
	modint operator+=(modint a){return (*this)=(*this)+a;}
	modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;}
	modint operator-=(modint a){return (*this)=(*this)-a;}
	modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;}
	modint operator*=(modint a){return (*this)=(*this)*a;}
	modint operator^(const ll &m)const{
		modint a(1);
		if(m==0)return a;
		if(m==1)return (*this);
		a=(*this)^(m/2);
		a*=a;
		return a*((*this)^(m&1));
	}
	modint odw(){
		return (*this)^((ll)mod-2);
	}
	modint operator/(modint a){return (*this)*a.odw();}
	modint operator/=(modint a){return (*this)=(*this)/a;}
	bool operator==(modint a){return a.n==n;}
	friend ostream& operator<<(ostream& os, modint m) {
		return os << m.n; 
	}
};
/*modint fact[N], fact2[N];
typedef vector<modint> vm;
void factinit(){
    fact[0]=1;
    for(int i=1; i<N; i++){
        fact[i]=(fact[i-1]*modint(i))%mod;
    }
    fact2[N-1]=fact[N-1].odw();
    for(int i=N-2; i>=0; i--){
    	fact2[i]=(fact2[i+1]*modint(i+1))%mod;
    }
}
modint npok(int _n, int _k){
 	if(_n<_k)return 0;
    return fact[_n]*fact2[_k]*fact2[_n-_k];
}*/
int los(int a, int b){
	return a+(rng())%(b-a+1);
}
int r[N];
int find(int v){
	if(v==r[v])return v;
	return r[v]=find(r[v]);
}
void Union(int u, int v){
	r[find(u)]=find(v);
}
int main(){
	//factinit();
	//BOOST
	int n, m, q;
	cin>>n>>m;
	vector<pair<int, pii> > edg(m);
	vi pocz(m, 1e9+5), kon(m, 1e9+5);
	for(int i=0; i<m; i++){
		int u, v, w;
		cin>>u>>v>>w;
		edg[i]=mp(w, mp(u, v));
	}
	sor(edg);
	for(int i=1; i<=n; i++)r[i]=i;
	vi akt;
	for(int i=0; i<m; i++){
		int u=edg[i].nd.st, v=edg[i].nd.nd;
		u=find(u);
		v=find(v);
		if(u!=v){
			r[u]=v;
			pocz[i]=0;
			akt.pb(i);
		}
	}
	//deb(akt)
	for(int i=0; i<m; i++){
		if(pocz[i]==1e9+5){
			for(int j=1; j<=n; j++)r[j]=j;
			vi akt2;
			bool b=1;
			//deb(edg[i]);
			for(int e=n-2; e>=0; e--){
				if(b&&akt[e]<i){
					//deb("a");
					Union(edg[i].nd.st, edg[i].nd.nd);
					akt2.pb(i);
				 	b=0;
				}
				int u=find(edg[akt[e]].nd.st), v=find(edg[akt[e]].nd.nd);
				//deb(u, v);
				if(u!=v){
					r[u]=v;
					akt2.pb(akt[e]);
				}
				else kon[akt[e]]=pocz[i]=(edg[akt[e]].st+edg[i].st+1)/2;
			}
			rev(akt2);
			akt=akt2;
			//deb(akt);
		}
	}
	for(int i:akt)kon[i]=1e9+5;
	vector<pair<int, pii> > V;
	for(int i=0; i<m; i++){
		//deb(edg[i], pocz[i], kon[i]);
		V.eb(pocz[i], mp(-1, edg[i].st));
		V.eb(edg[i].st, mp(2, -2*edg[i].st));
		V.eb(kon[i], mp(-1, edg[i].st));
	}
	cin>>q;
	vii Q;
	for(int i=0; i<q; i++){
		int x;
		cin>>x;
		Q.eb(x, i);
	}
	vector<ll> ans(q);
	sor(Q);
	sor(V);
	ll A=0, B=0;
	int wsk=0;
	for(int i=0; i<=V.size(); i++){
		while(wsk<q && (i==V.size() || Q[wsk].st<V[i].st)){
			ans[Q[wsk].nd]=A*Q[wsk].st+B;
			//deb(Q[wsk], ans[Q[wsk].nd]);
			wsk++;
		}
		if(i<V.size()){
			//deb(V[i]);
			A+=V[i].nd.st;
			B+=V[i].nd.nd;
		}
	}
	for(int i=0; i<q; i++){
		cout<<ans[i]<<"\n";
	}
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:201:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  201 |  for(int i=0; i<=V.size(); i++){
      |               ~^~~~~~~~~~
reconstruction.cpp:202:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  202 |   while(wsk<q && (i==V.size() || Q[wsk].st<V[i].st)){
      |                   ~^~~~~~~~~~
reconstruction.cpp:207:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  207 |   if(i<V.size()){
      |      ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 312 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 312 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 673 ms 10236 KB Output is correct
21 Correct 427 ms 10244 KB Output is correct
22 Correct 402 ms 10228 KB Output is correct
23 Correct 421 ms 10232 KB Output is correct
24 Correct 512 ms 10176 KB Output is correct
25 Correct 728 ms 10236 KB Output is correct
26 Correct 675 ms 10344 KB Output is correct
27 Correct 682 ms 10240 KB Output is correct
28 Correct 689 ms 10236 KB Output is correct
29 Correct 522 ms 10172 KB Output is correct
30 Correct 680 ms 10236 KB Output is correct
31 Correct 671 ms 10236 KB Output is correct
32 Correct 672 ms 10236 KB Output is correct
33 Correct 683 ms 10240 KB Output is correct
34 Correct 496 ms 10316 KB Output is correct
35 Correct 672 ms 10240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 752 ms 48136 KB Output is correct
5 Correct 755 ms 47912 KB Output is correct
6 Correct 752 ms 47908 KB Output is correct
7 Correct 752 ms 49888 KB Output is correct
8 Correct 735 ms 49960 KB Output is correct
9 Correct 715 ms 50048 KB Output is correct
10 Correct 773 ms 48092 KB Output is correct
11 Correct 732 ms 49996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 312 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 371 ms 38092 KB Output is correct
21 Correct 380 ms 38228 KB Output is correct
22 Correct 388 ms 38352 KB Output is correct
23 Correct 376 ms 38220 KB Output is correct
24 Correct 373 ms 38128 KB Output is correct
25 Correct 397 ms 38048 KB Output is correct
26 Correct 378 ms 38096 KB Output is correct
27 Correct 376 ms 38168 KB Output is correct
28 Correct 376 ms 38088 KB Output is correct
29 Correct 373 ms 38164 KB Output is correct
30 Correct 372 ms 38224 KB Output is correct
31 Correct 369 ms 37944 KB Output is correct
32 Correct 404 ms 38752 KB Output is correct
33 Correct 382 ms 38044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 312 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 673 ms 10236 KB Output is correct
21 Correct 427 ms 10244 KB Output is correct
22 Correct 402 ms 10228 KB Output is correct
23 Correct 421 ms 10232 KB Output is correct
24 Correct 512 ms 10176 KB Output is correct
25 Correct 728 ms 10236 KB Output is correct
26 Correct 675 ms 10344 KB Output is correct
27 Correct 682 ms 10240 KB Output is correct
28 Correct 689 ms 10236 KB Output is correct
29 Correct 522 ms 10172 KB Output is correct
30 Correct 680 ms 10236 KB Output is correct
31 Correct 671 ms 10236 KB Output is correct
32 Correct 672 ms 10236 KB Output is correct
33 Correct 683 ms 10240 KB Output is correct
34 Correct 496 ms 10316 KB Output is correct
35 Correct 672 ms 10240 KB Output is correct
36 Correct 683 ms 10432 KB Output is correct
37 Correct 433 ms 10436 KB Output is correct
38 Correct 412 ms 10424 KB Output is correct
39 Correct 424 ms 10424 KB Output is correct
40 Correct 519 ms 10428 KB Output is correct
41 Correct 737 ms 10428 KB Output is correct
42 Correct 689 ms 10428 KB Output is correct
43 Correct 674 ms 10428 KB Output is correct
44 Correct 678 ms 10432 KB Output is correct
45 Correct 532 ms 10432 KB Output is correct
46 Correct 670 ms 10424 KB Output is correct
47 Correct 691 ms 10552 KB Output is correct
48 Correct 673 ms 10432 KB Output is correct
49 Correct 669 ms 10424 KB Output is correct
50 Correct 527 ms 10536 KB Output is correct
51 Correct 674 ms 10552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 312 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 673 ms 10236 KB Output is correct
21 Correct 427 ms 10244 KB Output is correct
22 Correct 402 ms 10228 KB Output is correct
23 Correct 421 ms 10232 KB Output is correct
24 Correct 512 ms 10176 KB Output is correct
25 Correct 728 ms 10236 KB Output is correct
26 Correct 675 ms 10344 KB Output is correct
27 Correct 682 ms 10240 KB Output is correct
28 Correct 689 ms 10236 KB Output is correct
29 Correct 522 ms 10172 KB Output is correct
30 Correct 680 ms 10236 KB Output is correct
31 Correct 671 ms 10236 KB Output is correct
32 Correct 672 ms 10236 KB Output is correct
33 Correct 683 ms 10240 KB Output is correct
34 Correct 496 ms 10316 KB Output is correct
35 Correct 672 ms 10240 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 752 ms 48136 KB Output is correct
40 Correct 755 ms 47912 KB Output is correct
41 Correct 752 ms 47908 KB Output is correct
42 Correct 752 ms 49888 KB Output is correct
43 Correct 735 ms 49960 KB Output is correct
44 Correct 715 ms 50048 KB Output is correct
45 Correct 773 ms 48092 KB Output is correct
46 Correct 732 ms 49996 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 371 ms 38092 KB Output is correct
49 Correct 380 ms 38228 KB Output is correct
50 Correct 388 ms 38352 KB Output is correct
51 Correct 376 ms 38220 KB Output is correct
52 Correct 373 ms 38128 KB Output is correct
53 Correct 397 ms 38048 KB Output is correct
54 Correct 378 ms 38096 KB Output is correct
55 Correct 376 ms 38168 KB Output is correct
56 Correct 376 ms 38088 KB Output is correct
57 Correct 373 ms 38164 KB Output is correct
58 Correct 372 ms 38224 KB Output is correct
59 Correct 369 ms 37944 KB Output is correct
60 Correct 404 ms 38752 KB Output is correct
61 Correct 382 ms 38044 KB Output is correct
62 Correct 683 ms 10432 KB Output is correct
63 Correct 433 ms 10436 KB Output is correct
64 Correct 412 ms 10424 KB Output is correct
65 Correct 424 ms 10424 KB Output is correct
66 Correct 519 ms 10428 KB Output is correct
67 Correct 737 ms 10428 KB Output is correct
68 Correct 689 ms 10428 KB Output is correct
69 Correct 674 ms 10428 KB Output is correct
70 Correct 678 ms 10432 KB Output is correct
71 Correct 532 ms 10432 KB Output is correct
72 Correct 670 ms 10424 KB Output is correct
73 Correct 691 ms 10552 KB Output is correct
74 Correct 673 ms 10432 KB Output is correct
75 Correct 669 ms 10424 KB Output is correct
76 Correct 527 ms 10536 KB Output is correct
77 Correct 674 ms 10552 KB Output is correct
78 Correct 1029 ms 46892 KB Output is correct
79 Correct 798 ms 48940 KB Output is correct
80 Correct 773 ms 48008 KB Output is correct
81 Correct 784 ms 47916 KB Output is correct
82 Correct 893 ms 47148 KB Output is correct
83 Correct 1102 ms 46880 KB Output is correct
84 Correct 1036 ms 46880 KB Output is correct
85 Correct 1044 ms 46892 KB Output is correct
86 Correct 1039 ms 46884 KB Output is correct
87 Correct 883 ms 48620 KB Output is correct
88 Correct 1027 ms 46940 KB Output is correct
89 Correct 1032 ms 46936 KB Output is correct
90 Correct 1057 ms 47000 KB Output is correct
91 Correct 1041 ms 46868 KB Output is correct
92 Correct 901 ms 49948 KB Output is correct
93 Correct 1034 ms 48044 KB Output is correct