This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
5 10
1 2 8
1 3 13
1 4 5
1 5 11
1 5 3
2 3 7
2 4 15
3 4 6
3 5 6
4 5 2
6
3
6
8
10
13
17
1 3
1 4
4 6
1 10
5 8
1 10
7 1000000000
9 1000000000
11 1000000000
11 1000000000
*/
#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;
}
struct segt{
ll seg[4*maxn],lazy[4*maxn];
int n;
void resize(int _n){
n=_n;
}
inline void pushdown(int idx,int l,int r){
if(!lazy[idx]) return;
if(l==r){
lazy[idx]=0;
return;
}
int mid=l+r>>1;
seg[idx<<1]+=lazy[idx]*(mid-l+1);
seg[idx<<1|1]+=lazy[idx]*(r-mid);
lazy[idx<<1]+=lazy[idx];
lazy[idx<<1|1]+=lazy[idx];
lazy[idx]=0;
}
inline void upd(int idx,int l,int r,int ql,int qr,ll x){
if(ql<=l and r<=qr){
lazy[idx]+=x;
seg[idx]+=x*(r-l+1);
pushdown(idx,l,r);
return;
}
pushdown(idx,l,r);
int mid=l+r>>1;
if(qr<=mid) upd(idx<<1,l,mid,ql,qr,x);
else if(ql>mid) upd(idx<<1|1,mid+1,r,ql,qr,x);
else upd(idx<<1,l,mid,ql,qr,x),upd(idx<<1|1,mid+1,r,ql,qr,x);
seg[idx]=(seg[idx<<1]+seg[idx<<1|1]);
}
inline ll query(int idx,int l,int r,int ql,int qr){
pushdown(idx,l,r);
if(ql<=l and r<=qr) return seg[idx];
int mid=l+r>>1;
if(qr<=mid) return query(idx<<1,l,mid,ql,qr);
else if(ql>mid) return query(idx<<1|1,mid+1,r,ql,qr);
return query(idx<<1,l,mid,ql,qr)+query(idx<<1|1,mid+1,r,ql,qr);
}
inline ll query(int l,int r){
if(l>r) return 0;
return query(1,0,n-1,l,r);
}
inline void upd(int l,int r,ll x){
if(l>r) return;
upd(1,0,n-1,l,r,x);
}
}seg;
vector<pii> v[maxn];
pii mn[maxn];
int l[maxn],r[maxn];
vector<pair<int,pii>> ed;
void dfs(int u,int p){
for(auto x:v[u]){
if(x.f==p) continue;
MNTO(mn[x.f],mn[u]);
MNTO(mn[x.f],make_pair(ed[x.s].f,x.s));
dfs(x.f,u);
}
}
int main(){
ios::sync_with_stdio(false),cin.tie(0);
int n,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;
REP(asd,m){
auto x=ed[asd];
int a=x.s.f,b=x.s.s;
REP(i,n) mn[i]={INF,INF};
dfs(a,-1);
v[a].pb({b,asd}),v[b].pb({a,asd});
pii z=mn[b];
if(z.f==INF){
l[asd]=1;
continue;
}
int mid=(z.f+x.f+1)/2;
r[z.s]=mid-1;
l[asd]=mid;
a=ed[z.s].s.f,b=ed[z.s].s.s;
v[a].erase(find(ALL(v[a]),make_pair(b,z.s)));
v[b].erase(find(ALL(v[b]),make_pair(a,z.s)));
}
REP(i,m){
if(!r[i]) r[i]=1e9;
//cout<<l[i]<<' '<<r[i]<<'\n';
turn.pb({l[i],{-1,ed[i].f}});
turn.pb({ed[i].f,{2,-2*ed[i].f}});
turn.pb({r[i]+1,{-1,ed[i].f}});
}
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';
}
}
Compilation message (stderr)
reconstruction.cpp: In member function 'void segt::pushdown(int, int, int)':
reconstruction.cpp:100:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
100 | int mid=l+r>>1;
| ~^~
reconstruction.cpp: In member function 'void segt::upd(int, int, int, int, int, ll)':
reconstruction.cpp:115:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
115 | int mid=l+r>>1;
| ~^~
reconstruction.cpp: In member function 'll segt::query(int, int, int, int, int)':
reconstruction.cpp:124:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
124 | int mid=l+r>>1;
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |