Submission #972563

#TimeUsernameProblemLanguageResultExecution timeMemory
972563panBridges (APIO19_bridges)C++17
73 / 100
3008 ms15768 KiB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef __int128  sll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> piii;
ll const blk = 1000;
ll n, m, q, t, l, r, counter;
ll par[100005], siz[100005], ans[100005];
ll weight[100005];
pii queries[100005];
pi co[100005];
bool ban[100005];
ll newweight[100005];
vector<ll> op;

void init()
{
	for (ll i=1; i<=n; ++i) par[i] = i;
	fill(siz+1, siz+n+1, 1);
	op.clear();
}

inline ll find(ll x)
{
	while (par[x]!=x) x= par[x];
	return x;
}

void unite(ll x, ll y)
{
	//show2(x,y);
	x = find(x), y = find(y);
	if (x==y) return;
	if (siz[x] > siz[y]) swap(x,y); // only union by rank work
	//show2(x,y);
	op.pb(x);
	counter++;
	siz[y]+=siz[x];
	par[x] = par[y];
	//show(siz[y]);
}

void rollback(ll x)
{
	//show(x);
	while (op.size() && x--) 
	{
		//show(op.back());
		ll now = op.back();
		op.pop_back();
		siz[par[now]] -= siz[now];
		par[now]= now;
	}
}



int main()
{
	input2(n, m);
	for (ll i=1; i<=m; ++i) input3(co[i].f, co[i].s, weight[i]);
	input(q);
	for (ll i=0; i<q; ++i) {input3(queries[i].f, queries[i].s.f, queries[i].s.s); if (queries[i].f==2) swap(queries[i].s.f, queries[i].s.s); }
	ll ind = -1;
	fill(ban+1, ban+m+1, 0);
	while (ind+1<q)
	{
		init();
		ll start = ind+1, end = min(ind+blk, q-1);
		vector<pi> edges, answer;
		vector<ll> update;
		for (ll i=start; i<=end; ++i)
		{
			if (queries[i].f==1) {ban[queries[i].s.f] = 1; update.pb(i);}
			else {answer.pb(mp(queries[i].s.f, i));}
		}
		for (ll i=1; i<=m; ++i) if (!ban[i]) edges.pb(mp(weight[i], i));
		sort(edges.begin(), edges.end(), greater<pi>());
		sort(answer.begin(), answer.end(), greater<pi>());
		ll slide = -1;
		for (pi u: answer)
		{
			//show2(u.f, u.s);
			ll where = queries[u.s].s.s;
			while (slide+1<edges.size() && edges[slide+1].f>=u.f)
			{
				slide++;
				ll i = edges[slide].s;
				unite(co[i].f, co[i].s);
				//show(i);
			}
			counter = 0;
			unordered_map<ll, ll> mapp;
			for (ll x: update)
			{
				ll which = queries[x].s.f;
				if (x>u.s && mapp.find(which)==mapp.end()) mapp[which] = weight[which];
				else if (x<u.s) mapp[which] = queries[x].s.s;
			}
			for (auto it = mapp.begin(); it!=mapp.end(); ++it) if (it->s>=u.f) {unite(co[it->f].f, co[it->f].s);}
			//show2(where, find(where));
			ans[u.s] = siz[find(where)];
			rollback(counter);
		}
		for (ll x: update) {weight[queries[x].s.f] = queries[x].s.s, ban[queries[x].s.f] = 0;}
		ind+=blk;
	}
	for (ll i=0; i<q; ++i) if (queries[i].f==2) { print(ans[i], '\n');}
	return 0;
}

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:113:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |    while (slide+1<edges.size() && edges[slide+1].f>=u.f)
      |           ~~~~~~~^~~~~~~~~~~~~
bridges.cpp:13:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define input2(x, y) scanf("%lld%lld", &x, &y);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~
bridges.cpp:88:2: note: in expansion of macro 'input2'
   88 |  input2(n, m);
      |  ^~~~~~
bridges.cpp:14:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 | #define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
      |                         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:89:26: note: in expansion of macro 'input3'
   89 |  for (ll i=1; i<=m; ++i) input3(co[i].f, co[i].s, weight[i]);
      |                          ^~~~~~
bridges.cpp:12:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | #define input(x) scanf("%lld", &x);
      |                  ~~~~~^~~~~~~~~~~~
bridges.cpp:90:2: note: in expansion of macro 'input'
   90 |  input(q);
      |  ^~~~~
bridges.cpp:14:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 | #define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
      |                         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:91:26: note: in expansion of macro 'input3'
   91 |  for (ll i=0; i<q; ++i) {input3(queries[i].f, queries[i].s.f, queries[i].s.s); if (queries[i].f==2) swap(queries[i].s.f, queries[i].s.s); }
      |                          ^~~~~~
#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...