0% found this document useful (0 votes)
10 views23 pages

Segmentation Assault ICPC Chennai (1) - Pagenumber

Uploaded by

bhavanamn1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views23 pages

Segmentation Assault ICPC Chennai (1) - Pagenumber

Uploaded by

bhavanamn1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

SEGMENTATION ASSAULT

(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

PrintWriter out = new PrintWriter(System.out);


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
Scanner SC = new Scanner(System.in);

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 min(long a, long b) {


return a > b ? b : a;
}

long ceil(long a, long b) {


return ((long) Math.ceil(((double) (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 gcd(long a, long b) {


if (b == 0)
return a;
return gcd(b, a % b);
}

long lcm(long a, long b) {


return ((a * b) / gcd(a, b));
}

long add(long a, long b) {


return (((a + mod) % mod + (b + mod) % mod) % mod);
}

long sub(long a, long b) {


return (((a + mod) % mod + ((-b) + mod) % mod) % mod);
}

long mul(long a, long b) {


return ((a % mod * b % mod) % mod);
}

long inv(long x) {
return pow(x, mod - 2);
}

long div(long x, long y) {


return mul(x, inv(y));
}

long pow(long a, long b) {


a %= mod;
long res = 1;
while (b > 0) {
if ((b & 1) != 0)
res = mul(res, a);
a = mul(a, a);
b /= 2;
}
return res;
}

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;
}

int MAXN = 1000001;


int spf[] = new int[MAXN];

public void sieve() {


spf[1] = 1;
for (int i = 2; i < MAXN; i++)
spf[i] = i;
for (int i = 4; i < MAXN; i += 2)
spf[i] = 2;
for (int i = 3; i * i < MAXN; i++)
if (spf[i] == i)
for (int j = i * i; j < MAXN; j += i)
if (spf[j] == j)
spf[j] = i;
}

public void primefactors(HashMap<Long, Long> map, int x) {


while (x != 1) {
fill(map, spf[x]);
x /= spf[x];
}
}

String Hashing (Using Rabin Karp)


ArrayList<Integer> search(char text[], char pattern[]) {
ArrayList<Integer> indexes = new ArrayList<Integer>(text.length);
int m = pattern.length;
int n = text.length;
long d = 31;
long hashp = 0;
long hasht = 0;
long h = 1;
for (int i = 0; i < m - 1; i++) {
h = mul(h, d);
}
for (int i = 0; i < m; i++) {
hashp = add(mul(d, hashp), pattern[i]);

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;
}

long[] prehash(char ch[]) {


int n = ch.length;
long ans[] = new long[n];
long a = 0;
long pr = 1;
for (int i = 0; i < n; i++) {
a = add(a, mul(sub(ch[i], 96), pr));
pr = mul(pr, 31);
ans[i] = a;
}
return ans;
}

long subhash(long ans[], int start, int end) {


return div(sub(ans[end], ans[start - 1]), pow(31, start));
}

Binary Search (Upper and Lower Bound)

int upper_bound(ArrayList<Long> a, long val) {


if (a.size() == 0)
return -1;
int start = 0;
int end = a.size() - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (a.get(mid) >= val) {
if (mid == 0 || a.get(mid - 1) < val)
return mid;
end = mid - 1;
} else
start = mid + 1;
}

6
return -1;
}

int lower_bound(ArrayList<Long> a, long val) {


if (a.size() == 0)
return -1;
int start = 0;
int end = a.size() - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (a.get(mid) > val) {
end = mid - 1;
continue;
} else {
if (mid == a.size() - 1 || a.get(mid + 1) > val)
return mid;
start = mid + 1;
}
}
return -1;
}

Custom Priority Queue


PriorityQueue<long[]> queue() {
PriorityQueue<long[]> queue = new PriorityQueue<long[]>((a, b) -> {
if (a[0] > b[0])
return 1;
else if (a[0] == b[0] && a[1] > b[1])
return 1;
else
return -1;
});
return queue;
}

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);
}
}

Segment Tree (Sum based Query0

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);
}

void buildTree(int node, int start, int end) {


if (start == end)
tree[node] = nums[start];
else {
int mid = (start + end) / 2;
buildTree(2 * node, start, mid);
buildTree(2 * node + 1, mid + 1, end);
tree[node] = tree[2 * node] + tree[2 * node + 1];
}
}

long query(int left, int right) {


return query(1, 0, n - 1, left - 1, right - 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);
}

void update(int index, long value) {


update(1, 0, n - 1, index - 1, value);
}

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;
}

void update(int index, long delta) {


for (; index <= n; index += index & -index)
tree[index] += delta;
}

long query(int index) {


long sum = 0;
for (; index > 0; index -= index & -index)
sum += tree[index];
return sum;
}

long query(int left, int right) {


return query(right) - query(left - 1);
}
}

Trie Data structure

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();
}

void insert(ArrayList<Character> ch) {


Node node = root;
for(int i=0;i<ch.size();i++) {
if(!node.containsKey(ch.get(i))) node.put(ch.get(i), new Node());
node = node.get(ch.get(i));
node.val++;
}
}

long search(ArrayList<Integer> ch) {


Node node = root;
for(int i=0;i<ch.size();i++) {
if(!node.containsKey(ch.get(i))) return 0;
node = node.get(ch.get(i));
}
return node.val;
}

void erase(ArrayList<Character> ch) {


Node node = root;
for(int i=0;i<ch.size();i++) {
if(node.containsKey(ch.get(i))) {
node = node.get(ch.get(i));
}
else return;
}
}

Sparse Table for grid queries

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]));
}
}
}
}

long query(int i, int j, int l){


int p = (int) log2(l);
return min(min(table[i][j][p],table[i+l-(1<<p)][j+l-(1<<p)][p]),min(table[i][j+l-
(1<<p)][p],table[i+l-(1<<p)][j][p]));
}
}

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;
}

void update(int x, int y, long val) {


for (; x <= n; x += (x & (-x))) {
for (int i = y; i <= n; i += (i & (-i))) { bit[x][i] += val; }
}
}

long query(int x1, int y1, int x2, int y2) {


long ans = 0;
for (int i = x2; i > 0; i -= (i & (-i))) {
for (int j = y2; j > 0; j -= (j & (-j))) { ans += bit[i][j]; }
}
for (int i = x2; i > 0; i -= (i & (-i))) {

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;
}
}

IMP: Bitwise Properties


1. Check if the number (X) is odd/even
X & 1 == 0 means X is even number
X & 1 == 1 means X is odd number
2. To check if X is power of 2
X & (X-1) == 0 means X is power of 2
X & (X-1) >0 means X is not power of 2
3. Check if the kth bit of X is set or not
(X>>k) & 1 == 0 kth bit is unset
(X>>k) & 1 == 1 kth bit is set
Some more.......
X ^ (1<<k) toggle’s the kth bit
X | (1<<k) set’s the kth bit
X & ~(1<<k) unsets the kth bit
4. If number of set bits in A = X, and number of set bits in B = Y, then number of set bits in
(A xor B) is even if (X + Y) is even and odd if (X + Y) is odd.
5. Find out of reminder of X when divided by 2^k
X & ((1<<k)-1) will give the reminder
6. IMP builtin function in CPP
__builtin_popcount(X) -> will give the number of set bits of number X
__builtin_popcountll(X) -> will give the number of set bits of number X but in ll
__builtin_ffs(x) (Find First Set) which returns (the index of the least significant bits of x) + 1.

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;

const int N = 10001;


int grundy[N];
int MEX(map<int, int> &mpp){
for(int i=0; ;i++){
if(mpp[i] == 0) return i;
}

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);
}

void build (long a[], int l, int r, int u){


if (l == r) tree[u] = a[l];
else {
int mid = (l + r) / 2;
build (a, l, mid, 2 * u + 1);
build (a, mid + 1, r, 2 * u + 2);
tree[u] = tree[2 * u + 1] + tree[2 * u + 2];
}
}

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);
}
}

long query(int index) {


return query(0, index - 1, 0, n - 1);
}

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
}
}

public class ConvexHull {

static int orientation(Point a, Point b, Point c) {


double v = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y);
if (v < 0) return -1; // clockwise
if (v > 0) return 1; // counter-clockwise
return 0;
}

static boolean cw(Point a, Point b, Point c, boolean includeCollinear) {


int o = orientation(a, b, c);
return o < 0 || (includeCollinear && o == 0);
}

static boolean collinear(Point a, Point b, Point c) {


return orientation(a, b, c) == 0;
}

static void convexHull(ArrayList<Point> points, boolean includeCollinear) {


Point p0 = Collections.min(points, Comparator.comparing((Point p) ->
p.y).thenComparing(p -> p.x));
points.sort((a, b) -> {
int o = orientation(p0, a, b);
if (o == 0)
return Double.compare((p0.x - a.x) * (p0.x - a.x) + (p0.y - a.y) * (p0.y - a.y),
(p0.x - b.x) * (p0.x - b.x) + (p0.y - b.y) * (p0.y - b.y));

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()));
}

ArrayList<Point> stack = new ArrayList<>();


for (int i = 0; i < points.size(); i++) {
while (stack.size() > 1 && !cw(stack.get(stack.size() - 2), stack.get(stack.size() - 1),
points.get(i), includeCollinear))
stack.remove(stack.size() - 1);
stack.add(points.get(i));
}

points.clear();
points.addAll(stack);
}

public static void main(String[] args) {


// Example usage:
ArrayList<Point> points = new ArrayList<>();
points.add(new Point(0, 0));
points.add(new Point(1, 1));
points.add(new Point(2, 2));
points.add(new Point(0, 2));

21
points.add(new Point(2, 0));

convexHull(points, false);

for (Point point : points) {


System.out.println("x: " + point.x + ", y: " + point.y);
}
}
}

22
23

You might also like