#include <bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define pb push_back
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#define FOR(i,a,b) for (int i = a; i<b; ++i)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x ){cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y){cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
//const int maxn = 3e5+5;
const int MXM = 1e5+5;
const int MXN = 502;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int eA[MXM], eB[MXM], eW[MXM];
int n,m;
vector<int> g[MXN];
ll best[MXN];
int par[MXN];
int find(int x) {
return x == par[x]? x : par[x] = find(par[x]);
}
void mrg(int a, int b) {
a=find(a); b =find(b); par[a] = b;
}
vector<int> curedges;
pii mst(){
REP(i,n) par[i] = i;
pii re = {curedges.back(), -1};
int gone = -1;
for (int i = SZ(curedges)-1; i >= 0; --i) {
int e = curedges[i];
if (find(eA[e]) != find(eB[e])) {
mrg(eA[e], eB[e]);
}else {
re.s = e;
gone = i;
}
}
if (gone != -1) {
curedges.erase(curedges.begin() + gone);
}
return re;
}
vector<int> gA, gB;
//bool used[maxn];
//ll mst2(int T){
// ll re = 0;
// int i = SZ(gA)-1, j = SZ(gB)-1;
// while (i>=0 || j>=0) {
// if (i==-1 || (j!=-1 && eW[gB]))
// }
//}
int L[MXM],R[MXM];
void get(vector<int> eid, bool workl) {
// for each added edge (in order), find the mst change
// vector<pii> re; // {added edge, removed edge}; -1 if no change
curedges.clear();
REP(i, SZ(eid)) {
curedges.pb(eid[i]);
pii get = mst();
int bye = get.s;
if (bye != -1) {
if (workl) {
L[bye] = (eW[get.s] + eW[eid[i]]) / 2 + 1;
}else{
R[bye] = (eW[get.s] + eW[eid[i]]) / 2 + 1;
}
}
}
}
//bool inuse[MXM];
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(__lg(25)+1);
cin>>n>>m;
// bug(get(n,m));
vector<int> eid;
REP(i,m) {
int a,b,w; cin>>a>>b>>w; --a; --b;
eA[i] = a, eB[i] = b, eW[i] = w;
eid.pb(i);
}
sort(ALL(eid), [&](int a, int b){return make_pair(eW[a],a) < make_pair(eW[b],b);});
// vector<pii> chga = get(eid);
// reverse(ALL(eid));
// vector<pii> chgb = get(eid);
// reverse(ALL(chgb));
// reverse(ALL(eid));
memset(L, -1, sizeof L);
memset(R, 0x3f, sizeof R);
get(eid, 0);
reverse(ALL(eid));
get(eid, 1);
reverse(ALL(eid));
for (int e : curedges) {
bug(e, eA[e], eB[e], eW[e]);
}
vector<pair<int, pii> > event;
REP(i,m) {
bug(i, eA[i], eB[i], eW[i]);
bug(L[i], R[i]);
event.pb({L[i], {eW[i], -1}});
event.pb({eW[i], {-2*eW[i], 2}});
event.pb({R[i], {eW[i], -1}});
}
int Q; cin>>Q;
int eat = 0;
// mst2();
sort(ALL(event));
vector<int> now;
ll fm =0, fb = 0; // mT + b
while (Q--) {
int T; cin>>T;
while (eat < SZ(event) && event[eat].f <= T) {
fm += event[eat].s.s;
fb += event[eat].s.f;
++eat;
}
bug(fm, fb, T);
cout<<fm * T + fb<<endl;
// bool madechg = 0;
// while (eat < SZ(eid) && T >= eW[eid[eat]]) {
//
// if (chga[eat].s!=-1) gA.erase(find(ALL(gA), chga[eat].s));
// if (chga[eat].f!=-1) gA.pb(chga[eat].f);
//
// if (chgb[eat].f!=-1) gB.erase(find(ALL(gB), chgb[eat].f));
// if (chgb[eat].s!=-1) {
// gB.pb(chgb[eat].s);
// for (int j = SZ(gB)-1; j>0 && eW[gB[j]] > eW[gB[j-1]]; --j) {
// swap(gB[j], gB[j-1]);
// }
// }
//
// ++eat;
// madechg = 1;
// }
// if (madechg) {
// mst2();
// }
}
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:132:14: warning: unused variable 'e' [-Wunused-variable]
132 | for (int e : curedges) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1888 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1920 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
1876 KB |
Output is correct |
18 |
Correct |
1 ms |
1888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1888 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1920 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
1876 KB |
Output is correct |
18 |
Correct |
1 ms |
1888 KB |
Output is correct |
19 |
Correct |
2 ms |
1876 KB |
Output is correct |
20 |
Correct |
2679 ms |
19256 KB |
Output is correct |
21 |
Correct |
1851 ms |
19184 KB |
Output is correct |
22 |
Correct |
2009 ms |
19224 KB |
Output is correct |
23 |
Correct |
2093 ms |
19176 KB |
Output is correct |
24 |
Correct |
2394 ms |
19168 KB |
Output is correct |
25 |
Correct |
2774 ms |
19172 KB |
Output is correct |
26 |
Correct |
2729 ms |
19172 KB |
Output is correct |
27 |
Correct |
2715 ms |
19168 KB |
Output is correct |
28 |
Correct |
2761 ms |
19156 KB |
Output is correct |
29 |
Correct |
2227 ms |
19156 KB |
Output is correct |
30 |
Correct |
2673 ms |
19344 KB |
Output is correct |
31 |
Correct |
2701 ms |
19124 KB |
Output is correct |
32 |
Correct |
2761 ms |
19316 KB |
Output is correct |
33 |
Correct |
2693 ms |
19272 KB |
Output is correct |
34 |
Correct |
2043 ms |
19236 KB |
Output is correct |
35 |
Correct |
2704 ms |
19156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1876 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
1876 KB |
Output is correct |
4 |
Correct |
2111 ms |
34576 KB |
Output is correct |
5 |
Correct |
2087 ms |
34508 KB |
Output is correct |
6 |
Correct |
2048 ms |
34724 KB |
Output is correct |
7 |
Correct |
1192 ms |
36496 KB |
Output is correct |
8 |
Correct |
1126 ms |
36792 KB |
Output is correct |
9 |
Correct |
1094 ms |
36628 KB |
Output is correct |
10 |
Correct |
2033 ms |
34748 KB |
Output is correct |
11 |
Correct |
1068 ms |
36688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1888 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1920 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
1876 KB |
Output is correct |
18 |
Correct |
1 ms |
1888 KB |
Output is correct |
19 |
Correct |
1 ms |
1876 KB |
Output is correct |
20 |
Correct |
179 ms |
23976 KB |
Output is correct |
21 |
Correct |
193 ms |
24168 KB |
Output is correct |
22 |
Correct |
197 ms |
24048 KB |
Output is correct |
23 |
Correct |
176 ms |
24008 KB |
Output is correct |
24 |
Correct |
187 ms |
23968 KB |
Output is correct |
25 |
Correct |
194 ms |
23972 KB |
Output is correct |
26 |
Correct |
192 ms |
23932 KB |
Output is correct |
27 |
Correct |
179 ms |
24008 KB |
Output is correct |
28 |
Correct |
192 ms |
23956 KB |
Output is correct |
29 |
Correct |
195 ms |
23984 KB |
Output is correct |
30 |
Correct |
201 ms |
24024 KB |
Output is correct |
31 |
Correct |
192 ms |
23896 KB |
Output is correct |
32 |
Correct |
192 ms |
24428 KB |
Output is correct |
33 |
Correct |
189 ms |
23932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1888 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1920 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
1876 KB |
Output is correct |
18 |
Correct |
1 ms |
1888 KB |
Output is correct |
19 |
Correct |
2 ms |
1876 KB |
Output is correct |
20 |
Correct |
2679 ms |
19256 KB |
Output is correct |
21 |
Correct |
1851 ms |
19184 KB |
Output is correct |
22 |
Correct |
2009 ms |
19224 KB |
Output is correct |
23 |
Correct |
2093 ms |
19176 KB |
Output is correct |
24 |
Correct |
2394 ms |
19168 KB |
Output is correct |
25 |
Correct |
2774 ms |
19172 KB |
Output is correct |
26 |
Correct |
2729 ms |
19172 KB |
Output is correct |
27 |
Correct |
2715 ms |
19168 KB |
Output is correct |
28 |
Correct |
2761 ms |
19156 KB |
Output is correct |
29 |
Correct |
2227 ms |
19156 KB |
Output is correct |
30 |
Correct |
2673 ms |
19344 KB |
Output is correct |
31 |
Correct |
2701 ms |
19124 KB |
Output is correct |
32 |
Correct |
2761 ms |
19316 KB |
Output is correct |
33 |
Correct |
2693 ms |
19272 KB |
Output is correct |
34 |
Correct |
2043 ms |
19236 KB |
Output is correct |
35 |
Correct |
2704 ms |
19156 KB |
Output is correct |
36 |
Correct |
2685 ms |
19340 KB |
Output is correct |
37 |
Correct |
1913 ms |
19300 KB |
Output is correct |
38 |
Correct |
2004 ms |
19552 KB |
Output is correct |
39 |
Correct |
2107 ms |
19372 KB |
Output is correct |
40 |
Correct |
2367 ms |
19356 KB |
Output is correct |
41 |
Correct |
2810 ms |
19376 KB |
Output is correct |
42 |
Correct |
2669 ms |
19372 KB |
Output is correct |
43 |
Correct |
2687 ms |
19416 KB |
Output is correct |
44 |
Correct |
2648 ms |
19536 KB |
Output is correct |
45 |
Correct |
2161 ms |
19372 KB |
Output is correct |
46 |
Correct |
2688 ms |
19388 KB |
Output is correct |
47 |
Correct |
2623 ms |
19380 KB |
Output is correct |
48 |
Correct |
2667 ms |
19364 KB |
Output is correct |
49 |
Correct |
2661 ms |
19352 KB |
Output is correct |
50 |
Correct |
2047 ms |
19468 KB |
Output is correct |
51 |
Correct |
2688 ms |
19356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1888 KB |
Output is correct |
2 |
Correct |
1 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
1920 KB |
Output is correct |
4 |
Correct |
1 ms |
1876 KB |
Output is correct |
5 |
Correct |
1 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1876 KB |
Output is correct |
8 |
Correct |
1 ms |
1876 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
1876 KB |
Output is correct |
18 |
Correct |
1 ms |
1888 KB |
Output is correct |
19 |
Correct |
2 ms |
1876 KB |
Output is correct |
20 |
Correct |
2679 ms |
19256 KB |
Output is correct |
21 |
Correct |
1851 ms |
19184 KB |
Output is correct |
22 |
Correct |
2009 ms |
19224 KB |
Output is correct |
23 |
Correct |
2093 ms |
19176 KB |
Output is correct |
24 |
Correct |
2394 ms |
19168 KB |
Output is correct |
25 |
Correct |
2774 ms |
19172 KB |
Output is correct |
26 |
Correct |
2729 ms |
19172 KB |
Output is correct |
27 |
Correct |
2715 ms |
19168 KB |
Output is correct |
28 |
Correct |
2761 ms |
19156 KB |
Output is correct |
29 |
Correct |
2227 ms |
19156 KB |
Output is correct |
30 |
Correct |
2673 ms |
19344 KB |
Output is correct |
31 |
Correct |
2701 ms |
19124 KB |
Output is correct |
32 |
Correct |
2761 ms |
19316 KB |
Output is correct |
33 |
Correct |
2693 ms |
19272 KB |
Output is correct |
34 |
Correct |
2043 ms |
19236 KB |
Output is correct |
35 |
Correct |
2704 ms |
19156 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1876 KB |
Output is correct |
38 |
Correct |
1 ms |
1876 KB |
Output is correct |
39 |
Correct |
2111 ms |
34576 KB |
Output is correct |
40 |
Correct |
2087 ms |
34508 KB |
Output is correct |
41 |
Correct |
2048 ms |
34724 KB |
Output is correct |
42 |
Correct |
1192 ms |
36496 KB |
Output is correct |
43 |
Correct |
1126 ms |
36792 KB |
Output is correct |
44 |
Correct |
1094 ms |
36628 KB |
Output is correct |
45 |
Correct |
2033 ms |
34748 KB |
Output is correct |
46 |
Correct |
1068 ms |
36688 KB |
Output is correct |
47 |
Correct |
1 ms |
1876 KB |
Output is correct |
48 |
Correct |
179 ms |
23976 KB |
Output is correct |
49 |
Correct |
193 ms |
24168 KB |
Output is correct |
50 |
Correct |
197 ms |
24048 KB |
Output is correct |
51 |
Correct |
176 ms |
24008 KB |
Output is correct |
52 |
Correct |
187 ms |
23968 KB |
Output is correct |
53 |
Correct |
194 ms |
23972 KB |
Output is correct |
54 |
Correct |
192 ms |
23932 KB |
Output is correct |
55 |
Correct |
179 ms |
24008 KB |
Output is correct |
56 |
Correct |
192 ms |
23956 KB |
Output is correct |
57 |
Correct |
195 ms |
23984 KB |
Output is correct |
58 |
Correct |
201 ms |
24024 KB |
Output is correct |
59 |
Correct |
192 ms |
23896 KB |
Output is correct |
60 |
Correct |
192 ms |
24428 KB |
Output is correct |
61 |
Correct |
189 ms |
23932 KB |
Output is correct |
62 |
Correct |
2685 ms |
19340 KB |
Output is correct |
63 |
Correct |
1913 ms |
19300 KB |
Output is correct |
64 |
Correct |
2004 ms |
19552 KB |
Output is correct |
65 |
Correct |
2107 ms |
19372 KB |
Output is correct |
66 |
Correct |
2367 ms |
19356 KB |
Output is correct |
67 |
Correct |
2810 ms |
19376 KB |
Output is correct |
68 |
Correct |
2669 ms |
19372 KB |
Output is correct |
69 |
Correct |
2687 ms |
19416 KB |
Output is correct |
70 |
Correct |
2648 ms |
19536 KB |
Output is correct |
71 |
Correct |
2161 ms |
19372 KB |
Output is correct |
72 |
Correct |
2688 ms |
19388 KB |
Output is correct |
73 |
Correct |
2623 ms |
19380 KB |
Output is correct |
74 |
Correct |
2667 ms |
19364 KB |
Output is correct |
75 |
Correct |
2661 ms |
19352 KB |
Output is correct |
76 |
Correct |
2047 ms |
19468 KB |
Output is correct |
77 |
Correct |
2688 ms |
19356 KB |
Output is correct |
78 |
Correct |
2862 ms |
33560 KB |
Output is correct |
79 |
Correct |
2043 ms |
35568 KB |
Output is correct |
80 |
Correct |
2142 ms |
34684 KB |
Output is correct |
81 |
Correct |
2203 ms |
34580 KB |
Output is correct |
82 |
Correct |
2519 ms |
33708 KB |
Output is correct |
83 |
Correct |
2968 ms |
33616 KB |
Output is correct |
84 |
Correct |
2890 ms |
33620 KB |
Output is correct |
85 |
Correct |
2838 ms |
33668 KB |
Output is correct |
86 |
Correct |
2822 ms |
33648 KB |
Output is correct |
87 |
Correct |
2295 ms |
35192 KB |
Output is correct |
88 |
Correct |
2787 ms |
33676 KB |
Output is correct |
89 |
Correct |
2759 ms |
33600 KB |
Output is correct |
90 |
Correct |
2765 ms |
33760 KB |
Output is correct |
91 |
Correct |
2764 ms |
33604 KB |
Output is correct |
92 |
Correct |
2183 ms |
36696 KB |
Output is correct |
93 |
Correct |
2769 ms |
34768 KB |
Output is correct |