hihocoder 1079离散化 java实现

package tree;


import java.util.HashMap;

import java.util.Scanner;


/*

 * 5 10

4 10

0 2

1 6

5 9

3 4

 */

public class Week21 {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int edgeNum = Integer.parseInt(scanner.nextLine().split(" ")[0]);

int []inputB = new int[edgeNum * 2];

int []inputA = new int[edgeNum * 2];

int inputIndex = 0;

int pasteIndex = 1;

HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

HashMap<Integer, Integer> sum = new HashMap<Integer, Integer>();

for(int i=0; i<edgeNum; i++){

String data = scanner.nextLine();

int horizon = Integer.parseInt(data.split(" ")[0]);

int vertical = Integer.parseInt(data.split(" ")[1]);

inputB[inputIndex++] = horizon;

inputB[inputIndex--] = vertical;

inputA[inputIndex++] = horizon;

inputA[inputIndex++] = vertical;

}

scanner.close();

mergeSort(inputA, 0, inputA.length-1);

maping(inputA, map);

node21 root = creSegTree(0, inputB.length - 1);

for(int i=0; i<inputB.length; i++){

int left = inputB[i++];

int right = inputB[i];

left = map.get(left);

right = map.get(right) - 1;

paste(root, left, right, map, pasteIndex++);

}

dfs(root, sum);

System.out.println(sum.size());

}


public static void mergeSort(int []input, int start, int end){

if(start < end){

int middle = (start + end) / 2;

mergeSort(input, start, middle);

mergeSort(input, middle + 1, end);


int []temp1 = new int[middle - start + 1];

int index1 = 0;

for(int i=0; i<temp1.length; i++) temp1[i] = input[start + i];

int []temp2 = new int[end - middle];

int index2 = 0;

for(int i=0; i<temp2.length; i++) temp2[i] = input[middle + 1 + i];


for(int i=start; i<end+1; i++){


if(temp1[index1] <= temp2[index2]) input[i] = temp1[index1++];

else input[i] = temp2[index2++];

if(index1 == temp1.length){

for(int j=i+1; j<end+1; j++) input[j] = temp2[index2++];

break;

}

else if(index2 == temp2.length){

for(int j=i+1; j<end+1; j++) input[j] = temp1[index1++];

break;

}

}

}

}


public static void maping(int []inputA, HashMap<Integer, Integer> map){

for(int i=0; i<inputA.length; i++){

map.put(inputA[i], i);

}

}

public static node21 creSegTree(int left, int right){

node21 node = new node21(left, right);

if(left != right){

node21 leftchild = creSegTree(left, (left + right) /2);

node21 rightchild = creSegTree((left + right) / 2 + 1, right);

node.leftChild = leftchild;

node.rightChild = rightchild;

return node;

}

else{

node.Value = -1;

return node;

}

}


public static void paste(node21 root, int left, int right, HashMap<Integer, Integer> map, int index){


if(left == root.leftBound && right == root.rightBound){

root.lazyTag = index;

}

else{

if(root.lazyTag != 0){

root.leftChild.lazyTag = root.lazyTag;

root.rightChild.lazyTag = root.lazyTag;

root.lazyTag = 0;

}

if(right < (root.leftBound + root.rightBound) / 2 + 1) paste(root.leftChild, left, right, map, index);

else if (left > (root.leftBound + root.rightBound) / 2) paste(root.rightChild, left, right, map, index);

else{

paste(root.leftChild, left, (root.leftBound + root.rightBound) / 2, map, index);

paste(root.rightChild, (root.leftBound + root.rightBound) / 2 + 1, right, map, index);

}

}

}


public static void dfs(node21 root, HashMap<Integer, Integer> sum){

if(root.lazyTag != 0){

sum.put(root.lazyTag, 1);

return;

}

if(root.leftChild != null) dfs(root.leftChild, sum);

if(root.rightChild != null) dfs(root.rightChild, sum);

}


class node21{

int leftBound;

int rightBound;

int Value;

node21 leftChild;

node21 rightChild;

int lazyTag;

public node21(int leftBound, int rightBound){

this.leftBound = leftBound;

this.rightBound = rightBound;

}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值