제출 #266976

#제출 시각아이디문제언어결과실행 시간메모리
266976Thistle다리 (APIO19_bridges)C++14
0 / 100
3077 ms81156 KiB
#pragma GCC target ("avx2") #pragma GCC optimize("O3") #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_]; ll readi() { char c; bool st = 0, neg = 0; ll ret = 0; while (true) { c = getchar(); if ((c - '0' < 0 || c - '0' > 9) && c != '-' && !st) continue; if ((c - '0' < 0 || c - '0' > 9) && c != '-' && st) break; if (st) ret *= 10; st = 1; if (c == '-') neg = 1; else ret += c - '0'; } if (!neg) return ret; return -ret; } //-------------------------------------------------------------- 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; set<int> 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; } for (auto& g : ed) { e[edge[g].a].pb(H{ edge[g].b,g }); e[edge[g].b].pb(H{ edge[g].a,g }); } 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.find(a), b = uf.find(b); if (a == b) continue; uf.unite(b, a); if (uf.find(a) == 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; queue<int>q; q.push(uf.find(g.fs)); int sum = 0; //今回はこれ以上でないといけない while (!q.empty()) { int x = q.front(); q.pop(); if (b[x]) continue; b[x] = 1; vis.pb(x); sum += uf[x].sc; for (auto& r : e[numb[x]]) { r.fs = uf.find(r.fs); if (b[r.fs] || (*prev(partition_point(all(edge[r.sc].v), [g](H a)->bool {return a.fs <= g.zz; }))).sc < g.yy) continue; //訪れたことがない、二分探索をした時のコストが自分よりも少なくない q.push(r.fs); } } ans[g.zz] = sum; for (auto& r : vis) b[r] = 0; } } signed main() { n = readi(), m = readi(); int t, a, b, c; edge.resize(m); rep(i, m) { a = readi(), b = readi(), c = readi(); a--; b--; idx.pb(c); edge[i] = edg{ a,b,c,vec<H>(1,H{0,c}) }; } q = readi(); qry.resize(q); rep(i, q) { t = readi(), a = readi(), b = readi(); 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)].insert(i); } int sq = sqrt(q) * 2; 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); numbers[getidx(idx, edge[qry[j].yy].cost)].erase(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)].insert(qry[j].yy); } } } rep(i, q) { if (qry[i].xx == 2) printf("%lld\n", ans[i]); } }

컴파일 시 표준 에러 (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...