Submission #647432

#TimeUsernameProblemLanguageResultExecution timeMemory
647432ghostwriterBridges (APIO19_bridges)C++14
59 / 100
2924 ms524288 KiB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
struct Edge {
	int u, v, w;
	Edge() {}
	Edge(int u, int v, int w) : u(u), v(v), w(w) {}
};
struct Query {
	int t, a, b;
	Query() {}
	Query(int t, int a, int b) : t(t), a(a), b(b) {}
};
struct Operation {
	int x, y, px, py, sx, sy;
	Operation() {}
	Operation(int x, int y, int px, int py, int sx, int sy) : x(x), y(y), px(px), py(py), sx(sx), sy(sy) {}
};
const int N = 5e4 + 5;
const int M = 1e5 + 5;
const int Mx2 = 2e5 + 5;
const int D = 450;
int n, m, q, p[N], s[N], e1[M], ans[M];
Edge e[M];
Query query[M];
vi v, d[Mx2];
stack<Operation> s1;
int getpos(int x) { return lb(all(v), x) - v.bg(); }
int getp(int i) { return i == p[i]? i : getp(p[i]); }
bool join(int x, int y) {
	x = getp(x);
	y = getp(y);
	s1.push(Operation(x, y, p[x], p[y], s[x], s[y]));
	if (x == y) return 0;
	if (s[x] < s[y]) swap(x, y);
	p[y] = p[x];
	s[x] += s[y];
	return 1;
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n >> m;
    FOR(i, 1, m) {
    	cin >> e[i].u >> e[i].v >> e[i].w;
    	v.pb(e[i].w);
    }
    cin >> q;
    v.pb(-1);
    FOR(i, 1, q) {
    	cin >> query[i].t >> query[i].a >> query[i].b;
    	v.pb(query[i].b);
    }
    sort(all(v));
    FOR(i, 1, m) e[i].w = getpos(e[i].w);
    FOR(i, 1, q) query[i].b = getpos(query[i].b);
    FOR(i, 1, q) {
    	int j = i;
    	WHILE(j <= q && j / D == i / D) ++j;
    	--j;
    	vi cedge, uedge;
    	FOR(z, i, j)
    		if (query[z].t == 2) d[query[z].b].pb(z);
    		else uedge.pb(query[z].a); 
    	vi a;
    	FOR(z, 1, sz(v) - 1) {
    		EACH(x, d[z]) a.pb(x);
    		d[z].clear();
    	}
    	FOR(z, 1, n) {
    		p[z] = z;
    		s[z] = 1;
    	}
    	EACH(z, uedge) e1[z] = 1;
    	FOR(z, 1, m)
    		if (!e1[z])
    			d[e[z].w].pb(z);
    	EACH(z, uedge) e1[z] = 0;
    	FOR(z, 1, sz(v) - 1) {
    		EACH(x, d[z]) cedge.pb(x);
    		d[z].clear();
    	}
    	int cur = sz(cedge) - 1;
    	FSN(z, sz(a)) {
    		int qpos = a[z];
    		WHILE(cur >= 0 && e[cedge[cur]].w >= query[qpos].b) {
    			int epos = cedge[cur];
    			join(e[epos].u, e[epos].v);
    			--cur;
    		}
    		FOR(x, i, qpos)
    			if (query[x].t == 1)
    				e1[query[x].a] = query[x].b;
    		int tupd = 0;
    		EACH(x, uedge) {
    			int w = (e1[x]? e1[x] : e[x].w);
    			if (w >= query[qpos].b) {
    				join(e[x].u, e[x].v);
    				++tupd;
    			}
    		}
    		ans[qpos] = s[getp(query[qpos].a)];
    		FRN(x, tupd) {
    			Operation &cur = s1.top();
    			p[cur.x] = cur.px;
    			p[cur.y] = cur.py;
    			s[cur.x] = cur.sx;
    			s[cur.y] = cur.sy;
    			s1.pop();
    		}
    		FOR(x, i, qpos)
    			if (query[x].t == 1)
    				e1[query[x].a] = 0;
    	}
    	FOR(z, i, j)
    		if (query[z].t == 1)
    			e[query[z].a].w = query[z].b;
    	i = j;
    }
    FOR(i, 1, q)
    	if (ans[i])
    		cout << ans[i] << '\n';
    return 0;
}
/*
3 4
1 2 5
2 3 2
3 1 4
2 3 8
5
2 1 5
1 4 1
2 2 5
1 1 1
2 3 2
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:92:5: note: in expansion of macro 'FOR'
   92 |     FOR(i, 1, m) {
      |     ^~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:98:5: note: in expansion of macro 'FOR'
   98 |     FOR(i, 1, q) {
      |     ^~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:103:5: note: in expansion of macro 'FOR'
  103 |     FOR(i, 1, m) e[i].w = getpos(e[i].w);
      |     ^~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:104:5: note: in expansion of macro 'FOR'
  104 |     FOR(i, 1, q) query[i].b = getpos(query[i].b);
      |     ^~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:105:5: note: in expansion of macro 'FOR'
  105 |     FOR(i, 1, q) {
      |     ^~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:110:6: note: in expansion of macro 'FOR'
  110 |      FOR(z, i, j)
      |      ^~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:114:6: note: in expansion of macro 'FOR'
  114 |      FOR(z, 1, sz(v) - 1) {
      |      ^~~
bridges.cpp:36:31: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
bridges.cpp:115:7: note: in expansion of macro 'EACH'
  115 |       EACH(x, d[z]) a.pb(x);
      |       ^~~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:118:6: note: in expansion of macro 'FOR'
  118 |      FOR(z, 1, n) {
      |      ^~~
bridges.cpp:36:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
bridges.cpp:122:6: note: in expansion of macro 'EACH'
  122 |      EACH(z, uedge) e1[z] = 1;
      |      ^~~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:123:6: note: in expansion of macro 'FOR'
  123 |      FOR(z, 1, m)
      |      ^~~
bridges.cpp:36:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
bridges.cpp:126:6: note: in expansion of macro 'EACH'
  126 |      EACH(z, uedge) e1[z] = 0;
      |      ^~~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:127:6: note: in expansion of macro 'FOR'
  127 |      FOR(z, 1, sz(v) - 1) {
      |      ^~~
bridges.cpp:36:31: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
bridges.cpp:128:7: note: in expansion of macro 'EACH'
  128 |       EACH(x, d[z]) cedge.pb(x);
      |       ^~~~
bridges.cpp:35:28: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   35 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
      |                            ^
bridges.cpp:132:6: note: in expansion of macro 'FSN'
  132 |      FSN(z, sz(a)) {
      |      ^~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:139:7: note: in expansion of macro 'FOR'
  139 |       FOR(x, i, qpos)
      |       ^~~
bridges.cpp:36:31: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
bridges.cpp:143:7: note: in expansion of macro 'EACH'
  143 |       EACH(x, uedge) {
      |       ^~~~
bridges.cpp:34:28: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
   34 | #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
      |                            ^
bridges.cpp:151:7: note: in expansion of macro 'FRN'
  151 |       FRN(x, tupd) {
      |       ^~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:159:7: note: in expansion of macro 'FOR'
  159 |       FOR(x, i, qpos)
      |       ^~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:163:6: note: in expansion of macro 'FOR'
  163 |      FOR(z, i, j)
      |      ^~~
bridges.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bridges.cpp:168:5: note: in expansion of macro 'FOR'
  168 |     FOR(i, 1, q)
      |     ^~~
#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...