let's start Code

ICPC Dhaka Regional Preliminary Contest, 2019

B. The Social Network

#include <bits/stdc++.h>
using namespace std;
#define INF 1<<30
#define endl '\n'
#define maxn 100005
#define tc printf("Case %d: ", cs)
#define tcn printf("Case %d:\n", cs);
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);
// Program showing a policy-based data structure.
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;
// a new data structure defined. Please refer below
// GNU link : https://goo.gl/WVDL6g
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
new_data_set;
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z) cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl;
#define dbg4(w,x, y, z) cerr << #w << " = " << w << ", " <<#x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl;
int parent[maxn * 2], Rank[maxn];
new_data_set st[maxn];
vector<int> graph[maxn];
void solve(int v, int l, int r)
{
ll ans = 0;
if (st[v].size()) {
//multiset<int>::iterator itlow, itup;
//for (auto p : st[v]) cerr << p << " ** ";
// cerr << endl;
auto low = st[v].order_of_key(r + 1);
// if (*st[v].find_by_order(low) == r + 1) ans = -1;
auto up = st[v].order_of_key(l);
int p = low;
int q = up;
ans += (ll)(p - q);
//dbg2(p, q);
// dbg2(l, r);
//dbg(ans);
}
printf("%lld\n", ans);
}
void Initialization()
{
for (int i = 0; i <= maxn; i++)
parent[i] = i, Rank[i] = 1;
}
int find_parent(int x)
{
if (parent[x] == x)
{
return x;
}
return parent[x] = find_parent(parent[x]);
}
void make_union(int x, int y)
{
int p = find_parent(x);
int q = find_parent(y);
if (p != q) {
if (Rank[p] < Rank[q])
swap(p, q);
parent[q] = p;
Rank[p] += Rank[q];
for (auto x : st[q])
st[p].insert(x);
}
}
bool isUnion(int x, int y)
{
return find_parent(x) == find_parent(y);
}
int main()
{
///*
//double start_time = clock();
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
//*/
int T;
scanf("%d", &T);
for (int cs = 1; cs <= T; cs++) {
int n, q;
Initialization();
scanf("%d %d", &n, &q);
for (int i = 0; i <= n; i++) {
//graph[i].clear();
st[i].clear();
}
printf("Case %d:\n", cs);
// cerr << "---------\n";
for (int i = 0; i < q; i++) {
int x;
scanf("%d", &x);
if (x == 0) {
int u, v;
scanf("%d %d", &u, &v);
make_union(u, v);
}
else if (x == 1) {
int u, t;
scanf("%d %d", &u, &t);
u = find_parent(u);
st[u].insert(t);
}
else {
int u, l, r;
scanf("%d %d %d", &u, &l, &r);
//dbg3(u, l, r);
//bfs(u, l, r);
int x = find_parent(u);
// dbg(x);
solve(x, l, r);
//cerr << "------------\n";
}
}
}
//double end_time = clock();
//printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000);
return 0;
}
view raw B.cpp hosted with ❤ by GitHub

Share:

Related Posts:

No comments:

Post a Comment

About

let's start CODE

Popular Posts