Skip to content

Commit

Permalink
AtCoder Library Practice Contest
Browse files Browse the repository at this point in the history
  • Loading branch information
kzrnm committed Dec 27, 2024
1 parent 611d728 commit 16e942f
Show file tree
Hide file tree
Showing 13 changed files with 450 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Sample/AtCoder Library Practice Contest/A - Disjoint Set Union.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using AtCoder;

var (n, q) = Console.ReadLine().Split().Select(int.Parse).ToTuple2();
var dsu = new Dsu(n);

while (--q >= 0)
{
var (t, u, v) = Console.ReadLine().Split().Select(int.Parse).ToTuple3();
if (t == 0)
{
dsu.Merge(u, v);
}
else
{
Console.WriteLine(dsu.Same(u, v) ? 1 : 0);
}
}


static class Extension
{
public static (T, T) ToTuple2<T>(this IEnumerable<T> t)
{
var e = t.GetEnumerator();
e.MoveNext(); var v0 = e.Current;
e.MoveNext(); var v1 = e.Current;
e.Dispose();
return (v0, v1);
}
public static (T, T, T) ToTuple3<T>(this IEnumerable<T> t)
{
var e = t.GetEnumerator();
e.MoveNext(); var v0 = e.Current;
e.MoveNext(); var v1 = e.Current;
e.MoveNext(); var v2 = e.Current;
e.Dispose();
return (v0, v1, v2);
}
}
42 changes: 42 additions & 0 deletions Sample/AtCoder Library Practice Contest/B - Fenwick Tree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using AtCoder;

var (n, q) = Console.ReadLine().Split().Select(int.Parse).ToTuple2();
var fw = new FenwickTree<long>(n);
var line = Console.ReadLine().Split().Select(int.Parse).ToArray();
for (var i = 0; i < n; i++)
fw.Add(i, line[i]);

while (--q >= 0)
{
var (t, l, r) = Console.ReadLine().Split().Select(int.Parse).ToTuple3();
if (t == 0)
{
fw.Add(l, r);
}
else
{
Console.WriteLine(fw[l..r]);
}
}


static class Extension
{
public static (T, T) ToTuple2<T>(this IEnumerable<T> t)
{
var e = t.GetEnumerator();
e.MoveNext(); var v0 = e.Current;
e.MoveNext(); var v1 = e.Current;
e.Dispose();
return (v0, v1);
}
public static (T, T, T) ToTuple3<T>(this IEnumerable<T> t)
{
var e = t.GetEnumerator();
e.MoveNext(); var v0 = e.Current;
e.MoveNext(); var v1 = e.Current;
e.MoveNext(); var v2 = e.Current;
e.Dispose();
return (v0, v1, v2);
}
}
24 changes: 24 additions & 0 deletions Sample/AtCoder Library Practice Contest/C - Floor Sum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using AtCoder;

int t = int.Parse(Console.ReadLine());

while (--t >= 0)
{
var (n, m, a, b) = Console.ReadLine().Split().Select(int.Parse).ToTuple4();

Console.WriteLine(MathLib.FloorSum(n, m, a, b));
}

static class Extension
{
public static (T, T, T, T) ToTuple4<T>(this IEnumerable<T> t)
{
var e = t.GetEnumerator();
e.MoveNext(); var v0 = e.Current;
e.MoveNext(); var v1 = e.Current;
e.MoveNext(); var v2 = e.Current;
e.MoveNext(); var v3 = e.Current;
e.Dispose();
return (v0, v1, v2, v3);
}
}
71 changes: 71 additions & 0 deletions Sample/AtCoder Library Practice Contest/D - Maxflow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using AtCoder;

var (n, m) = Console.ReadLine().Split().Select(int.Parse).ToTuple2();
var S = Enumerable.Range(0, n).Select(_ => Console.ReadLine().ToCharArray()).ToArray();
ReadOnlySpan<(int, int)> moves = [
(0, -1),
(0, +1),
(-1, 0),
(+1, 0),
];

var mf = new MfGraph<int>(n * m + 2);

var start = n * m;
var goal = start + 1;
for (int h = 0; h < n; h++)
for (int w = 0; w < m; w++)
{
var ix = h * m + w;
if (S[h][w] != '.') continue;
if ((h ^ w) % 2 != 0)
{
foreach (var (mh, mw) in moves)
{
var dh = h + mh;
var dw = w + mw;

if ((uint)dh < n && (uint)dw < m && S[dh][dw] == '.')
{
var dx = dh * m + dw;
mf.AddEdge(ix, dx, 1);
}
}
mf.AddEdge(start, ix, 1);
}
else
mf.AddEdge(ix, goal, 1);
}
Console.WriteLine(mf.Flow(start, goal));
foreach (var e in mf.Edges())
{
if (e.From >= start || e.To >= start || e.Flow == 0) continue;
var ix1 = e.From;
var ix2 = e.To;
if (ix1 > ix2) (ix1, ix2) = (ix2, ix1);
if (ix1 + m != ix2)
{
S[ix1 / m][ix1 % m] = '>';
S[ix2 / m][ix2 % m] = '<';
}
else
{
S[ix1 / m][ix1 % m] = 'v';
S[ix2 / m][ix2 % m] = '^';
}
}
foreach (var line in S)
{
Console.WriteLine(line);
}
static class Extension
{
public static (T, T) ToTuple2<T>(this IEnumerable<T> t)
{
var e = t.GetEnumerator();
e.MoveNext(); var v0 = e.Current;
e.MoveNext(); var v1 = e.Current;
e.Dispose();
return (v0, v1);
}
}
55 changes: 55 additions & 0 deletions Sample/AtCoder Library Practice Contest/E - MinCostFlow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using AtCoder;

var (n, K) = Console.ReadLine().Split().Select(int.Parse).ToTuple2();

var g = new McfGraph<int, long>(2 * n + 2);
int s = 2 * n, t = 2 * n + 1;

// we can "waste" the flow
g.AddEdge(s, t, n * K, int.MaxValue);

for (int i = 0; i < n; i++)
{
g.AddEdge(s, i, K, 0);
g.AddEdge(n + i, t, K, 0);
}

for (int i = 0; i < n; i++)
{
var line = Console.ReadLine().Split().Select(int.Parse).ToArray();
for (int j = 0; j < line.Length; j++)
{
long a = line[j];
g.AddEdge(i, n + j, 1, int.MaxValue - a);
}
}
var result = g.Flow(s, t, n * K);
Console.WriteLine((long)n * K * int.MaxValue - result.cost);

var grid = Enumerable.Range(0, n).Select(MakeLine).ToArray();
foreach (var e in g.Edges())
{
if (e.From == s || e.To == t || e.Flow == 0) continue;
grid[e.From][e.To - n] = 'X';
}
foreach (var line in grid)
{
Console.WriteLine(line);
}
char[] MakeLine(int _)
{
var rt = new char[n];
rt.AsSpan().Fill('.');
return rt;
}
static class Extension
{
public static (T, T) ToTuple2<T>(this IEnumerable<T> t)
{
var e = t.GetEnumerator();
e.MoveNext(); var v0 = e.Current;
e.MoveNext(); var v1 = e.Current;
e.Dispose();
return (v0, v1);
}
}
7 changes: 7 additions & 0 deletions Sample/AtCoder Library Practice Contest/F - Convolution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using AtCoder;

Console.ReadLine();
var a = Console.ReadLine().Split().Select(int.Parse).ToArray();
var b = Console.ReadLine().Split().Select(int.Parse).ToArray();

Console.WriteLine(string.Join(' ', MathLib.Convolution(a, b)));
29 changes: 29 additions & 0 deletions Sample/AtCoder Library Practice Contest/G - SCC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using AtCoder;

var (n, m) = Console.ReadLine().Split().Select(int.Parse).ToTuple2();
var scc = new SccGraph(n);

while (--m >= 0)
{
var (a, b) = Console.ReadLine().Split().Select(int.Parse).ToTuple2();
scc.AddEdge(a, b);
}

var groups = scc.Scc();
Console.WriteLine(groups.Length);
foreach (var g in groups)
{
Console.WriteLine(string.Join(' ', g.Prepend(g.Length)));
}

static class Extension
{
public static (T, T) ToTuple2<T>(this IEnumerable<T> t)
{
var e = t.GetEnumerator();
e.MoveNext(); var v0 = e.Current;
e.MoveNext(); var v1 = e.Current;
e.Dispose();
return (v0, v1);
}
}
41 changes: 41 additions & 0 deletions Sample/AtCoder Library Practice Contest/H - Two SAT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using AtCoder;
using static System.Math;

var (n, D) = Console.ReadLine().Split().Select(int.Parse).ToTuple2();
(int X, int Y)[] XY = Enumerable.Range(0, n).Select(_ => Console.ReadLine().Split().Select(int.Parse).ToTuple2()).ToArray();
var ts = new TwoSat(n);
for (int i = 0; i < XY.Length; i++)
{
var (x1, y1) = XY[i];
for (int j = i + 1; j < XY.Length; j++)
{
var (x2, y2) = XY[j];
if (Abs(x1 - x2) < D) ts.AddClause(i, true, j, true);
if (Abs(x1 - y2) < D) ts.AddClause(i, true, j, false);
if (Abs(y1 - x2) < D) ts.AddClause(i, false, j, true);
if (Abs(y1 - y2) < D) ts.AddClause(i, false, j, false);
}
}
if (ts.Satisfiable())
{
Console.WriteLine("Yes");
var rt = ts.Answer();
foreach (var v in rt.Zip(XY, (b, t) => b ? t.Y : t.X))
Console.WriteLine(v);
}
else
{
Console.WriteLine("No");
}

static class Extension
{
public static (T, T) ToTuple2<T>(this IEnumerable<T> t)
{
var e = t.GetEnumerator();
e.MoveNext(); var v0 = e.Current;
e.MoveNext(); var v1 = e.Current;
e.Dispose();
return (v0, v1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using AtCoder;

var s = Console.ReadLine();
var sa = StringLib.SuffixArray(s);

Console.WriteLine((long)(s.Length + 1) * s.Length / 2 - StringLib.LcpArray(s, sa).Select(i => (long)i).Sum());
31 changes: 31 additions & 0 deletions Sample/AtCoder Library Practice Contest/J - Segment Tree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using AtCoder;

var line = Console.ReadLine().Split(' ');
var N = int.Parse(line[0]);
var Q = int.Parse(line[1]);
var seg = new Segtree<long, Op>(Console.ReadLine().Split(' ').Select(long.Parse).ToArray());

for (int q = 0; q < Q; q++)
{
line = Console.ReadLine().Split(' ');
int t = int.Parse(line[0]);
int l = int.Parse(line[1]) - 1;
int r = int.Parse(line[2]);
switch (t)
{
case 1:
seg[l] = r;
break;
case 2:
Console.WriteLine(seg.Prod(l, r));
break;
case 3:
Console.WriteLine(1 + seg.MaxRight(l, num => num < r));
break;
}
}
struct Op : ISegtreeOperator<long>
{
public long Identity => long.MinValue;
public long Operate(long x, long y) => Math.Max(x, y);
}
Loading

0 comments on commit 16e942f

Please sign in to comment.