Submission #997766

#TimeUsernameProblemLanguageResultExecution timeMemory
997766SN0WM4N다리 (APIO19_bridges)C++14
60 / 100
1629 ms11056 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sort undefined_function // To use stable_sort instead sort 
#define bpc __builtin_popcount
#define ull unsigned long long
#define ld double
#define ll long long
#define mp make_pair
#define F first
#define S second

#pragma GCC optimize("O3")

#ifdef LOCAL 
	#include "debug.h"
#else 
	#define dbg(...) 0
#endif

using namespace __gnu_pbds;
using namespace std;

typedef tree<long long, null_type, less_equal<long long>,
    rb_tree_tag, tree_order_statistics_node_update> Tree;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll INF = 9223372036854775807LL;
const ll inf = 2147483647;
const ll MOD = 1e9 + 7; //[998244353, 1e9 + 7, 1e9 + 13]
const ld PI = acos(-1);
const ll NROOT = 800;

ll binpow(ll a, ll b, ll _MOD = -1) {
	if (_MOD == -1)
		_MOD = MOD;
	ll res = 1;
	for (; b; b /= 2, a *= a, a %= _MOD)
		if (b & 1) res *= a, res %= _MOD;
	return res;
}

void set_IO(string s) {
#ifndef LOCAL
	string in  = s +  ".in";
	string out = s + ".out";
	freopen(in.c_str(),  "r",  stdin);
	freopen(out.c_str(), "w", stdout);
#endif
}

bool dataOverflow(ll a, ll b) {return (log10(a) + log10(b) >= 18);}
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a * b / gcd(a, b);}
ll ceil(ll a, ll b) {return (a + b - 1) / b;}
ll invmod(ll a) {return binpow(a, MOD - 2);}

class DSU{
	private:
		int n;
		vector<int> uf, sz;
		stack<int> s;

	public:
		DSU (int _n) {
			n = _n;
			sz.resize(n + 1, 1);
			uf.resize(n + 1, -1);
		}

		int uf_find(int x) {
			return uf[x] == -1 ? x : uf[x] = uf_find(uf[x]);
		}

		bool uf_join(int x, int y) {
			x = uf_find(x);
			y = uf_find(y);

			if (x == y) 
				return 0;

			if (y < x)
				swap(x, y);
			s.push(y);
			sz[x] += sz[y];
			uf[y] = x;
			return 1;
		}

		void rollBack(int k) {
			while (s.size() != k) {
				int x = s.top();
				s.pop();
				sz[uf_find(x)] -= sz[x];
				uf[x] = -1;
			}	
		}

		int get_size(int x) {
			return sz[uf_find(x)];
		}

		int stack_size() {
			return s.size();
		}
};

// FROM KACTL 
class RollBackDSU{
public:
	vector<int> e;
	vector<pair<int, int>> st;
	RollBackDSU(int n) : e(n, -1) {}
	int get_size(int x) {return -e[find(x)];}
       	int find(int x) { return e[x] < 0 ? x : find(e[x]);}	
	int time() { return st.size(); }

	void rollBack(int t) {
		for (int i = time(); i --> t;) 
			e[st[i].first] = st[i].second;
		st.resize(t);
	}
	
	bool join(int a, int b) {
		a = find(a); b = find(b);
		if (a == b) return 0;
		if (e[a] > e[b]) swap(a, b);
		st.push_back({a, e[a]});
		st.push_back({b, e[b]});
		e[a] += e[b]; e[b] = a;
		return 1;
	}
};

class edge {
public:
	int w, a, b;
	bool operator< (edge other) {return w < other.w;}
};

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int n, m;
	cin >> n >> m;

	vector<edge> edges(m + 1);

	for (int i = 1; i <= m; i ++) {
		int a, b, c;
		cin >> a >> b >> c;
		edges[i] = {c, a, b};
	}

	int q;
	cin >> q;

	using T = pair<int, pair<int, int>>;
	vector<vector<T>> qrys(q + 10);

	int block = -1;
	for (int i = 0; i < q; i ++) {
		if (i % NROOT == 0)
			block ++;

		int tq, a, b;
		cin >> tq >> a >> b;
		qrys[block].push_back({tq, {a, b}});
	}

	for (auto &qr : qrys) {
		// In the block I need some things
		// First lets separate operation between UPDATES and QUERIES
		// Then lets separate edges in CHANGED and UNCHANGES

		if (qr.empty())
			break;

	       	vector<bool> chg(n + 1);	
		vector<int> chgd;

		vector<pair<pair<int, int>, int>> ask;
		vector<vector<pair<int, int>>> d;

		for (auto &x : qr) {
			int tq = x.F;
			auto [a, b] = x.S;
			if (tq == 1) {	
				chg[a] = 1;
				chgd.push_back(a);
			}
		}

		vector<pair<int, int>> ans;
		vector<pair<int, int>> oedges;
		RollBackDSU aux(n + 1);

		for (auto &x : qr) {
			int tq = x.F;
			auto [a, b] = x.S;

			if (tq == 1) {
				edges[a].w = b;
			} else {
				ask.push_back(make_pair(make_pair(b, a), (int)d.size()));
				d.push_back({});
				for (auto &c : chgd) {
					if (edges[c].w >= b)
						d.back().push_back({edges[c].a, edges[c].b});		
				}
			}
		}

		stable_sort(ask.rbegin(), ask.rend());
		if (chgd.empty()) {
			for (int i = 1; i <= m; i ++) 
				oedges.push_back({edges[i].w, i});
			stable_sort(oedges.rbegin(), oedges.rend());
			
			int i = 0;
			for (auto &r : ask) {
				auto [w, x] = r.F;
				int index = r.S;

				while (i < oedges.size() && oedges[i].F >= w) {
					aux.join(edges[oedges[i].S].a, edges[oedges[i].S].b);
					i ++;
				} 
				ans.emplace_back(index, aux.get_size(x));
			}

			stable_sort(ans.begin(), ans.end());

			for (auto &x : ans)
				cout << x.S << "\n";
			continue;

		}

		for (int i = 1; i <= m; i ++) 
			if (!chg[i])
				oedges.push_back({edges[i].w, i});
		stable_sort(oedges.rbegin(), oedges.rend());

		int i = 0;
		for (auto &r : ask) {
			auto [w, x] = r.F;
			int index = r.S;

			while (i < oedges.size() && oedges[i].F >= w) {
				aux.join(edges[oedges[i].S].a, edges[oedges[i].S].b);
				i ++;
			} 

			int K = aux.time();
			for (auto &e : d[index]) 
				aux.join(e.F, e.S);

			ans.emplace_back(index, aux.get_size(x));
			aux.rollBack(K);
		}

		stable_sort(ans.begin(), ans.end());
		for (auto &x : ans)
			cout << x.S << "\n";
	}

	return 0;
}

Compilation message (stderr)

bridges.cpp: In member function 'void DSU::rollBack(int)':
bridges.cpp:91:20: warning: comparison of integer expressions of different signedness: 'std::stack<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   91 |    while (s.size() != k) {
      |           ~~~~~~~~~^~~~
bridges.cpp: In function 'int32_t main()':
bridges.cpp:188:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  188 |    auto [a, b] = x.S;
      |         ^
bridges.cpp:201:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  201 |    auto [a, b] = x.S;
      |         ^
bridges.cpp:223:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  223 |     auto [w, x] = r.F;
      |          ^
bridges.cpp:226:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  226 |     while (i < oedges.size() && oedges[i].F >= w) {
      |            ~~^~~~~~~~~~~~~~~
bridges.cpp:248:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  248 |    auto [w, x] = r.F;
      |         ^
bridges.cpp:251:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  251 |    while (i < oedges.size() && oedges[i].F >= w) {
      |           ~~^~~~~~~~~~~~~~~
bridges.cpp: In function 'void set_IO(std::string)':
bridges.cpp:47:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |  freopen(in.c_str(),  "r",  stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:48:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |  freopen(out.c_str(), "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#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...