Пример #1
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);
                }
            }
        }
Пример #2
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]);
        }
Пример #3
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)));
                    }
                }
            }
        }
Пример #4
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);
        }
Пример #5
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));
        }
Пример #6
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);
        }
Пример #7
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]);
        }