Пример #1
0
        static void Method(string[] args)
        {
            int[] nq = ReadInts();
            int   n  = nq[0];
            int   q  = nq[1];

            int[]   cs      = ReadInts();
            int[][] queries = new int[q][];
            for (int i = 0; i < q; i++)
            {
                int[] lr = ReadInts();
                queries[i] = new int[4] {
                    lr[0] - 1, lr[1] - 1, 0, i
                };
            }

            int[] prevs = new int[n];
            int[] nexts = new int[n];
            for (int i = n - 1; i >= 0; i--)
            {
                if (prevs[cs[i] - 1] > 0)
                {
                    nexts[i] = prevs[cs[i] - 1];
                }
                prevs[cs[i] - 1] = i;
            }

            SegTree <int> tree = new SegTree <int>(n, 0, (a, b) => a + b, 0);

            for (int i = 0; i < n; i++)
            {
                if (prevs[cs[i] - 1] == i)
                {
                    tree.Update(i, 1);
                }
            }

            queries = queries.OrderBy(a => a[0]).ToArray();
            int left = 0;

            for (int i = 0; i < q; i++)
            {
                while (left < queries[i][0])
                {
                    if (nexts[left] > 0)
                    {
                        tree.Update(nexts[left], 1);
                    }
                    left++;
                }

                queries[i][2] = tree.Scan(queries[i][0], queries[i][1] + 1);
            }

            queries = queries.OrderBy(a => a[3]).ToArray();
            for (int i = 0; i < q; i++)
            {
                WriteLine(queries[i][2]);
            }
        }
Пример #2
0
        static Action Solve()
        {
            int[] nk = ReadInts();
            int   n  = nk[0];
            int   k  = nk[1];

            int[]         cs       = ReadInts();
            int[]         ds       = ReadInts();
            SegTree <int> segTreeC = new SegTree <int>(n, Max, 0);
            SegTree <int> segTreeD = new SegTree <int>(n, Max, 0);

            for (int i = 0; i < n; i++)
            {
                segTreeC.Update(i, cs[i]);
                segTreeD.Update(i, ds[i]);
            }

            long cnt = 0;

            for (int i = 0; i < n; i++)
            {
                for (int j = i + 1; j <= n; j++)
                {
                    int delta = Abs(segTreeC.Scan(i, j) - segTreeD.Scan(i, j));
                    if (delta <= k)
                    {
                        cnt++;
                    }
                }
            }

            return(() => WriteLine(cnt));
        }
Пример #3
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            long[]         ps = ReadLongs();
            SegTree <long> segTreeMin
                = new SegTree <long>(n, long.MaxValue, Min, long.MaxValue);
            SegTree <long> segTreeMax = new SegTree <long>(n, -1, Max, -1);

            long[] sorted = new long[n];
            var    poses  = new Dictionary <long, int>();

            for (int i = 0; i < n; i++)
            {
                sorted[i] = ps[i];
                poses.Add(ps[i], i);
            }
            Array.Sort(sorted);
            long res = 0;

            for (int i = n - 1; i >= 0; i--)
            {
                long bottom = segTreeMax.Scan(0, poses[sorted[i]]);
                long top    = segTreeMin.Scan(poses[sorted[i]], n);
                if (bottom != -1)
                {
                    long bottom2 = segTreeMax.Scan(0, (int)bottom);
                    long topTmp  = top == long.MaxValue ? n : top;
                    res += (topTmp - poses[sorted[i]]) * (bottom - bottom2) * sorted[i];
                }
                if (top != long.MaxValue)
                {
                    long top2   = segTreeMin.Scan((int)top + 1, n);
                    long topTmp = top2 == long.MaxValue ? n : top2;
                    res += (topTmp - top) * (poses[sorted[i]] - bottom) * sorted[i];
                }
                segTreeMax.Update(poses[sorted[i]], poses[sorted[i]]);
                segTreeMin.Update(poses[sorted[i]], poses[sorted[i]]);
            }
            WriteLine(res);
        }
Пример #4
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            char[]  s       = Read().ToCharArray();
            int     q       = ReadInt();
            SegTree segTree = new SegTree(n, (a, b) =>
            {
                bool[] res = new bool[26];
                for (int i = 0; i < a.Length; i++)
                {
                    res[i] = a[i] | b[i];
                }
                return(res);
            });

            for (int i = 0; i < n; i++)
            {
                bool[] tmp = segTree.Look(i);
                tmp[s[i] - 'a'] = true;
                segTree.Update(i);
            }
            for (int i = 0; i < q; i++)
            {
                string[] query = Read().Split();
                int      type  = int.Parse(query[0]);
                if (type == 1)
                {
                    int    index = int.Parse(query[1]) - 1;
                    bool[] tmp   = segTree.Look(index);
                    tmp[s[index] - 'a']    = false;
                    tmp[query[2][0] - 'a'] = true;
                    s[index] = query[2][0];
                    segTree.Update(index);
                }
                else
                {
                    int    l     = int.Parse(query[1]) - 1;
                    int    r     = int.Parse(query[2]);
                    bool[] tmp   = segTree.Scan(l, r);
                    int    kinds = 0;
                    for (int j = 0; j < 26; j++)
                    {
                        if (tmp[j])
                        {
                            kinds++;
                        }
                    }
                    WriteLine(kinds);
                }
            }
        }
Пример #5
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            int[]   array       = new int[n];
            int[][] sortedArray = new int[n][];
            for (int i = 0; i < n; i++)
            {
                array[i]       = ReadInt();
                sortedArray[i] = new int[2] {
                    array[i], i
                };
            }
            sortedArray = sortedArray.OrderBy(a => a[0]).ToArray();
            int[] indexes = new int[n];
            for (int i = 0; i < n; i++)
            {
                indexes[sortedArray[i][1]] = i;
            }
            int[] prevs = new int[n];
            int   prev  = -1;

            prevs[0] = -1;
            for (int i = 1; i < n; i++)
            {
                if (sortedArray[i - 1][0] != sortedArray[i][0])
                {
                    prev = i - 1;
                }
                prevs[i] = prev;
            }

            SegTree <int> segTree = new SegTree <int>(n, -1, Max, -1);
            int           res     = 0;

            for (int i = 0; i < n; i++)
            {
                int index = indexes[i];
                int val   = segTree.Scan(0, prevs[index] + 1);
                if (val == -1)
                {
                    res++;
                }
                else
                {
                    segTree.Update(val, -1);
                }
                segTree.Update(index, index);
            }

            WriteLine(res);
        }
Пример #6
0
        static void Method(string[] args)
        {
            int[] nk = ReadInts();
            int   n  = nk[0];
            long  k  = nk[1];

            long[] array = new long[n];
            for (int i = 0; i < n; i++)
            {
                array[i]  = ReadLong();
                array[i] -= k;
            }

            long[] sums    = new long[n];
            var    posDict = new Dictionary <long, Stack <int> >();
            long   res     = 0;

            for (int i = 0; i < n; i++)
            {
                sums[i] = array[i];
                if (i > 0)
                {
                    sums[i] += sums[i - 1];
                }

                if (!posDict.ContainsKey(sums[i]))
                {
                    posDict.Add(sums[i], new Stack <int>());
                }
                posDict[sums[i]].Push(i);

                if (sums[i] >= 0)
                {
                    res++;
                }
            }

            Array.Sort(sums);
            Array.Reverse(sums);
            SegTree <long> segTree = new SegTree <long>(n, (a, b) => a + b, 0);

            for (int i = 0; i < n; i++)
            {
                int index = posDict[sums[i]].Pop();
                res += segTree.Scan(index + 1, n);
                segTree.Update(index, 1);
            }
            WriteLine(res);
        }
Пример #7
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            long[][] xds = new long[n][];
            for (int i = 0; i < n; i++)
            {
                xds[i] = ReadLongs();
            }
            xds = xds.OrderBy(a => a[0]).ToArray();

            SegTree <int> tree = new SegTree <int>(n, 0, Max, 0);

            for (int i = n - 1; i >= 0; i--)
            {
                tree.Update(i, i);
                int bottom = i;
                int top    = n;
                while (bottom + 1 < top)
                {
                    int mid = (bottom + top) / 2;
                    if (xds[mid][0] < xds[i][0] + xds[i][1])
                    {
                        bottom = mid;
                    }
                    else
                    {
                        top = mid;
                    }
                }
                tree.Update(i, tree.Scan(i, top));
            }

            int[] dp   = new int[n];
            int   mask = 998244353;

            dp[0]             = 1;
            dp[tree.Look(0)] += 1;

            for (int i = 1; i < n; i++)
            {
                dp[i] += dp[i - 1];
                dp[i] %= mask;
                int to = tree.Look(i);
                dp[to] += dp[i - 1];
                dp[to] %= mask;
            }
            WriteLine(dp[n - 1]);
        }
Пример #8
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            long[] array   = ReadLongs();
            var    sumTree = new SegTree <long>(n, 0, (a, b) => a + b, 0);
            var    gcdTree = new SegTree <long>(n, 0, (a, b) => GCD(a, b), 0);

            for (int i = 0; i < n; i++)
            {
                long delta = array[i];
                if (i > 0)
                {
                    delta -= array[i - 1];
                }
                sumTree.Update(i, delta);
                gcdTree.Update(i, Abs(delta));
            }

            int m = ReadInt();

            for (int i = 0; i < m; i++)
            {
                int[] tlr = ReadInts();
                int   t   = tlr[0];
                int   l   = tlr[1];
                int   r   = tlr[2];

                if (t == 0)
                {
                    long top = sumTree.Scan(0, l);
                    long gcd = gcdTree.Scan(l, r);
                    WriteLine(GCD(top, gcd));
                }
                else
                {
                    sumTree.Update(l - 1, sumTree.Look(l - 1) + t);
                    gcdTree.Update(l - 1, Abs(sumTree.Look(l - 1)));
                    if (r < n)
                    {
                        sumTree.Update(r, sumTree.Look(r) - t);
                        gcdTree.Update(r, Abs(sumTree.Look(r)));
                    }
                }
            }
        }
Пример #9
0
        static void Method(string[] args)
        {
            int[] hwm = ReadInts();
            int   h   = hwm[0];
            int   w   = hwm[1];
            int   m   = hwm[2];

            List <int>[] hws = new List <int> [h];
            for (int i = 0; i < h; i++)
            {
                hws[i] = new List <int>();
            }
            SegTree <int> tree = new SegTree <int>(w, 0, Max, 0);

            for (int i = 0; i < m; i++)
            {
                int[] hw = ReadInts();
                hw[0]--;
                hw[1]--;
                hws[hw[0]].Add(hw[1]);
                tree.Update(hw[1], tree.Look(hw[1]) + 1);
            }

            int res = 0;

            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < hws[i].Count; j++)
                {
                    tree.Update(hws[i][j], tree.Look(hws[i][j]) - 1);
                }

                int tmp = hws[i].Count + tree.Scan(0, h + 1);
                res = Max(res, tmp);

                for (int j = 0; j < hws[i].Count; j++)
                {
                    tree.Update(hws[i][j], tree.Look(hws[i][j]) + 1);
                }
            }

            WriteLine(res);
        }
Пример #10
0
        static void Method(string[] args)
        {
            int           q       = ReadInt();
            SegTree <int> segTree = new SegTree <int>(200010, 0, (a, b) => a + b, 0);
            List <int>    reses   = new List <int>();

            for (int i = 0; i < q; i++)
            {
                int[] tx = ReadInts();
                int   t  = tx[0];
                int   x  = tx[1];
                if (t == 1)
                {
                    segTree.Update(x, segTree[x] + 1);
                }
                else
                {
                    int bottom = 0;
                    int top    = 200001;
                    while (bottom + 1 < top)
                    {
                        int mid = (bottom + top) / 2;
                        if (segTree.Scan(1, mid + 1) < x)
                        {
                            bottom = mid;
                        }
                        else
                        {
                            top = mid;
                        }
                    }
                    reses.Add(top);
                    segTree.Update(top, segTree[top] - 1);
                }
            }
            for (int i = 0; i < reses.Count; i++)
            {
                WriteLine(reses[i]);
            }
        }
Пример #11
0
        static void Method(string[] args)
        {
            int[] nl     = ReadInts();
            int   n      = nl[0];
            int   length = nl[1];

            int[][] lrcs = new int[n][];
            for (int i = 0; i < n; i++)
            {
                lrcs[i] = ReadInts();
            }
            Array.Sort(lrcs, (a, b) => a[0] - b[0]);
            SegTree <long> tree = new SegTree <long>(length,
                                                     long.MaxValue / 2, Min, long.MaxValue / 2);

            for (int i = 0; i < n; i++)
            {
                if (lrcs[i][0] == 0)
                {
                    if (lrcs[i][2] < tree.Look(lrcs[i][1] - 1))
                    {
                        tree.Update(lrcs[i][1] - 1, lrcs[i][2]);
                    }
                }
                else
                {
                    long val = lrcs[i][2]
                               + tree.Scan(lrcs[i][0] - 1, lrcs[i][1] - 1);
                    if (val < tree.Look(lrcs[i][1] - 1))
                    {
                        tree.Update(lrcs[i][1] - 1, val);
                    }
                }
            }
            WriteLine(tree.Look(length - 1));
        }
Пример #12
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            int[]   array  = ReadInts();
            int[]   bArray = ReadInts();
            int[][] aPoses = new int[n][];
            int[][] bPoses = new int[n][];
            for (int i = 0; i < n; i++)
            {
                aPoses[i] = new int[2] {
                    array[i], i
                };
                bPoses[i] = new int[2] {
                    bArray[i], i
                };
            }
            Array.Sort(array);
            Array.Reverse(array);
            Array.Sort(bArray);
            Array.Sort(aPoses, (a, b) => b[0] - a[0]);
            Array.Sort(bPoses, (a, b) => a[0] - b[0]);
            var dict = new Dictionary <int, int>();

            for (int i = 0; i < n; i++)
            {
                if (dict.ContainsKey(bArray[i]))
                {
                    continue;
                }
                dict.Add(bArray[i], dict.Count);
            }
            SegTree <int> segTree
                = new SegTree <int>(dict.Count, 0, (a, b) => a + b, 0);
            long res = 0;

            for (int i = 0; i < n; i++)
            {
                int bottom = -1;
                int top    = n - 1;
                while (bottom + 1 < top)
                {
                    int mid = (bottom + top + 1) / 2;
                    if (mid >= array[i])
                    {
                        top = mid;
                    }
                    else
                    {
                        bottom = mid;
                    }
                }
                int cnt = segTree.Scan(dict[bArray[top]], n);
                //if(bArray[aPoses[i][1]])
            }
            if (res <= n - 2)
            {
                WriteLine("Yes");
            }
            else
            {
                WriteLine("No");
            }
            WriteLine(res);
        }
Пример #13
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            int[] array       = ReadInts();
            var   segTreeEven = new SegTree <int>(n, int.MaxValue / 10, Min, int.MaxValue / 10);
            var   segTreeOdd  = new SegTree <int>(n, int.MaxValue / 10, Min, int.MaxValue / 10);
            var   poses       = new int[n];

            for (int i = 0; i < n; i++)
            {
                if (i % 2 == 0)
                {
                    segTreeEven.Update(i, array[i]);
                }
                else
                {
                    segTreeOdd.Update(i, array[i]);
                }
                poses[array[i] - 1] = i;
            }

            var que = new PriorityQueue <int[]>();

            que.Enqueue(segTreeEven.Scan(0, n), new int[2] {
                0, n
            });
            while (que.Count > 0)
            {
                var pair   = que.Dequeue();
                int top    = (int)pair.Key;
                int topPos = poses[top - 1];
                int last;
                if (pair.Value[0] % 2 == 0)
                {
                    last = segTreeOdd.Scan(topPos + 1, pair.Value[1]);
                }
                else
                {
                    last = segTreeEven.Scan(topPos + 1, pair.Value[1]);
                }
                int lastPos = poses[last - 1];

                Write(top + " " + last + " ");

                if (pair.Value[1] - pair.Value[0] <= 2)
                {
                    continue;
                }

                if (pair.Value[0] % 2 == 0)
                {
                    if (pair.Value[0] < topPos)
                    {
                        que.Enqueue(segTreeEven.Scan(pair.Value[0], topPos),
                                    new int[2] {
                            pair.Value[0], topPos
                        });
                    }
                    if (topPos + 1 < lastPos)
                    {
                        que.Enqueue(segTreeOdd.Scan(topPos + 1, lastPos),
                                    new int[2] {
                            topPos + 1, lastPos
                        });
                    }
                    if (lastPos + 1 < pair.Value[1])
                    {
                        que.Enqueue(segTreeEven.Scan(lastPos + 1, pair.Value[1]),
                                    new int[2] {
                            lastPos + 1, pair.Value[1]
                        });
                    }
                }
                else
                {
                    if (pair.Value[0] < topPos)
                    {
                        que.Enqueue(segTreeOdd.Scan(pair.Value[0], topPos),
                                    new int[2] {
                            pair.Value[0], topPos
                        });
                    }
                    if (topPos + 1 < lastPos)
                    {
                        que.Enqueue(segTreeEven.Scan(topPos + 1, lastPos),
                                    new int[2] {
                            topPos + 1, lastPos
                        });
                    }
                    if (lastPos + 1 < pair.Value[1])
                    {
                        que.Enqueue(segTreeOdd.Scan(lastPos + 1, pair.Value[1]),
                                    new int[2] {
                            lastPos + 1, pair.Value[1]
                        });
                    }
                }
            }
            WriteLine();
        }
Пример #14
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            int[][] xys = new int[n][];
            for (int i = 0; i < n; i++)
            {
                xys[i] = ReadInts();
            }
            Array.Sort(xys, (a, b) => a[1] - b[1]);
            for (int i = 0; i < n; i++)
            {
                xys[i][1] = i;
            }
            Array.Sort(xys, (a, b) => a[0] - b[0]);

            long[]        belows  = new long[n];
            SegTree <int> segTree = new SegTree <int>(n, 0, (a, b) => a + b, 0);

            for (int i = 0; i < n; i++)
            {
                belows[i] = segTree.Scan(0, xys[i][1]);
                segTree.Update(xys[i][1], 1);
            }

            long mask = 998244353;

            long[] pows = new long[n + 1];
            pows[0] = 1;
            for (int i = 1; i <= n; i++)
            {
                pows[i]  = pows[i - 1] * 2;
                pows[i] %= mask;
            }

            long res = (pows[n - 1] * n) % mask;

            for (int i = 0; i < n; i++)
            {
                long tmp = pows[n - 1] - 1;
                if (i >= 0)
                {
                    tmp -= pows[i] - 1;
                }
                if (xys[i][1] >= 0)
                {
                    tmp -= pows[xys[i][1]] - 1;
                }
                if (n - i - 1 >= 0)
                {
                    tmp -= pows[n - i - 1] - 1;
                }
                if (n - xys[i][1] - 1 >= 0)
                {
                    tmp -= pows[n - xys[i][1] - 1] - 1;
                }

                while (tmp < 0)
                {
                    tmp += mask;
                }

                tmp += pows[belows[i]] - 1;
                tmp += pows[i - belows[i]] - 1;
                long afterBelows = segTree.Scan(0, xys[i][1]) - belows[i];
                tmp += pows[afterBelows] - 1;
                tmp += pows[n - i - 1 - afterBelows] - 1;
                tmp %= mask;

                res += tmp;
                res %= mask;
            }
            WriteLine(res);
        }
Пример #15
0
        static void Method(string[] args)
        {
            string s    = Read();
            int    cCnt = 26;

            int[] cnts = new int[26];
            for (int i = 0; i < s.Length; i++)
            {
                cnts[s[i] - 'a']++;
            }

            int oddCnt   = 0;
            int oddIndex = 0;

            for (int i = 0; i < cCnt; i++)
            {
                if (cnts[i] % 2 > 0)
                {
                    oddCnt++;
                    oddIndex = i;
                }
            }

            if ((s.Length % 2 == 0 && oddCnt > 0) ||
                (s.Length % 2 > 0 && oddCnt != 1))
            {
                WriteLine(-1);
                return;
            }

            var remain = new Dictionary <int, List <int> >();
            var posNos = new List <int>();

            int[] useCnt    = new int[cCnt];
            int   unusedCnt = 0;
            long  res       = 0;

            for (int i = 0; i < s.Length; i++)
            {
                int index = s[i] - 'a';
                if (useCnt[index] < cnts[index] / 2)
                {
                    res += unusedCnt;
                    useCnt[index]++;
                    posNos.Add(index);
                }
                else
                {
                    if (!remain.ContainsKey(index))
                    {
                        remain.Add(index, new List <int>());
                    }
                    remain[index].Add(unusedCnt);
                    unusedCnt++;
                }
            }
            if (s.Length % 2 == 1)
            {
                posNos.Add(oddIndex);
            }

            SegTree <int> segTree = new SegTree <int>(unusedCnt, (a, b) => a + b, 0);

            for (int i = posNos.Count - 1; i >= 0; i--)
            {
                int pos = remain[posNos[i]][0];
                res += pos - segTree.Scan(0, pos);
                segTree.Update(pos, 1);
                remain[posNos[i]].RemoveAt(0);
            }

            WriteLine(res);
        }
Пример #16
0
        static void Method(string[] args)
        {
            int[] nm  = ReadInts();
            int   n   = nm[0];
            int   m   = nm[1];
            int   max = 1000000;

            long res = 1;

            int[][] ylrs = new int[n][];
            for (int i = 0; i < n; i++)
            {
                ylrs[i] = ReadInts();
                if (ylrs[i][1] == 0 && ylrs[i][2] == max)
                {
                    res++;
                }
            }
            Array.Sort(ylrs, (a, b) => a[0] - b[0]);

            int[][] xyAdds = new int[m][];
            int[][] xyRems = new int[m][];
            for (int i = 0; i < m; i++)
            {
                int[] xlr = ReadInts();
                xyAdds[i] = new int[2] {
                    xlr[0], xlr[1]
                };
                xyRems[i] = new int[2] {
                    xlr[0], xlr[2]
                };
                if (xlr[1] == 0 && xlr[2] == max)
                {
                    res++;
                }
            }
            Array.Sort(xyAdds, (a, b) => a[1] - b[1]);
            Array.Sort(xyRems, (a, b) => a[1] - b[1]);

            SegTree <long> segTree = new SegTree <long>(max + 10, 0, (a, b) => a + b, 0);
            int            idxAdd  = 0;
            int            idxRem  = 0;

            for (int i = 0; i < n; i++)
            {
                while (idxAdd < m && xyAdds[idxAdd][1] <= ylrs[i][0])
                {
                    segTree.Update(xyAdds[idxAdd][0], segTree.Look(xyAdds[idxAdd][0]) + 1);
                    idxAdd++;
                }
                while (idxRem < m && xyRems[idxRem][1] < ylrs[i][0])
                {
                    segTree.Update(xyRems[idxRem][0], segTree.Look(xyRems[idxRem][0]) - 1);
                    idxRem++;
                }

                res += segTree.Scan(ylrs[i][1], ylrs[i][2] + 1);
            }

            WriteLine(res);
        }
Пример #17
0
        static void Method(string[] args)
        {
            long n = ReadLong();

            int[] array  = ReadInts();
            int[] sorted = new int[n];
            for (int i = 0; i < n; i++)
            {
                sorted[i] = array[i];
            }
            Array.Sort(sorted);
            Dictionary <int, int> pos = new Dictionary <int, int>();

            for (int i = 0; i < n; i++)
            {
                if (pos.ContainsKey(sorted[i]))
                {
                    continue;
                }

                pos.Add(sorted[i], i);
            }

            int bottom = 0;
            int top    = (int)n;

            while (bottom + 1 < top)
            {
                int   mid  = (bottom + top) / 2;
                int   val  = sorted[mid];
                int[] sums = new int[n];
                for (int i = 0; i < n; i++)
                {
                    if (val <= array[i])
                    {
                        sums[i] = 1;
                    }
                    else
                    {
                        sums[i] = -1;
                    }

                    if (i > 0)
                    {
                        sums[i] += sums[i - 1];
                    }
                }

                long          cnt  = 0;
                SegTree <int> tree = new SegTree <int>(2 * 100000 + 5, 0, (a, b) => a + b, 0);
                for (int i = (int)n - 1; i >= 0; i--)
                {
                    tree.Update(sums[i] + 100000, tree.Look(sums[i] + 100000) + 1);
                    if (val <= array[i])
                    {
                        cnt += tree.Scan(sums[i] - 1 + 100000, 2 * 100000 + 10);
                    }
                    else
                    {
                        cnt += tree.Scan(sums[i] + 1 + 100000, 2 * 100000 + 10);
                    }
                }

                long thres = n * (n + 1) / 4;
                if ((n * (n + 1) / 2) % 2 > 0)
                {
                    thres++;
                }
                if (cnt >= thres)
                {
                    bottom = mid;
                }
                else
                {
                    top = mid;
                }
            }

            Console.WriteLine(sorted[bottom]);
        }