Skip to content

Instantly share code, notes, and snippets.

@KirillLykov
KirillLykov / kth0onSegment.cpp
Created March 25, 2018 19:40
Find k-th 0 on interval [l,r] using segment tree
#include <bits/stdc++.h>
using namespace std;
typedef long long int lint;
typedef long double ldouble;
const int N = 100001;
int a[N];
int t[4*N];
void build(int cur, int l, int r) {
@KirillLykov
KirillLykov / cf_121_d2_e.cpp
Last active June 10, 2019 14:24
Solution of cf 121, div2, E using LCA in logN
// Solution for http://codeforces.com/contest/192/problem/E
// You are given a tree and in each node of the tree there is a person (called full further)
// which goes to another person living in different node.
// You need to compute for every edge how many persons will pass through it.
// We decompose the path from one person to another using LCA.
// Using LCA algorithm which requires logN operations for search
#include <bits/stdc++.h>
using namespace std;
typedef long long int lint;
@KirillLykov
KirillLykov / cf_e3_e.cpp
Last active April 2, 2018 19:20
Solution for problem: find cost of Min Spanning Tree if edge 1,2,... must not be excluded. Used fast LCA (logN) and dynamics on tree
// Solution for http://codeforces.com/problemset/problem/609/E
// Idea:
// 1. Build Min Spanning Tree MST
// 2. For each edge:
// 2.1 if edge belongs to MST => just print cost(MST)
// 2.2 else
// if we add this edge to MST, it will not be a tree anymore since
// we introduced a loop L.
// Now, if we delete an edge from L with max cost (except for newly added),
// we get a tree again. Cost(newTree) = cost(MST) + cost(addedEdge) - cost(deletedEdge)
@KirillLykov
KirillLykov / tc_tco18_r1a_b.cpp
Created April 21, 2018 17:55
Topcoder TCO2018, Round1A, B
#include <bits/stdc++.h>
using namespace std;
class Resistance {
public:
bool pass(int a, vector<string>& missions) {
for (auto& m : missions) {
if (m[0] == 'F') {
int mi = stoi(m.substr(1), nullptr, 2);
if ((mi & a) == 0)
@KirillLykov
KirillLykov / mccme_3327.cpp
Created April 27, 2018 21:06
Solution of a problem using dynamic segment tree
// Solution for http://informatics.mccme.ru/moodle/mod/statements/view.php?chapterid=3327#1
// Given an array A[1,..,n] it is required to implement two operations
// 1) get(index) return A[index]
// 2) add(l, r, val) A[l,..,r] += val
// I used dynamic (also called implicit) segment tree
// this type of implementation reduces space from O(N) to O(mlogN)
#include <bits/stdc++.h>
using namespace std;
typedef long long int lint;
@KirillLykov
KirillLykov / cf_spb_seg2_a.cpp
Created April 28, 2018 20:31
Explicit segment tree problem
// solution for http://codeforces.com/gym/100094/attachments/download/1239/20122013-tryenirovka-spbgu-b-6-zaprosy-na-otryezkye-dyeryevo-otryezkov-2-ru.pdf
// problem A using explicit segment tree
#include <bits/stdc++.h>
using namespace std;
typedef long long int lint;
const int N = 1e6;
int t[4*N+1];
@KirillLykov
KirillLykov / cf_spb_seg2_a_dyn.cpp
Created April 28, 2018 20:33
Implicit segment tree problem
// solution for http://codeforces.com/gym/100094/attachments/download/1239/20122013-tryenirovka-spbgu-b-6-zaprosy-na-otryezkye-dyeryevo-otryezkov-2-ru.pdf
// problem A using Implicit (dynamic) segment tree
// For unknown reason it fails test 8 with TLE
// Nevertheless might be useful as an example of dynamic implementation
#include <bits/stdc++.h>
using namespace std;
typedef long long int lint;
const lint BIG = 1e9 + 1;
struct Node {
@KirillLykov
KirillLykov / .tmux.conf
Last active March 1, 2022 09:58
My TMUX config
# remap prefix to Control + q
set -g prefix C-q
unbind C-b
bind C-q send-prefix
# force a reload of the config file
unbind r
bind r source-file ~/.tmux.conf
# quick pane cycling
@KirillLykov
KirillLykov / arc97_a.cpp
Last active October 21, 2020 13:06
solution for atcoder contest arc97, A
// Solution of https://arc097.contest.atcoder.jp/tasks/arc097_a
// using string hashing
// Good as a source of code for general hashing with reversed element
// But overcomplicated for this problem -- it is possible to just generate all the
// sustrings, sort/unique them and return k-th
//
#include <bits/stdc++.h>
using namespace std;
typedef long long int lint;
@KirillLykov
KirillLykov / seg_lazy.cpp
Created June 21, 2018 21:56
Two lazy tree implemented using array and dynamic memory
#include <bits/stdc++.h>
using namespace std;
typedef long long int lint;
// Explicit tree: using array
const int N = 7;
int tr[4*N + 1];
int maxV[4*N + 1];
void update(int cur, int L, int R, int l, int r) // increments by one corresponding vertices