Advertisement
Guest User

Untitled

a guest
Oct 12th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. import static java.lang.Math.*;
  5.  
  6. public class BlockFenwick implements Runnable {
  7.  
  8.     static boolean LOCAL = true;
  9.    
  10.     BufferedReader in;
  11.     PrintWriter out;
  12.     StringTokenizer tok;
  13.    
  14.     public void run() {
  15.         try {
  16.             long t1 = 0, t2 = 0;
  17.             if (LOCAL) {
  18.                 t1 = System.currentTimeMillis();
  19.             }
  20.             Locale.setDefault(Locale.US);
  21. //          try {
  22. //              in = new BufferedReader(new FileReader("input.txt"));
  23. //              out = new PrintWriter("output.txt");
  24. //          } catch (Exception e) {
  25.                 in = new BufferedReader(new InputStreamReader(System.in));
  26.                 out = new PrintWriter(System.out);
  27. //              String name = getClass().getSimpleName().toLowerCase();
  28. //              in = new BufferedReader(new FileReader(name + ".in"));
  29. //              out = new PrintWriter(name + ".out");
  30.                 LOCAL = false;
  31. //          }
  32.             tok = new StringTokenizer("");
  33.             solve();
  34.             in.close();
  35.             out.close();
  36.             if (LOCAL) {
  37.                 t2 = System.currentTimeMillis();
  38.                 System.err.println("Time = " + (t2 - t1));
  39.             }
  40.         } catch (Throwable e) {
  41.             e.printStackTrace(System.err);
  42.             System.exit(-1);
  43.         }
  44.     }
  45.    
  46.     String readString() throws IOException {
  47.         while (!tok.hasMoreTokens()) {
  48.             String line = in.readLine();
  49.             if (line == null) return null;
  50.             tok = new StringTokenizer(line);
  51.         }
  52.         return tok.nextToken();
  53.     }
  54.    
  55.     int readInt() throws IOException {
  56.         return Integer.parseInt(readString());
  57.     }
  58.    
  59.     long readLong() throws IOException {
  60.         return Long.parseLong(readString());
  61.     }
  62.    
  63.     double readDouble() throws IOException {
  64.         return Double.parseDouble(readString());
  65.     }
  66.    
  67.     void debug(Object... o) {
  68.         if (LOCAL) {
  69.             System.err.println(Arrays.deepToString(o));
  70.         }
  71.     }
  72.    
  73.     public static void main(String[] args) {
  74.         new Thread(null, new BlockFenwick(), "", 256 * 1024 * 1024).start();
  75.     }
  76.    
  77. //------------------------------------------------------------------------------
  78.  
  79.     class Team implements Comparable<Team> {
  80.         int a, b, c;
  81.  
  82.         public Team(int a, int b, int c) {
  83.             this.a = a;
  84.             this.b = b;
  85.             this.c = c;
  86.         }
  87.        
  88.         @Override
  89.         public int compareTo(Team other) {
  90.             return this.c - other.c;
  91.         }
  92.     }
  93.    
  94.     int n;
  95.     Team[] t;
  96.     int[] a, b;
  97.     int[] posA, posB;
  98.     int blockLen, blockCnt;
  99.     int[][] blockFenwick;
  100.    
  101.     void update(int x, int y) {
  102.         int xBlock = x / blockLen;
  103.         int yBlock = y / blockLen;
  104.         for (int i = xBlock; i < blockCnt; i |= i+1) {
  105.             for (int j = yBlock; j < blockCnt; j |= j+1) {
  106.                 blockFenwick[i][j]++;
  107.             }
  108.         }
  109.     }
  110.    
  111.     long get(int x, int y) {
  112.         int xBlock = x / blockLen;
  113.         int yBlock = y / blockLen;
  114.         long ans = 0;
  115.         for (int i = xBlock - 1; i >= 0; i = (i & (i+1)) - 1) {
  116.             for (int j = yBlock - 1; j >= 0; j = (j & (j+1)) - 1) {
  117.                 ans += blockFenwick[i][j];
  118.             }
  119.         }
  120.         for (int i = xBlock * blockLen; i <= x; i++) {
  121.             int pos = posA[i];
  122.             if (pos != -1 && b[pos] < y) ans++;
  123.         }
  124.         for (int j = yBlock * blockLen; j <= y; j++) {
  125.             int pos = posB[j];
  126.             if (pos != -1 && a[pos] < xBlock * blockLen) ans++;
  127.         }
  128.         return ans;
  129.     }
  130.    
  131.     void solve() throws IOException {
  132.         n = readInt();
  133.         t = new Team[n];
  134.         for (int i = 0; i < n; i++) {
  135.             t[i] = new Team(readInt() - 1, readInt() - 1, readInt() - 1);
  136.         }
  137.         Arrays.sort(t);
  138.         a = new int[n];
  139.         b = new int[n];
  140.         for (int i = 0; i < n; i++) {
  141.             a[i] = t[i].a;
  142.             b[i] = t[i].b;
  143.         }
  144.         blockLen = (int) sqrt(n + 1e-9);
  145.         blockCnt = (n + blockLen - 1) / blockLen;
  146.         blockFenwick = new int[blockCnt][blockCnt];
  147.         posA = new int[n];
  148.         posB = new int[n];
  149.         Arrays.fill(posA, -1);
  150.         Arrays.fill(posB, -1);
  151.         long ans = 0;
  152.         for (int i = 0; i < n; i++) {
  153.             ans += get(a[i], b[i]);
  154.             update(a[i], b[i]);
  155.             posA[a[i]] = i;
  156.             posB[b[i]] = i;
  157.         }
  158.         out.println((long)n*(n-1)/2 - ans);
  159.     }
  160.    
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement