-
Notifications
You must be signed in to change notification settings - Fork 0
/
1249B2_Books_Exchange_(hard_version).cpp
252 lines (221 loc) · 8.05 KB
/
1249B2_Books_Exchange_(hard_version).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Vidur Goel
//Codeforcees Handle: Vidurcodviz
#include<iostream>
#include<string>
#include<cmath>
#include<climits>
#include<algorithm>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<stack>
#include<chrono>
#include<random>
#include<cassert>
#include<array>
#include<bitset>
#include<complex>
#include<cstring>
#include<functional>
#include<iomanip>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<utility>
#include<string_view>
#include<deque>
#include<iterator>
#include<sstream>
using namespace std;
using namespace chrono;
void solve_array();
void solve_single();
void solve_mul();
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double lld;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<vl> vvl;
#define make_it_fast() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define rept(i, a, n) for (ll i = (a); i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define sor(x) sort(all(x))
#define sorr(x) sort(x.rbegin(),x.rend()) // this is in order to do sorting in descending order
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define MOD 1000000007
#define MOD1 998244353
#define PI 3.141592653589793238462
#define mset multiset<ll> // it contains multiple instances of the same value in ascending order
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repd(i,a,b) for(ll i=b-1;i>=a;i--)
#define nn endl
#define setbits(n) __builtin_popcount(n)
ll seiv[1000001]={0};
string yup="YES";
string nope="NO";
ll lmin(vl arr){return *min_element(arr.begin(),arr.end());}
ll lmax(vl arr){return *max_element(arr.begin(),arr.end());}
ll fibonacci(ll n){ll a=0;ll b=1;ll c;if(n==0 || n==1){return n;}for(ll i=2;i<n+1;i++){c=a+b;a=b;b=c;}return c;}
ll sum(vl a){ll sum=0;rep(i,0,a.size()){sum+=a[i];}return sum;}
void rev(vl &arr,ll n){rep(i,0,n){cin>>arr[i];}return;}
void prv(vl arr){rep(i,0,arr.size()){cout<<arr[i]<<" ";}cout<<nn;return;}
bool prime(ll n){rep(i,2,ceil(sqrt(n))){if(n%i==0){return false;}}return true;}
void seive(){seiv[0]=0;seiv[1]=1;for(ll i=2;i*i<1000001;i++){if(seiv[i]==0){seiv[i]=i;for(ll j=i*i;j<1000001;j=j+i){if(seiv[j]==0){seiv[j]=i;}}}}}
ll gcd(ll a,ll b){if(b==0){return a;}if(a>=b){return gcd(b,a%b);}else{return gcd(b,a);}}
ll expo(ll num,ll coef){ll res=1;while(coef!=0){if(coef%2==0){coef=coef/2;num=num*num;}else{coef=coef-1;res=res*num;}}return res;}
ll expo_mod(ll a,ll n,ll m){ll res=1;while(n!=0){if(n%2==0){a=a*a;a=a%m;n=n/2;}else{res=res*a;res=res%m;n--;}}return res;}
ll add_mod(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mul_mod(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll sub_mod(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
/*
A) double sqrt(double arg): It returns the square root of a number to type double.
B) float sqrtf(float arg): It returns the square root of a number to type float.
C) long double sqrtl(long double arg): It returns the square root of a number to type long double with more precision.
Advised to always use C) as always give correct one as other may halt in case of the big numbers
cbrt() in built function to give the cube root in float/double
abs() is used for the absolute value of a number
swap() function in c++ used to swap value of two elements of the same data type.
toupper() This function is used for converting a lowercase character to uppercase.
tolower() This function is used for converting an uppercase character to lowercase.
ceil() and floor() function
sort(vect.begin(),vect.end(), greater<int>());
reverse(vect.begin(), vect.end());
count(first_iterator, last_iterator,x) – To count the occurrences of x in vector.
find(first_iterator, last_iterator, x) – Returns an iterator to the first occurrence of x in vector and points to last address of vector ((name_of_vector).end()) if element is not present in vector
maximium value long long can take 9, 223, 372, 036, 854, 775, 807
2^63-1
i.e, length of 19 only
maximium value long long can take 18, 446, 744, 073, 709, 551, 615
2^64-1
i.e, length of 20 only
lower_bound(v.begin(), v.end(), 6) these are the syntax
upper_bound(v.begin(), v.end(), 6)
In multiset to remove all element of a same number use a.erase()
else to remove 1 lement only use ans.erase(ans.find(*it)) here it is the iterator
priority_queue<int, vector<int>, greater<int> > gquiz(arr, arr + n);
Here above is the syntax of the min_heap implementation with the help of the priority queue and here push() and pop() and top() are the main operations
priority_queue<int> gquiz(arr, arr + n);
Here above is the syntax of the max_heap implementation with the help of the priority queue and here push() and pop() and top() are the main operations
Whenever need to do the hashing always use the map which is the stl template of hashing never use the array indexing method.
map.find() function has complexity 0(logn)
map.insert function has complexity 0(1)
__builtin_popcount(n) - we use this function to count the number of 1's (set bits) in the number in binary form
__builtin_parity(n) - this is boolean function which return true if number of 1's in binary form of n are odd else returns false;
__builtin_clz(n) - eg: Binary form of 16 is 00000000 00000000 00000000 00010000 therefore will return the number of the leading zeroes in n here answer will be 27
__builtin_ctz(n) - eg: Binary form of 20 is 00000000 00000000 00000000 00010100 therefore will return the number of the trailing zeroes in n here answer will be 2
An important info about the lower_bound used in various data structures
actually if number is present they will return te iterator pointing to that number in the data structure otherwise return the
next iterator in that data structure so depends whether sorted in ascending or descending order.
An important info about the upper_bound used in various data structures
is that it will return the iterator pointing to the next iterator to which the number should be there also depends on the sorting order
*/
struct dsu{
public:
vl parent,size;
dsu(ll n){
size.resize(n+1);
parent.resize(n+1);
rep(i,0,n+1){
make_set(i);
}
}
void make_set(ll v){
parent[v]=v;
size[v]=1;
}
ll find_set(ll v){
if(v==parent[v]){
return v;
}
else{
return parent[v]=find_set(parent[v]);
}
}
void union_set(ll a,ll b){
a=find_set(a);
b=find_set(b);
if(a==b){
return;
}
else{
if(size[a]>=size[b]){
parent[b]=a;
size[a] += size[b];
}
else{
parent[a]=b;
size[b] += size[a];
}
}
}
};
bool mycompare(pll p1 ,pll p2){
if(p1.first<p2.first){return true;}
else if(p1.first==p2.first){return p1.second<p2.second;}
else{return false;}
}
void solve_mul(){
ll test;
cin>>test;
rep(i,0,test){
solve_array();
}
}
void solve_single(){
ll n;
cin>>n;
}
void solve_array(){
ll n;
cin>>n;
vl arr(n,0);
rev(arr,n);
vector<bool> visited(n,false);
dsu * ds=new dsu(n);
rep(i,0,n){
if(!visited[i]){
ll s=i+1;
ll a=s;
visited[s-1]=true;
while(arr[a-1]!=s){
a=arr[a-1];
ds->union_set(a,s);
visited[a-1]=true;
}
}
}
rep(i,0,n){
cout<<ds->size[ds->find_set(i+1)]<<" ";
}
cout<<nn;
}
void solve_graph(){
ll n,m;
cin>>n>>m;
vl a;
vvl arr(n,a);
rep(i,0,m){
ll x,y;
cin>>x>>y;
arr[x-1].pb(y);
arr[y-1].pb(x);
}
}
signed main(){
make_it_fast();
//seive();
solve_mul();
//solve_array();
//solve_single();
return 0;
}