Submission #266769

#TimeUsernameProblemLanguageResultExecution timeMemory
266769ThistleBridges (APIO19_bridges)C++11
13 / 100
3065 ms68336 KiB
#pragma GCC target ("avx") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _USE_MATH_DEFINES #include<iostream> #include<string> #include<queue> #include<cmath> #include<map> #include<set> #include<list> #include<iomanip> #include<vector> #include<random> #include<functional> #include<algorithm> #include<stack> #include<cstdio> #include<cstring> #include<bitset> #include<unordered_set> #include<unordered_map> #include<climits> #include<fstream> #include<complex> #include<time.h> #include<cassert> #include<functional> #include<numeric> #include<tuple> using namespace std; using ll = long long; using ld = long double; #define int long long #define all(a) (a).begin(),(a).end() #define fs first #define sc second #define xx first #define yy second.first #define zz second.second #define H pair<int, int> #define P pair<int, pair<int, int>> #define Q(i,j,k) mkp(i,mkp(j,k)) #define rng(i,s,n) for(int i = (s) ; i < (n) ; i++) #define rep(i,n) rng(i, 0, (n)) #define mkp make_pair #define vec vector #define vi vec<int> #define pb emplace_back #define siz(a) (int)(a).size() #define crdcomp(b) sort(all((b)));(b).erase(unique(all((b))),(b).end()) #define getidx(b,i) (lower_bound(all(b),(i))-(b).begin()) #define ssp(i,n) (i==(int)(n)-1?"\n":" ") #define ctoi(c) (int)(c-'0') #define itoc(c) (char)(c+'0') #define cyes printf("Yes\n") #define cno printf("No\n") #define cdf(n) int quetimes_=(n);rep(qq123_,quetimes_) #define gcj printf("Case #%lld: ",qq123_+1) #define readv(a,n) a.resize(n,0);rep(i,(n)) a[i]=read() #define found(a,x) (a.find(x)!=a.end()) //#define endl "\n" constexpr int mod = (ll)1e9 + 7; constexpr int Mod = 998244353; constexpr ld EPS = 1e-10; constexpr ll inf = (ll)3 * 1e18; constexpr int Inf = (ll)15 * 1e8; constexpr int dx[] = { -1,1,0,0 }, dy[] = { 0,0,-1,1 }; template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } ll read() { ll u, k = scanf("%lld", &u); return u; } string reads() { string s; cin >> s; return s; } H readh(short g = 0) { H u; int k = scanf("%lld %lld", &u.fs, &u.sc); if (g == 1) u.fs--, u.sc--; if (g == 2) u.fs--; return u; } bool ina(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; } bool ina(int t, int l, int r) { return l <= t && t < r; } ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; } ll popcount(ll x) { int sum = 0; for (int i = 0; i < 60; i++)if ((1ll << i) & x) sum++; return sum; } template<typename T> class csum { vec<T> v; public: csum(vec<T>& a) :v(a) { build(); } csum() {} void init(vec<T>& a) { v = a; build(); } void build() { for (int i = 1; i < v.size(); i++) v[i] += v[i - 1]; } T a(int l, int r) { if (r < l) return 0; return v[r] - (l == 0 ? 0 : v[l - 1]); }//[l,r] T b(int l, int r) { return a(l, r - 1); }//[l,r) T a(pair<int, int>t) { return a(t.first, t.second); } T b(pair<int, int>t) { return b(t.first, t.second); } }; class mint { public:ll v; mint(ll v = 0) { s(v % mod + mod); } constexpr static int mod = (ll)1e9 + 7; constexpr static int fn_ = (ll)2e6 + 5; static mint fact[fn_], comp[fn_]; mint pow(int x) const { mint b(v), c(1); while (x) { if (x & 1) c *= b; b *= b; x >>= 1; } return c; } inline mint& s(int vv) { v = vv < mod ? vv : vv - mod; return *this; } inline mint inv()const { return pow(mod - 2); } inline mint operator-()const { return mint() - *this; } inline mint& operator+=(const mint b) { return s(v + b.v); } inline mint& operator-=(const mint b) { return s(v + mod - b.v); } inline mint& operator*=(const mint b) { v = v * b.v % mod; return *this; } inline mint& operator/=(const mint b) { v = v * b.inv().v % mod; return *this; } inline mint operator+(const mint b) const { return mint(v) += b; } inline mint operator-(const mint b) const { return mint(v) -= b; } inline mint operator*(const mint b) const { return mint(v) *= b; } inline mint operator/(const mint b) const { return mint(v) /= b; } friend ostream& operator<<(ostream& os, const mint& m) { return os << m.v; } friend istream& operator>>(istream& is, mint& m) { int x; is >> x; m = mint(x); return is; } bool operator<(const mint& r)const { return v < r.v; } bool operator>(const mint& r)const { return v > r.v; } bool operator<=(const mint& r)const { return v <= r.v; } bool operator>=(const mint& r)const { return v >= r.v; } bool operator==(const mint& r)const { return v == r.v; } bool operator!=(const mint& r)const { return v != r.v; } explicit operator bool()const { return v; } explicit operator int()const { return v; } mint comb(mint k) { if (k > * this) return mint(); if (!fact[0]) combinit(); if (v >= fn_) { if (k > * this - k) k = *this - k; mint tmp(1); for (int i = v; i >= v - k.v + 1; i--) tmp *= mint(i); return tmp * comp[k.v]; } return fact[v] * comp[k.v] * comp[v - k.v]; }//nCk mint perm(mint k) { if (k > * this) return mint(); if (!fact[0]) combinit(); if (v >= fn_) { mint tmp(1); for (int i = v; i >= v - k.v + 1; i--) tmp *= mint(i); return tmp; } return fact[v] * comp[v - k.v]; }//nPk static void combinit() { fact[0] = 1; for (int i = 1; i < fn_; i++) fact[i] = fact[i - 1] * mint(i); comp[fn_ - 1] = fact[fn_ - 1].inv(); for (int i = fn_ - 2; i >= 0; i--) comp[i] = comp[i + 1] * mint(i + 1); } }; mint mint::fact[fn_], mint::comp[fn_]; //-------------------------------------------------------------- class unionfind { public: int size = 0; int pa[60000]; void init(int n) { size = n; for (int i = 0; i <= size; i++) pa[i] = -1; } int find(int x) { if (pa[x] < 0) return x; return pa[x] = find(pa[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (pa[x] > pa[y]) swap(x, y); pa[x] += pa[y]; pa[y] = x; return true; } bool same(int x, int y) { return find(x) == find(y); } bool isroot(int x) { return x == find(x); } int sz(int x) { return -pa[find(x)]; } H operator[](int x) { x = find(x); return H{ x,-pa[x] }; } }; //--------------------------------------------------------------------- int n, m, q; struct edg { int a, b, cost; vec<H>v;//時刻、コスト }; vec<edg>edge; bool chg[200000]; int numb[60000]; vec<P>qry; int ans[200000]; vec<H>e[600000]; bool b[600000]; unionfind uf; vi idx; vi numbers[300000]; void solve(vec<P>&v, vi& ed) { sort(all(v), [](P a, P b) {return a.yy > b.yy; }); //大きさが大きい順にソートをした rep(i, n) { e[i].clear(); numb[i] = i; } rep(i, siz(ed)) { e[edge[ed[i]].a].pb(H{ edge[ed[i]].b,ed[i] }); e[edge[ed[i]].b].pb(H{ edge[ed[i]].a,ed[i] }); } uf.init(n); int cur = siz(idx) - 1; for (auto& g : v) { //gは今回のクエリ 辿っていきます while (cur >= 0 && idx[cur] >= g.yy) { for (auto& rt : numbers[cur]) { int k = rt; if (chg[k]) continue; int a = edge[k].a, b = edge[k].b; a = uf[a].fs, b = uf[b].fs; if (a == b) continue; uf.unite(b, a); if (uf[a].fs == a) swap(a, b); //aが親を変えられたうらみのある人 if (siz(e[numb[b]]) > siz(e[numb[a]])) { for (auto& g : e[numb[a]]) { e[numb[b]].pb(g); } } else { for (auto& g : e[numb[b]]) { e[numb[a]].pb(g); } numb[b] = numb[a]; } } cur--; }//グラフの縮約が完了した vi vis; function<int(int, int, int)> dfs = [&](int x, int cost, int num)->int { //今回はこれ以上でないといけない int sum = 0; b[x] = 1; vis.pb(x); sum += uf[x].sc; for (auto& g : e[numb[x]]) { g.fs = uf[g.fs].fs; int ttt = (*prev(partition_point(all(edge[g.sc].v), [num](H a)->bool {return a.fs <= num; }))).sc; if (b[g.fs] || ttt < cost) continue; //訪れたことがない、二分探索をした時のコストが自分よりも少なくない sum += dfs(g.fs, cost, num); } return sum; }; ans[g.zz] = dfs(uf[g.xx].fs, g.yy, g.zz); for (auto r : vis) b[r] = 0; } } signed main() { n = read(), m = read(); int t, a, b, c; edge.resize(m); rep(i, m) { a = read(), b = read(), c = read(); a--; b--; idx.pb(c); edge[i] = edg{ a,b,c,vec<H>(1,H{0,c}) }; } q = read(); qry.resize(q); rep(i, q) { t = read(), a = read(), b = read(); a--; qry[i] = Q(t, a, b); if (t == 1) { edge[a].v.pb(H{ i,b }); idx.pb(b); } } crdcomp(idx); rep(i, m) { numbers[getidx(idx, edge[i].cost)].pb(i); } int sq = sqrt(q) * 0.5; vec<P>qq; //どっちのクエリか、重量がどうなっているのか、次の情報 vi p;//道 for (int i = 0; i < q; i += sq) { qq.clear(); p.clear(); for (int j = i; j < i + sq && j < q; j++) { if (qry[j].xx == 1) { chg[qry[j].yy] = 1; p.pb(qry[j].yy); } else { qq.pb(Q(qry[j].yy, qry[j].zz, j)); } } solve(qq, p); for (int j = i; j < i + sq && j < q; j++) { if (qry[j].xx == 1) { chg[qry[j].yy] = 0; edge[qry[j].yy].cost = qry[j].zz; numbers[getidx(idx, qry[j].zz)].pb(qry[j].yy); } } rep(j, siz(idx)) { for (auto it = numbers[j].begin(); it != numbers[j].end(); ) { if (edge[*it].cost != idx[j]) { it = numbers[j].erase(it); continue; } it++; } } } rep(i, q) { if (qry[i].xx == 2) printf("%lld\n", ans[i]); } }

Compilation message (stderr)

bridges.cpp: In function 'll read()':
bridges.cpp:72:19: warning: unused variable 'k' [-Wunused-variable]
   72 | ll read() { ll u, k = scanf("%lld", &u); return u; }
      |                   ^
bridges.cpp: In function 'std::pair<long long int, long long int> readh(short int)':
bridges.cpp:74:33: warning: unused variable 'k' [-Wunused-variable]
   74 | H readh(short g = 0) { H u; int k = scanf("%lld %lld", &u.fs, &u.sc); if (g == 1) u.fs--, u.sc--; if (g == 2) u.fs--; return u; }
      |                                 ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...