-
Notifications
You must be signed in to change notification settings - Fork 0
/
googer.cxx
245 lines (240 loc) · 7.27 KB
/
googer.cxx
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
#include <algorithm>
#include <array>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <numeric>
#include <utility>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <bitset>
#include <cmath>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cstdint>
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-label"
#pragma GCC diagnostic ignored "-Wunused-label"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wmissing-braces"
#pragma GCC diagnostic ignore "-Wmaybe-uninitialized"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#pragma GCC optimize("Ofast", "unroll-loops")
#ifdef __linux__
# include<unistd.h>
#endif
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using str=string;
#define TRUE 1
#define FALSE 0
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define eb emplace_back
#define RANGE(a,b) for(int i=a;i<=b;i++)
#define PI 3.14159265358979323846
#ifdef LOCAL_JUDGE_HOST
# define __trace(x) cerr<<"["<<__LINE__<<"] "<<#x<<"@"<<&x<<"="<<x<<endl;
#else
# define __trace(x)
#endif
namespace judge
{
template<typename T>
inline void partial_sum(vector<T> x)
{
partial_sum(all(x),x.begin());
}
inline void setIO(str name="")
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
if (int(name.size()))
{
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
}
int gcd(int a,int b)
{
return a==0||a==b?b:b==0||b%a==0?a:a>b?gcd(b,a):gcd(a,b%a);
}
inline int low_bit(int x)
{
return x&(-x);
}
#ifdef __linux__
ostream& operator<<(ostream& os,const __int128_t& v)
{
if (!v)
os<<"0";
__int128_t tmp=v<0?(os<<"-",-v):v;
string s;
while (tmp)
{
s+='0'+(tmp%10);
tmp/=10;
}
return reverse(all(s)),os<<s;
}
ostream& operator<<(ostream& os,const __uint128_t& v)
{
if (!v)
os<<"0";
__uint128_t tmp=v;
string s;
while (tmp)
{
s+='0'+(tmp%10);
tmp/=10;
}
return reverse(all(s)),os<<s;
}
#endif
};
using namespace judge;
namespace generics
{
template<typename T>
class Graph
{
public:
struct Edge
{
int from;
int to;
T weight;
Edge(int from,int to,T weight) :from(from),to(to),weight(weight)
{
}
};
vector<Edge> edges;
vector<vector<int>> ver;
int n;
Graph(int n) :n(n)
{
ver.resize(n);
}
virtual int addEdge(const Edge& edge)=0;
};
template<typename T>
class UndirectedGraph :public Graph<T>
{
public:
UndirectedGraph(int n) :Graph<T>(n)
{
}
int addEdge(const typename Graph<T>::Edge& edge) override
{
assert(0<=edge.from&&edge.from<Graph<T>::n&&0<=edge.to&&edge.to<Graph<T>::n);
int id=(int)Graph<T>::edges.size();
Graph<T>::ver[edge.from].pb(id);
Graph<T>::g[edge.to].pb(id);
Graph<T>::edges.pb(edge);
return id;
}
};
template<typename T>
class DirectedGraph :public Graph<T>
{
public:
DirectedGraph(int n) :Graph<T>(n)
{
}
int addEdge(const typename Graph<T>::Edge& edge) override
{
assert(0<=edge.from&&edge.from<Graph<T>::n&&0<=edge.to&&edge.to<Graph<T>::n);
int id=(int)Graph<T>::edges.size();
Graph<T>::g[edge.from].pb(id);
Graph<T>::edges.pb(edge);
return id;
}
};
struct UnionFind
{
vector<int> uf;
int n;
UnionFind(int n) :uf(n,-1),n(n)
{
}
int find(int v)
{
return uf[v]<0?v:uf[v]=find(uf[v]);
}
bool join(int v,int w)
{
if ((v=find(v))==(w=find(w)))
return false;
if (uf[v]>uf[w])
::swap(v,w);
uf[v]+=uf[w];
uf[w]=v;
n--;
return true;
}
bool connected(int v,int w)
{
return find(v)==find(w);
}
int size(int v)
{
return -uf[find(v)];
}
};
template<class T>
class BIT
{
private:
int sz;
vector<T> bit,arr;
public:
BIT(int sz):sz(sz),bit(sz+1),arr(sz)
{
}
void add(int i,int v)
{
arr[i]+=v;
i++;
for(;i<=sz;i+=low_bit(i))
bit[i]+=v;
}
T psum(int i)
{
i++;
T sum=0;
for(;i>0;i-=low_bit(i))
sz+=bit[i];
return sum;
}
void set(int i,int v)
{
add(i,v-arr[i]);
}
};
};
signed main()
{
setIO();
return 0;
}