コード例 #1
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
     using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
     {
         int n = fs.NextInt();
         Segment[] segms = new Segment[n];
         int[] ans = new int[n];
         for (int i = 0; i < n; i++)
         {
             segms[i] = new Segment { Id = i, Left = fs.NextInt(), Right = fs.NextInt() };
         }
         segms = segms.OrderBy(s => s.Left).ToArray();
         Segment[] t = segms.OrderBy(s => s.Right).ToArray();
         int[] ind = new int[n];
         for (int i = 0; i < n; i++)
         {
             ind[i] = findItem(t, 0, n - 1, segms[i].Right);
         }
         SegmentTree tree = new SegmentTree(new int[n]);
         for (int i = n - 1; i >= 0; i--)
         {
             ans[segms[i].Id] = tree.QuerySum(0, ind[i] - 1);
             tree.Update(ind[i], ind[i], 1);
         }
         for (int i = 0; i < n; i++)
         {
             writer.WriteLine(ans[i]);
         }
     }
 }
コード例 #2
0
 private static int findItem(Segment[] s, int l, int r, int i)
 {
     if (l > r) return -1;
     int mid = (r + l) / 2;
     if (s[mid].Right < i) return findItem(s, mid + 1, r, i);
     else if (s[mid].Right > i) return findItem(s, 0, mid - 1, i);
     else return mid;
 }