This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define int long long
#define ff first
#define ss second
template<class T> bool umin(T& a, const T b) { if(a > b) { a = b; return 1; } return 0; }
template<class T> bool umax(T& a, const T b) { if(a < b) { a = b; return 1; } return 0; }
const int mod = 1e9+7;
const int N = 1e5+5;
int n, m, q, mn[N], dis[N];
vector<pair<int, int>> edges[N];
int tree[3*N];
array<int, 3> tt[N];
void sett(int i, int v, int lx = 0, int rx = m - 1, int x = 1){
if(lx == rx) { tree[x] = v; return; }
int m = (lx + rx)>>1;
if(i <= m) sett(i, v, lx, m, x<<1);
else sett(i, v, m+1, rx, x<<1|1);
tree[x] = min(tree[x<<1], tree[x<<1|1]);
}
int flx(int v, int lx, int rx, int x){
if(lx == rx) return lx;
int m = (lx + rx)>>1;
if(tree[x<<1] < v) return flx(v, lx, m, x<<1);
else return flx(v, m+1, rx, x<<1|1);
}
int find_l(int l, int r, int v, int lx = 0, int rx = m-1, int x = 1){
if(lx > r || rx < l) return m;
if(lx >= l && rx <= r){
if(tree[x] < v) return flx(v, lx, rx, x);
if(tree[x] >= v) return m;
} int m = (lx + rx)>>1;
return min(find_l(l, r, v, lx, m, x<<1), find_l(l, r, v, m+1, rx, x<<1|1));
}
int frx(int v, int lx, int rx, int x){
if(lx == rx) return lx;
int m = (lx + rx)>>1;
if(tree[x<<1|1] < v) return frx(v, m+1, rx, x<<1|1);
else return frx(v, lx, m, x<<1);
}
int find_r(int l, int r, int v, int lx = 0, int rx = m-1, int x = 1){
if(lx > r || rx < l) return -1;
if(lx >= l && rx <= r){
if(tree[x] < v) return frx(v, lx, rx, x);
if(tree[x] >= v) return -1;
} int m = (lx + rx)>>1;
return max(find_r(l, r, v, lx, m, x<<1), find_r(l, r, v, m+1, rx, x<<1|1));
}
/*
4 3
1 2 3
2 3 4
3 4 4
3
2 1 3
2 2 3
2 3 4
*/
vector<int> is[N];
int par[N], sz[N], rr[N];
int f(int x) { return (x == par[x] ? x : par[x] = f(par[x])); }
void merge(int a, int b){
a = f(a), b = f(b);
if(a == b) return;
if(sz[a] > sz[b]) swap(a, b);
par[b] = a, sz[a] += sz[b];
}
/*
7 8
1 2 5
1 6 5
2 3 5
2 7 5
3 4 5
4 5 5
5 6 5
6 7 5
7
2 1 6
2 1 2
2 2 2
2 2 4
2 4 2
2 1 1
2 1 3
*/
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i=0;i<m;i++){
int a, b, c; cin>>a>>b>>c;
edges[a].pb({b, c}), edges[b].pb({a, c});
tt[i] = {a, b, c};
} cin>>q;
//if(n <= 1000 && m <= 1000 && q <= 10000){
//while(q--){
//int t; cin>>t;
//if(t == 1){
//int br, w; cin>>br>>w, br--;
//int a = tt[br][0], b = tt[br][1], c = tt[br][2]; tt[br][2] = w;
//for(auto& x : edges[a]) if(x.ff == b && x.ss == c) { x.ss = w; break; }
//for(auto& x : edges[b]) if(x.ff == a && x.ss == c) { x.ss = w; break; }
//} else {
//int u, w; cin>>u>>w;
//for(int i=1;i<=n;i++) mn[i] = -mod;
//priority_queue<pair<int, int>> qq;
//mn[u] = mod; qq.push({mod, u});
//while(!qq.empty()){
//auto u = qq.top(); qq.pop();
//if(mn[u.ss] > u.ff) continue;
//for(auto x : edges[u.ss])
//if(umax(mn[x.ff], min(mn[u.ss], x.ss))) qq.push({mn[x.ff], x.ff});
//} int rr = 0;
//for(int i=1;i<=n;i++) if(mn[i] >= w) rr++;
//cout<<rr<<"\n";
//}
//} return 0;
//}
//bool tree = 1;
//for(int i=0;i<m;i++) tree &= (tt[i][0] == (i+1) && tt[i][1] == (i+2));;
//if(tree && n == m + 1){
//for(int i=0;i<m;i++) sett(i, tt[i][2]);
//while(q--){
//int t; cin>>t;
//if(t == 1){
//int i, v; cin>>i>>v;
//sett(i - 1, v);
//} else {
//int i, w; cin>>i>>w, i--;
//int inl = -1, inr = m;
//if(0 <= i-1) inl = find_r(0, i - 1, w);
//if(i <= m-1) inr = find_l(i, m - 1, w);
//cout<<inr - inl<<"\n";
//}
//}
//return 0;
//} if(n == m-1){
//} else {
vector<array<int, 3>> off, ofq;
for(int i=0;i<m;i++) off.pb({tt[i][2], tt[i][0], tt[i][1]});
sort(off.begin(), off.end());
for(int i=0;i<q;i++){
int t, u, w; cin>>t>>u>>w;
ofq.pb({w, u, i});
} sort(ofq.begin(), ofq.end());
int l = 0;
//cout<<"\n";
//for(auto x : off) cout<<x[0]<<" "<<x[1]<<" "<<x[2]<<"\n";
//cout<<"\n";
//for(auto x : ofq) cout<<x[0]<<" "<<x[1]<<" "<<x[2]<<"\n";
//cout<<"\n";
for(int i=0;i<m;){
while(l < q && ofq[l][0] <= off[i][0]) is[i].pb(l), l++;
int j = i;
while(j < m && off[j][0] == off[i][0]) j++;
i = j;
} for(int i=1;i<=n;i++) par[i] = i, sz[i] = 1;
while(l < q) rr[ofq[l][2]] = 1, l++;
for(int i=m-1;i>=0;i--){
int j = i;
while(j >= 0 && off[j][0] == off[i][0]) merge(off[j][1], off[j][2]), j--;
i = j+1;
for(auto x : is[i]){
int in = ofq[x][2], u = ofq[x][1];
rr[in] = sz[f(u)];
}
}
//for(int i=0;i<q;i++) assert(rr[i]);
for(int i=0;i<q;i++) cout<<rr[i]<<" ";
//}
}
# | 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... |