Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- public class D {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- int m = in.nextInt();
- int q = in.nextInt();
- ArrayList<Integer>[][] swaps = new ArrayList[n][m];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- swaps[i][j] = new ArrayList<Integer>();
- }
- }
- ArrayList<Integer>[] rowSwaps = new ArrayList[n];
- int u = (int)Math.sqrt(n);
- int v = (int)Math.sqrt(m);
- int[] rowBins = new int[(n-1)/u + 1];
- int[][] colBins = new int[rowBins.length][(m-1)/v + 1];
- for( int i = 0; i < rowBins.length; i++ )
- {
- rowBins[i] = -1;
- }
- for( int i = 0; i < colBins.length; i++ )
- {
- for( int j = 0; j < colBins[i].length; j++ )
- {
- colBins[i][j] = -1;
- }
- }
- for (int i = 0; i < n; i++) {
- rowSwaps[i] = new ArrayList<Integer>();
- }
- int books = 0;
- ArrayList<Integer> answers = new ArrayList<Integer>();
- answers.add(0);
- for (int t = 1; t <= q; t++) {
- int type = in.nextInt();
- int i = 0, j = 0, k = 0;
- if( type <= 2 )
- {
- i = in.nextInt() - 1;
- j = in.nextInt() - 1;
- int rBin = i / u;
- if( rowBins[rBin] != -1 )
- {
- for( int c = (i/u) * u; c / u == rBin; c++ )
- {
- int sm = leq(rowSwaps[c], rowBins[rBin] );
- if( (sm % 2) != (rowSwaps[c].size() % 2) )
- {
- rowSwaps[c].add(sm, rowBins[rBin]);
- }
- }
- }
- rowBins[rBin] = -1;
- int cBin = j / v;
- if( colBins[rBin][cBin] != -1 )
- {
- for( int c = (j / v) * v; c / v == cBin; c++ )
- {
- int sm = leq( swaps[i][c], colBins[rBin][cBin] );
- if( (sm % 2) != (swaps[i][c].size() % 2) )
- {
- swaps[i][c].add(sm, colBins[rBin][cBin]);
- }
- }
- }
- colBins[rBin][cBin] = -1;
- }
- if( type == 1 )
- {
- // place i,j
- if ((swaps[i][j].size() + rowSwaps[i].size()) % 2 == 0) {
- swaps[i][j].add(t);
- books++;
- }
- } else if( type == 2 )
- {
- // remove i,j
- if ((swaps[i][j].size() + rowSwaps[i].size()) % 2 != 0) {
- swaps[i][j].add(t);
- books--;
- }
- } else if( type == 3 ) {
- // invert row i
- i = in.nextInt() - 1;
- // 0 1 2 3 4 5 6 7 8 9
- // 0 0 0 1 1 1 2 2 2 3
- // u = 3
- int bin = i / u;
- if( rowBins[bin] != -1 )
- {
- for( int c = (i/u) * u; c / u == bin; c++ )
- {
- int sm = leq(rowSwaps[c], rowBins[bin] );
- if( (sm % 2) != (rowSwaps[c].size() % 2) )
- {
- rowSwaps[c].add(sm, rowBins[bin]);
- }
- }
- }
- rowBins[bin] = -1;
- for( int c = 0; c < m; c++ )
- {
- if( (swaps[i][c].size() + rowSwaps[i].size()) % 2 == 0 )
- {
- books++;
- } else
- {
- books--;
- }
- }
- rowSwaps[i].add(t);
- } else {
- // go back to state k
- k = in.nextInt();
- for( int c = 0; c < rowBins.length; c++ )
- {
- rowBins[c] = k;
- }
- for( int c = 0; c < colBins.length; c++ )
- {
- for( int d = 0; d < colBins[c].length; d++ )
- {
- colBins[c][d] = k;
- }
- }
- books = answers.get(k);
- }
- answers.add(books);
- System.out.println(books);
- }
- }
- static int leq(ArrayList<Integer> arr, int val) {
- if( arr.size() == 0 )
- return 0;
- // [1, 2, 4], 0, 2
- // 1, 2
- // 1
- int l = 0;
- int r = arr.size() - 1;
- while (l != r) {
- int mid = (l + r) / 2;
- if (arr.get(mid) > val) {
- r = mid;
- } else if (arr.get(mid) < val) {
- l = mid;
- } else {
- return mid;
- }
- }
- if (arr.get(l) > val) {
- return l;
- } else {
- return l + 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement