Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import static java.lang.Math.*;
- public class BlockFenwick implements Runnable {
- static boolean LOCAL = true;
- BufferedReader in;
- PrintWriter out;
- StringTokenizer tok;
- public void run() {
- try {
- long t1 = 0, t2 = 0;
- if (LOCAL) {
- t1 = System.currentTimeMillis();
- }
- Locale.setDefault(Locale.US);
- // try {
- // in = new BufferedReader(new FileReader("input.txt"));
- // out = new PrintWriter("output.txt");
- // } catch (Exception e) {
- in = new BufferedReader(new InputStreamReader(System.in));
- out = new PrintWriter(System.out);
- // String name = getClass().getSimpleName().toLowerCase();
- // in = new BufferedReader(new FileReader(name + ".in"));
- // out = new PrintWriter(name + ".out");
- LOCAL = false;
- // }
- tok = new StringTokenizer("");
- solve();
- in.close();
- out.close();
- if (LOCAL) {
- t2 = System.currentTimeMillis();
- System.err.println("Time = " + (t2 - t1));
- }
- } catch (Throwable e) {
- e.printStackTrace(System.err);
- System.exit(-1);
- }
- }
- String readString() throws IOException {
- while (!tok.hasMoreTokens()) {
- String line = in.readLine();
- if (line == null) return null;
- tok = new StringTokenizer(line);
- }
- return tok.nextToken();
- }
- int readInt() throws IOException {
- return Integer.parseInt(readString());
- }
- long readLong() throws IOException {
- return Long.parseLong(readString());
- }
- double readDouble() throws IOException {
- return Double.parseDouble(readString());
- }
- void debug(Object... o) {
- if (LOCAL) {
- System.err.println(Arrays.deepToString(o));
- }
- }
- public static void main(String[] args) {
- new Thread(null, new BlockFenwick(), "", 256 * 1024 * 1024).start();
- }
- //------------------------------------------------------------------------------
- class Team implements Comparable<Team> {
- int a, b, c;
- public Team(int a, int b, int c) {
- this.a = a;
- this.b = b;
- this.c = c;
- }
- @Override
- public int compareTo(Team other) {
- return this.c - other.c;
- }
- }
- int n;
- Team[] t;
- int[] a, b;
- int[] posA, posB;
- int blockLen, blockCnt;
- int[][] blockFenwick;
- void update(int x, int y) {
- int xBlock = x / blockLen;
- int yBlock = y / blockLen;
- for (int i = xBlock; i < blockCnt; i |= i+1) {
- for (int j = yBlock; j < blockCnt; j |= j+1) {
- blockFenwick[i][j]++;
- }
- }
- }
- long get(int x, int y) {
- int xBlock = x / blockLen;
- int yBlock = y / blockLen;
- long ans = 0;
- for (int i = xBlock - 1; i >= 0; i = (i & (i+1)) - 1) {
- for (int j = yBlock - 1; j >= 0; j = (j & (j+1)) - 1) {
- ans += blockFenwick[i][j];
- }
- }
- for (int i = xBlock * blockLen; i <= x; i++) {
- int pos = posA[i];
- if (pos != -1 && b[pos] < y) ans++;
- }
- for (int j = yBlock * blockLen; j <= y; j++) {
- int pos = posB[j];
- if (pos != -1 && a[pos] < xBlock * blockLen) ans++;
- }
- return ans;
- }
- void solve() throws IOException {
- n = readInt();
- t = new Team[n];
- for (int i = 0; i < n; i++) {
- t[i] = new Team(readInt() - 1, readInt() - 1, readInt() - 1);
- }
- Arrays.sort(t);
- a = new int[n];
- b = new int[n];
- for (int i = 0; i < n; i++) {
- a[i] = t[i].a;
- b[i] = t[i].b;
- }
- blockLen = (int) sqrt(n + 1e-9);
- blockCnt = (n + blockLen - 1) / blockLen;
- blockFenwick = new int[blockCnt][blockCnt];
- posA = new int[n];
- posB = new int[n];
- Arrays.fill(posA, -1);
- Arrays.fill(posB, -1);
- long ans = 0;
- for (int i = 0; i < n; i++) {
- ans += get(a[i], b[i]);
- update(a[i], b[i]);
- posA[a[i]] = i;
- posB[b[i]] = i;
- }
- out.println((long)n*(n-1)/2 - ans);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement