제출 #683253

#제출 시각아이디문제언어결과실행 시간메모리
683253Nursik다리 (APIO19_bridges)C++14
13 / 100
3061 ms53380 KiB
#include <stdio.h> #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define ll long long #define pb push_back #define mp make_pair #define f first #define s second #define ld long double #define bug cout << "bug\n"; const ll maxn = 1e6 + 1, maxm = 2e2 + 1; const ll mod = 1e9 + 7, inf = 1e9, block = 550, hb = 126067, base = 1000050017, biginf = 5e18; const ld eps = 1e-15; using namespace std; int subtask1 = 1, subtask2 = 1, subtask4 = 1; int n, m, q; int p[maxn], sz[maxn]; int get(int v){ if (v == p[v]) return v; return p[v] = get(p[v]); } void unite(int a, int b){ a = get(a), b = get(b); if (a == b) return; if (sz[a] > sz[b]) swap(a, b); p[a] = b; sz[b] += sz[a]; } int u[maxn], v[maxn], d[maxn], used[maxn], t[maxn * 4], ans[maxn]; int type[maxn], x[maxn], y[maxn]; vector<pair<int, int>> g[maxn]; void upd(int pos, int val, int v = 1, int tl = 1, int tr = n - 1){ if (tl == tr){ t[v] = val; return; } int tm = (tl + tr) / 2; if (pos <= tm) upd(pos, val, v + v, tl, tm); else upd(pos, val, v + v + 1, tm + 1, tr); t[v] = min(t[v + v], t[v + v + 1]); } int get(int l, int r, int v = 1, int tl = 1, int tr = n - 1){ if (l <= tl && tr <= r) return t[v]; if (l > tr || r < tl) return inf; int tm = (tl + tr) / 2; return min(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr)); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m; subtask2 &= (m == n - 1); for (int i = 1; i <= m; ++i){ cin >> u[i] >> v[i] >> d[i]; g[u[i]].pb(mp(v[i], i)); g[v[i]].pb(mp(u[i], i)); subtask2 &= (u[i] == i && v[i] == i + 1); if (subtask2 > 0){ upd(i, d[i]); } } cin >> q; for (int i = 1; i <= q; ++i){ cin >> type[i]; cin >> x[i] >> y[i]; subtask4 &= (type[i] == 2); } subtask1 &= (n <= 1000 && m <= 1000 && q <= 10000); if (subtask1){ for (int i = 1; i <= q; ++i){ if (type[i] == 1){ d[x[i]] = y[i]; } else{ for (int j = 1; j <= n; ++j){ used[j] = 0; } queue<int> q; q.push(x[i]); used[x[i]] = 1; while (q.size() > 0){ int v = q.front(); q.pop(); for (auto to : g[v]){ pair<int, int> nx = to; if (d[nx.s] >= y[i] && used[nx.f] == 0){ used[nx.f] = 1; q.push(nx.f); } } } int ans = 0; for (int j = 1; j <= n; ++j){ ans += used[j]; } cout << ans << '\n'; } } return 0; } if (subtask2){ for (int i = 1; i <= q; ++i){ if (type[i] == 1){ upd(x[i], y[i]); } else{ int kol = 1; int l = 1, r = x[i] - 1, ans = -1; while (l <= r){ int mid = (l + r) / 2; if (get(mid, x[i] - 1) >= y[i]){ r = mid - 1, ans = mid; } else{ l = mid + 1; } } if (ans != -1){ kol += (x[i] - ans); } l = x[i], r = n - 1, ans = -1; while (l <= r){ int mid = (l + r) / 2; if (get(x[i], mid) >= y[i]){ l = mid + 1, ans = mid; } else{ r = mid - 1; } } if (ans != -1){ kol += (ans - x[i] + 1); } cout << kol << '\n'; } } } if (subtask4){ vector<pair<int, int>> query, reb; for (int i = 1; i <= q; ++i){ query.pb(mp(y[i], i)); } for (int i = 1; i <= m; ++i){ reb.pb(mp(d[i], i)); } sort(query.begin(), query.end()); sort(reb.begin(), reb.end()); for (int i = 1; i <= n; ++i){ p[i] = i, sz[i] = 1; } int uk = 0; for (auto it : query){ pair<int, int> cur = it; while (uk < m && reb[uk].f <= cur.f){ int xu = u[reb[uk].s], yu = v[reb[uk].s]; unite(xu, yu); } ans[cur.s] = sz[get(x[cur.f])]; } for (int i = 1; i <= q; ++i){ cout << ans[i] << '\n'; } } } /* */
#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...