/*input
3 4
1 2 1
1 2 4
2 3 2
2 3 4
4
1
2
3
4
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=5e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-6;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
out<<P.f<<' '<<P.s;
return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?" ":"");
return out;
}
ll mult(ll a,ll b){
return a*b%MOD;
}
ll mypow(ll a,ll b){
a%=MOD;
if(a==0) return 0;
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=(res*a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return res;
}
int par[maxn];
int find(int u){
if(par[u]==u) return u;
return par[u]=find(par[u]);
}
void merge(int a,int b){
a=find(a),b=find(b);
if(a==b) return;
par[a]=b;
}
vector<int> vv[maxn];
bool flp[maxn];
int cur[maxn];
vector<pair<int,pii>> ed;
int n;
bool wk(int mid,pair<int,pii> cur){
REP(i,n) par[i]=i;
for(auto x:ed){
if(make_pair(abs(x.f-mid),x)<make_pair(abs(cur.f-mid),cur)){
merge(x.s.f,x.s.s);
}
}
int a=cur.s.f,b=cur.s.s;
if(find(a)!=find(b)) return true;
return false;
}
int main(){
ios::sync_with_stdio(false),cin.tie(0);
int m;
cin>>n>>m;
REP(i,m){
int a,b,c;
cin>>a>>b>>c;
--a,--b;
ed.pb({c,{a,b}});
}
sort(ALL(ed));
vector<pair<int,pii>> turn;
for(auto x:ed){
int l=1,r=x.f;
while(l<r){
int mid=(l+r)/2;
if(wk(mid,x)) r=mid;
else l=mid+1;
}
int L=l;
l=x.f,r=1e9;
while(l<r){
int mid=(l+r+1)/2;
if(wk(mid,x)) l=mid;
else r=mid-1;
}
turn.pb({L,{-1,x.f}});
turn.pb({x.f,{2,-2*x.f}});
turn.pb({r+1,{-1,x.f}});
//cout<<L<<' '<<r<<'\n';
}
sort(ALL(turn));
int q;
cin>>q;
ll co=0,s=0;
int p=0;
while(q--){
int k;
cin>>k;
while(p<sz(turn) and turn[p].f<=k){
co+=turn[p].s.f;
s+=turn[p].s.s;
++p;
}
cout<<s+co*k<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
9 ms |
11984 KB |
Output is correct |
3 |
Correct |
7 ms |
11976 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11988 KB |
Output is correct |
9 |
Correct |
8 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
12012 KB |
Output is correct |
11 |
Correct |
6 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
11988 KB |
Output is correct |
15 |
Correct |
8 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
6 ms |
12068 KB |
Output is correct |
18 |
Correct |
9 ms |
11988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
9 ms |
11984 KB |
Output is correct |
3 |
Correct |
7 ms |
11976 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11988 KB |
Output is correct |
9 |
Correct |
8 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
12012 KB |
Output is correct |
11 |
Correct |
6 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
11988 KB |
Output is correct |
15 |
Correct |
8 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
6 ms |
12068 KB |
Output is correct |
18 |
Correct |
9 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Execution timed out |
5020 ms |
13720 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12076 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Execution timed out |
5055 ms |
13700 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
9 ms |
11984 KB |
Output is correct |
3 |
Correct |
7 ms |
11976 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11988 KB |
Output is correct |
9 |
Correct |
8 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
12012 KB |
Output is correct |
11 |
Correct |
6 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
11988 KB |
Output is correct |
15 |
Correct |
8 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
6 ms |
12068 KB |
Output is correct |
18 |
Correct |
9 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
474 ms |
24304 KB |
Output is correct |
21 |
Correct |
433 ms |
24512 KB |
Output is correct |
22 |
Correct |
436 ms |
24400 KB |
Output is correct |
23 |
Correct |
452 ms |
24552 KB |
Output is correct |
24 |
Correct |
466 ms |
24568 KB |
Output is correct |
25 |
Correct |
477 ms |
24464 KB |
Output is correct |
26 |
Correct |
481 ms |
24396 KB |
Output is correct |
27 |
Correct |
466 ms |
24372 KB |
Output is correct |
28 |
Correct |
460 ms |
24384 KB |
Output is correct |
29 |
Correct |
460 ms |
24404 KB |
Output is correct |
30 |
Correct |
459 ms |
24536 KB |
Output is correct |
31 |
Correct |
465 ms |
24404 KB |
Output is correct |
32 |
Correct |
428 ms |
25044 KB |
Output is correct |
33 |
Correct |
488 ms |
24256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
9 ms |
11984 KB |
Output is correct |
3 |
Correct |
7 ms |
11976 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11988 KB |
Output is correct |
9 |
Correct |
8 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
12012 KB |
Output is correct |
11 |
Correct |
6 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
11988 KB |
Output is correct |
15 |
Correct |
8 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
6 ms |
12068 KB |
Output is correct |
18 |
Correct |
9 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Execution timed out |
5020 ms |
13720 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
9 ms |
11984 KB |
Output is correct |
3 |
Correct |
7 ms |
11976 KB |
Output is correct |
4 |
Correct |
7 ms |
12080 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
11988 KB |
Output is correct |
9 |
Correct |
8 ms |
11988 KB |
Output is correct |
10 |
Correct |
7 ms |
12012 KB |
Output is correct |
11 |
Correct |
6 ms |
11988 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
7 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
11988 KB |
Output is correct |
15 |
Correct |
8 ms |
11988 KB |
Output is correct |
16 |
Correct |
7 ms |
11988 KB |
Output is correct |
17 |
Correct |
6 ms |
12068 KB |
Output is correct |
18 |
Correct |
9 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
11988 KB |
Output is correct |
20 |
Execution timed out |
5020 ms |
13720 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |