示例#1
0
        public static void Solve(int t)
        {
            SegmentTreeCompare treec = new SegmentTreeCompare(cs, (i, j) => Math.Max(i, j));
            SegmentTreeCompare treed = new SegmentTreeCompare(ds, (i, j) => Math.Max(i, j));

            long res = 0;
            Dictionary <int, int> dic = new Dictionary <int, int>();

            for (int i = 0; i < n; i++)
            {
                int l, r, lm, rm, bl = -1;

                if (dic.ContainsKey(cs[i]))
                {
                    bl         = dic[cs[i]];
                    dic[cs[i]] = i;
                }
                else
                {
                    dic[cs[i]] = i;
                }

                l = BSearchL(treec, treed, bl + 1, i, cs[i], cs[i] + k + 1);
                r = BSearchR(treec, treed, i, n - 1, cs[i], cs[i] + k + 1);

                lm = BSearchL(treec, treed, bl + 1, i, cs[i], cs[i] - k);
                rm = BSearchR(treec, treed, i, n - 1, cs[i], cs[i] - k);

                res += (r - i + 1) * (i - l + 1);
                res -= (rm - i + 1) * (i - lm + 1);
            }

            Output(t, res.ToString());
        }
示例#2
0
        static int BSearchR(SegmentTreeCompare treec, SegmentTreeCompare treed, int l, int r, int max, int max2)
        {
            int ii = l;

            while (r >= l)
            {
                int mid = (l + r) / 2;
                if (treec.Value(ii, mid) <= max && treed.Value(ii, mid) < max2)
                {
                    l = mid + 1;
                }
                else
                {
                    r = mid - 1;
                }
            }

            return(r);
        }
示例#3
0
        static int BSearchL(SegmentTreeCompare treec, SegmentTreeCompare treed, int l, int r, int max, int max2)
        {
            int ii = r;

            while (r >= l)
            {
                int mid = (l + r) / 2;
                if (treec.Value(mid, ii) <= max && treed.Value(mid, ii) < max2)
                {
                    r = mid - 1;
                }
                else
                {
                    l = mid + 1;
                }
            }

            return(l);
        }