Segmentation Assault ICPC Chennai (1) - Pagenumber
Segmentation Assault ICPC Chennai (1) - Pagenumber
(TSEC, MUMBAI)
TEAM NOTEBOOK
(ICPC CHENNAI REGIONALS)
1
Linux Commands for build and execution
1. For C++
g++ -std=c++17 file.cpp && ./a.out
2. For Java
javac Main.java && java Main
Template
Custom Hash
struct chash
{
static uint64_t splitmix64(uint64_t x)
{
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const
{
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
Java Fast IO
String ins() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
}
return st.nextToken();
}
2
char[] inc() {
return ins().toCharArray();
}
ArrayList<Character> incl() {
ArrayList<Character> ch = new ArrayList<>();
String s = ins();
for (int i = 0; i < s.length(); i++)
ch.add(s.charAt(i));
return ch;
}
int ini() {
return Integer.parseInt(ins());
}
long in() {
return Long.parseLong(ins());
}
double ind() {
return Double.parseDouble(ins());
}
Math Functions
long max(long a, long b) {
return a > b ? a : b;
}
long abs(long a) {
if (a < 0)
return (-1 * a);
return a;
}
long log(long a) {
return (long) (Math.log(a));
}
3
long log2(long a) {
return (long) (Math.log(a) / Math.log(2));
}
long inv(long x) {
return pow(x, mod - 2);
}
long sqrt(long x) {
long start = 0, end = (long) 3e9, ans = 1;
4
while (start <= end) {
long mid = (start + end) / 2;
if (mid * mid <= x) {
ans = mid;
start = mid + 1;
} else
end = mid - 1;
}
return ans;
}
5
hasht = add(mul(d, hasht), text[i]);
}
for (int i = 0; i <= n - m; i++) {
if (hashp == hasht) {
indexes.add(i);
}
if (i < n - m) {
hasht = add(mul(d, (sub(hasht, mul(text[i], h)))), text[i + m]);
if (hasht < 0)
hasht = add(hasht, mod);
}
}
return indexes;
}
6
return -1;
}
class Pair {
long f;
long s;
Pair(long f, long s) {
this.f = f;
this.s = s;
}
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return f == pair.f && s == pair.s;
}
7
@Override public int hashCode() {
return Objects.hash(f, s);
}
}
class SegmentTree {
long[] tree;
long[] nums;
int n;
SegmentTree(long[] nums) {
this.nums = nums;
this.n = nums.length;
this.tree = new long[4 * n];
buildTree(1, 0, n - 1);
}
long query(int node, int start, int end, int left, int right) {
if (right < start || left > end)
return 0;
else if (left <= start && right >= end)
return tree[node];
else
return query(2 * node, start, (start + end) / 2, left, right)
+ query(2 * node + 1, ((start + end) / 2) + 1, end, left, right);
}
8
void update(int node, int start, int end, int index, long value) {
if (start == end) {
nums[index] = value;
tree[node] = value;
} else {
int mid = (start + end) / 2;
if (index >= start && index <= mid)
update(2 * node, start, mid, index, value);
else
update(2 * node + 1, mid + 1, end, index, value);
tree[node] = tree[2 * node] + tree[2 * node + 1];
}
}
}
Fenwick Tree
class FenwickTree {
long[] tree;
int n;
FenwickTree(int size) {
this.tree = new long[size + 1];
this.n = size;
}
class Node {
Node links[] = new Node[26];
int val = 0;
9
boolean containsKey(char ch) {return links[ch - 97] != null;}
Node get(char ch) {return links[ch - 97];}
void put(char ch, Node node) {links[ch - 97] = node;}
}
class Trie {
Node root;
Trie(){
root = new Node();
}
class SparseTable {
long table[][][];
SparseTable(long a[][]){
int n = a.length;
10
int m = a[0].length;
int t = (int)log2(min(n, m)) + 1;
table = new long[a.length][a[0].length][t];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) table[i][j][0] = a[i][j];
for (int k = 1; k < t; k++) {
for (int i = 0; i + (1 << k) <= n; i++) {
for (int j = 0; j + (1 << k) <= m; j++) {
table[i][j][k] = min(min(table[i][j][k - 1], table[i + (1 << (k - 1))][j][k - 1]),
min(table[i][j + (1 << (k - 1))][k - 1], table[i + (1 << (k - 1))][j + (1 << (k - 1))][k - 1]));
}
}
}
}
2D Fenwick Tree
class FenwickTree2D {
long[][] bit;
int n;
int m;
FenwickTree2D(int n, int m) {
this.bit = new long[n + 1][m + 1];
this.n = n;
this.m = m;
}
11
for (int j = y1 - 1; j > 0; j -= (j & (-j))) { ans -= bit[i][j]; }
}
for (int i = x1 - 1; i > 0; i -= (i & (-i))) {
for (int j = y2; j > 0; j -= (j & (-j))) { ans -= bit[i][j]; }
}
for (int i = x1 - 1; i > 0; i -= (i & (-i))) {
for (int j = y1 - 1; j > 0; j -= (j & (-j))) { ans += bit[i][j]; }
}
return ans;
}
}
12
__lg(x) which returns the index of the highest set bit.
__builtin_clz(x): Counts the leading number of zeros of the integer(long/long long).
__builtin_ctz(x): Counts the trailing number of zeros of the integer(long/long long).
7. Two important identites
A+B = (A ^ B) + 2*(A&B) -------> Full adder logic
A+B = (A | B) + (A&B)
8. Clear all the trailing zeros
X & (X+1) will clear all the trailing zeros
E.g. X = 110010111 so final value will be 110010000
9. Multiply and divide a number by 2^k
(X<<k) will give a value X multiplied with 2^k
(X>>k) will give a value X divide with 2^k
10. Swap without using temporary variable (fastest swap) (f*ck u)
X=X^Y
Y=X^Y
X=X^Y
Grundy Number
#define index2(i, j) cout << "(" << i << "," << j << ") "
#define index3(i, j, k) cout << "(" << i << "," << j << "," << k << ") "
#define tv(v) for(auto it: v) cout << it << " "; cout << endl
const int mod = 1e9 + 7;
13
}
void precompute(){
for(int i=0; i<N; i++){
map<int, int> mpp;
for(int j=1; j < i - j; j++){
int t = grundy[i-j]^grundy[j];
mpp[t]++;
}
grundy[i] = MEX(mpp);
}
}
void solve(int test){
cout << "Case " << test << ": ";
int n;
cin >> n;
int x = 0;
for(int i=0; i<n; i++){
int a;
cin >> a;
x ^= grundy[a];
}
cout << (x == 0 ? "Bob\n" : "Alice\n");
}
int main() {
int t = 1;
cin >> t;
precompute();
for(int i=1; i<=t; i++){
solve(i);
14
}
return 0;
}
Dijkstra Algorithm
public void solve() {
int n = ini();
int m = ini();
ArrayList<Integer>[] a = new ArrayList[n+1];
for(int i=0;i<n+1;i++) {
a[i] = new ArrayList<Integer>();
}
while(m-- > 0) {
int first = ini();
int second = ini();
a[first].add(second);
a[second].add(first);
}
PriorityQueue<int[]> queue = pq(0, 2, true);
int neighbour[] = new int[n+1];
int distance[] = new int[n+1];
boolean visited[] = new boolean[n+1];
distance[1] = 0;
for(int i=2;i<=n;i++) {
distance[i] = n;
}
queue.add(new int[] {0, 1});
while(queue.size() > 0) {
int current[] = queue.poll();
int vertex = (int)(current[1]);
15
int dis = current[0];
if(visited[vertex]) {
continue;
}
visited[vertex] = true;
for(long next : a[(int)(vertex)]) {
int vert = (int)(next);
if(!visited[vert]) {
int ndis = dis + 1;
if(distance[vert] > ndis) {
distance[vert] = ndis;
neighbour[vert] = vertex;
}
queue.add(new int[] {ndis, vert});
}
}
}
int count = 0;
int start = n;
ArrayList<Integer> order = new ArrayList<Integer>(n);
while(true) {
if(neighbour[start]==0 && start != 1) {
pl("IMPOSSIBLE");
return;
}
order.add(start);
if(start == 1) {
break;
}
16
start = neighbour[start];
}
pl(order.size());
for(int i=order.size()-1;i>=0;i--) {
ps(order.get(i));
}
}
Laaaazy Segment Tree
class SegmentLazy {
long tree[];
long lazy[];
int n;
SegmentLazy(long a[]){
this.n = a.length;
tree = new long[4*n];
lazy = new long[4*n];
build (a, 0, n - 1, 0);
}
17
void update(int l, int r, long val){
update(0, n - 1, l - 1, r - 1, 0, val);
}
void update (int l, int r, int rl, int rr, int u, long val) {
if (rr < l || rl > r) return;
if (l == r) {tree[u] += lazy[u] + val; lazy[u] = 0;}
else if (rl < l && rr > r) {
tree[u] += lazy[u] * (r - l + 1);
lazy[2 * u + 1] += (val + lazy[u]);
lazy[2 * u + 2] += (val + lazy[u]);
lazy[u] = 0;
tree[u] += (r - l + 1) * val;
}
else {
tree[u] += lazy[u] * (r - l + 1);
lazy[2 * u + 1] += lazy[u];
lazy[2 * u + 2] += lazy[u];
lazy[u] = 0;
int mid = (l + r) / 2;
update(l, mid, rl, rr, 2 * u + 1, val);
update(mid + 1, r, rl, rr, 2 * u + 2, val);
}
}
18
long query(int u, int k, int l, int r) {
if (l == r) {
tree[u] += lazy[u];
lazy[u] = 0;
return tree[u];
}
else {
tree[u] += lazy[u] * (r - l + 1);
lazy[2 * u + 1] += lazy[u];
lazy[2 * u + 2] += lazy[u];
lazy[u] = 0;
int mid = (l + r) / 2;
if (k <= mid) return query(2 * u + 1, k, l, mid);
else return query(2 * u + 2, k, mid + 1, r);
}
}
}
Conex Hull
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Point {
double x, y;
Point(double x, double y) {
this.x = x;
this.y = y;
19
}
}
20
return Integer.compare(o, 0);
});
if (includeCollinear) {
int i = points.size() - 1;
while (i >= 0 && collinear(p0, points.get(i), points.get(points.size() - 1)))
i--;
Collections.reverse(points.subList(i + 1, points.size()));
}
points.clear();
points.addAll(stack);
}
21
points.add(new Point(2, 0));
convexHull(points, false);
22
23