hihocoder 1080更为复杂的买卖房屋姿势 java实现

package tree;


import java.util.Scanner;


/*

 * 10 6

3195 2202 4613 3744 2892 4858 619 5079 9478 7366 8942 

0 1 6 886

1 0 2 9710

1 0 10 7980

0 4 9 -7594

0 2 8 1581

0 4 4 -1010

 */

public class Week22 {


private static int inputIndex = 0;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String indexData = scanner.nextLine();

String valueData = scanner.nextLine();

node22 root = creSegTree(0, Integer.parseInt(indexData.split(" ")[0]), valueData);


for(int i=0; i<Integer.parseInt(indexData.split(" ")[1]); i++){

String queryData = scanner.nextLine();

int type = Integer.parseInt(queryData.split(" ")[0]);

if(type == 1){ 

goverUpdate(root, Integer.parseInt(queryData.split(" ")[1]), Integer.parseInt(queryData.split(" ")[2]), Integer.parseInt(queryData.split(" ")[3]));

System.out.println(root.sumValue);

}

if(type == 0){ 

marketUpdate(root, Integer.parseInt(queryData.split(" ")[1]), Integer.parseInt(queryData.split(" ")[2]), Integer.parseInt(queryData.split(" ")[3]));

System.out.println(root.sumValue);

}

}

scanner.close();

}


public static node22 creSegTree(int leftBound, int rightBound, String valueData){

node22 node = new node22(leftBound, rightBound);

if(leftBound != rightBound){

node22 leftChild = creSegTree(leftBound, (leftBound + rightBound) / 2, valueData);

node22 rightChild = creSegTree((leftBound + rightBound) / 2 + 1, rightBound, valueData);

node.leftChild = leftChild;

node.rightChild = rightChild;

node.sumValue = leftChild.sumValue + rightChild.sumValue;

return node;

}

else{

node.sumValue = Integer.parseInt(valueData.split(" ")[inputIndex++]);

return node;

}

}


public static void goverUpdate(node22 root, int leftBound, int rightBound, int newValue){

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

root.sumValue = (root.rightBound - root.leftBound + 1) * newValue;

root.lazyTagforG = newValue;

root.lazyTagforM = 0;

}

else{

if(root.lazyTagforG != 0){

root.leftChild.lazyTagforG = root.lazyTagforG;

root.leftChild.lazyTagforM = 0;

root.leftChild.sumValue = (root.leftChild.rightBound - root.leftChild.leftBound + 1) * root.lazyTagforG;

root.rightChild.lazyTagforG = root.lazyTagforG;

root.rightChild.lazyTagforM = 0;

root.rightChild.sumValue = (root.rightChild.rightBound - root.rightChild.leftBound + 1) * root.lazyTagforG;

root.lazyTagforG = 0;

}

if(root.lazyTagforM != 0){

root.leftChild.lazyTagforM += root.lazyTagforM;

root.leftChild.sumValue += (root.leftChild.rightBound - root.leftChild.leftBound + 1) * root.lazyTagforM;

root.rightChild.lazyTagforM += root.lazyTagforM;

root.rightChild.sumValue += (root.rightChild.rightBound - root.rightChild.leftBound + 1) * root.lazyTagforM;

root.lazyTagforM = 0;

}

if(rightBound < (root.leftBound + root.rightBound) / 2 + 1){ 

goverUpdate(root.leftChild, leftBound, rightBound, newValue);

root.sumValue = root.leftChild.sumValue + root.rightChild.sumValue;

}

else if(leftBound > (root.leftBound + root.rightBound) / 2){ 

goverUpdate(root.rightChild, leftBound, rightBound, newValue);

root.sumValue = root.leftChild.sumValue + root.rightChild.sumValue;

}

else{

goverUpdate(root.leftChild, leftBound, (root.leftBound + root.rightBound) / 2, newValue);

goverUpdate(root.rightChild, (root.leftBound + root.rightBound) / 2 + 1, rightBound, newValue);

root.sumValue = root.leftChild.sumValue + root.rightChild.sumValue;

}

}

}


public static void marketUpdate(node22 root, int leftBound, int rightBound, int newValue){

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

if(root.lazyTagforG != 0){

root.sumValue += (root.rightBound - root.leftBound + 1) * newValue;

root.lazyTagforG += newValue;

}

else{

root.sumValue += (root.rightBound - root.leftBound + 1) * newValue;

root.lazyTagforM += newValue;

}

}

else{

if(root.lazyTagforG != 0){

root.leftChild.lazyTagforG = root.lazyTagforG;

root.leftChild.lazyTagforM = 0;

root.leftChild.sumValue = (root.leftChild.rightBound - root.leftChild.leftBound + 1) * root.lazyTagforG;

root.rightChild.lazyTagforG = root.lazyTagforG;

root.rightChild.lazyTagforM = 0;

root.rightChild.sumValue = (root.rightChild.rightBound - root.rightChild.leftBound + 1) * root.lazyTagforG;

root.lazyTagforG = 0;

}

if(root.lazyTagforM != 0){

root.leftChild.lazyTagforM += root.lazyTagforM;

root.leftChild.sumValue += (root.leftChild.rightBound - root.leftChild.leftBound + 1) * root.lazyTagforM;

root.rightChild.lazyTagforM += root.lazyTagforM;

root.rightChild.sumValue += (root.rightChild.rightBound - root.rightChild.leftBound + 1) * root.lazyTagforM;

root.lazyTagforM = 0;

}

if(rightBound < (root.leftBound + root.rightBound) / 2 + 1){ 

marketUpdate(root.leftChild, leftBound, rightBound, newValue);

root.sumValue = root.leftChild.sumValue + root.rightChild.sumValue;

}

else if(leftBound > (root.leftBound + root.rightBound) / 2){ 

marketUpdate(root.rightChild, leftBound, rightBound, newValue);

root.sumValue = root.leftChild.sumValue + root.rightChild.sumValue;

}

else{

marketUpdate(root.leftChild, leftBound, (root.leftBound + root.rightBound) / 2, newValue);

marketUpdate(root.rightChild, (root.leftBound + root.rightBound) / 2 + 1, rightBound, newValue);

root.sumValue = root.leftChild.sumValue + root.rightChild.sumValue;

}

}

}

}


class node22{

int leftBound;

int rightBound;

int sumValue;

int lazyTagforG;

int lazyTagforM;

node22 leftChild;

node22 rightChild;

public node22(int leftBound, int rightBound){

this.leftBound = leftBound;

this.rightBound = rightBound;

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值