题目1407:快速找出最小数 线段树

题目1407:快速找出最小数

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:678

解决:82

题目描述:

给定一个大小为N 的整数数组array,我们定义两种操作:

1)       Add(L, R, W)。即将子数组[L, R]中的元素,都累加一个整数W。

2)       Min(L, R)。即返回子数组[L, R]之中,最小的一个元素的值。

其中L和R为数组的下标,且从0开始计数。当数组下标L > R时,我们认为这个子数组的元素包含array[L], array[L+1], … array[N – 1], array[0], array[1], … array[R]。

输入:

每个测试文件包含多个测试案例。

每个测试案例的第一行为整数N, 其中1 <= N <= 100000, 表示数组array的元素个数。

第二行将会输入N个数组元素a1, a2,… an,且-10^6 <= ai <= 10^6。

第三行将输入整数M,其中 100000 <= M <= 100000,代表接下来将会输入的操作数

余下的M行将会是具体的对数组的操作指令:若一行中含有3个整数参数,则该操作为Add操作,需要对array的元素进行更新操作,三个参数依次代表L, R以及W;若一行中只有两个参数,则该操作为Min操作,你需要返回当前子数组[L, R]之中最小的元素大小。对于所有的操作指令, 都满足0 <= L, R <= N – 1, -10^6 <= W <= 10^6。

输出:

对于每个测试案例中的所有Min操作,都返回其指定的操作值。

样例输入:
31 2 430 20 0 10 2
样例输出:
12
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;


public class Main {
    public static void main(String[] args) {
	    new Task().solve() ;  	
	}
}

class Task{
	InputReader in = new InputReader(System.in) ;
	PrintWriter out = new PrintWriter(System.out) ;
	
	long[] min , add , value ;
	
	void up(int t){
		min[t] = Math.min(min[t<<1] , min[t<<1|1]) ;
	}
	
	void down(int t){
		if(add[t] != 0){
			min[t<<1] += add[t] ;
			add[t<<1] += add[t] ;
			min[t<<1|1] += add[t] ;
			add[t<<1|1] += add[t] ;
			add[t] = 0 ;
		}
	}
	
	void build(int l , int r , int t){
		add[t] = 0 ;
		if(l == r){
			min[t] = value[l] ;
			return ;
		}
		int mid = (l + r) >> 1 ;
		build(l , mid , t<<1) ;
		build(mid+1 , r , t<<1|1);
		up(t);
	}
	
	void add(int L , int R , long c , int l , int r , int t){
		if(L <= l && r <= R){
			add[t] += c ;
			min[t] += c ;
			return ;
		}
		down(t) ;
		int mid = (l + r) >> 1 ;
		if(L <= mid) add(L, R, c, l, mid , t<<1) ;
		if(R > mid) add(L , R , c , mid+1 , r , t<<1|1) ;
		up(t) ;
	}
	
	long ask(int L , int R , int l , int r , int t){
	    if(L <= l && r <= R) return min[t] ;
	    down(t);
	    int mid = (l + r) >> 1 ;
	    long res = Long.MAX_VALUE ;
	    if(L <= mid) res = Math.min(res , ask(L, R, l, mid, t<<1)) ;
	    if(R > mid) res = Math.min(res , ask(L, R, mid+1, r, t<<1|1)) ;
	    return res ; 
	}
	
	void solve(){
		while(in.hasNext()){
			int n = in.nextInt() ;
			min = new long[n<<2] ;
			add = new long[n<<2] ;
			value = new long[n+1] ;
			for(int i = 1 ; i <= n ; i++) value[i] = in.nextLong() ;
			build(1, n, 1);
			int m = in.nextInt() ;
			while(m-- > 0){
				String[] args = in.nextLine().split(" ") ;
				String[] params = new String[3] ;
				int size = 0 ;
				for(String s : args){
					if(s == null || "".equals(s.trim())) continue ;
					params[size++] = s ;
				}
				if(size == 2){ 
					int L = Integer.parseInt(params[0])+1 ;
					int R = Integer.parseInt(params[1])+1 ;
					if(L <= R) out.println(ask(L, R, 1, n, 1)) ;
					else out.println(Math.min(ask(1, R, 1, n, 1) , ask(L, n, 1, n, 1) ));
				}
				else{
					int L = Integer.parseInt(params[0])+1 ;
					int R = Integer.parseInt(params[1])+1 ;
					long c = Long.parseLong(params[2]) ;
					if(L <= R) add(L, R, c, 1, n, 1) ;
					else{
						add(1, R, c, 1, n, 1) ;
						add(L, n, c, 1, n, 1) ;
					}
				}
			}
		//	out.flush();
		}
		out.flush();
	}
}


class InputReader {  
    public BufferedReader reader;  
    public StringTokenizer tokenizer;  
  
    public InputReader(InputStream stream) {  
        reader = new BufferedReader(new InputStreamReader(stream), 32768);  
        tokenizer = new StringTokenizer("");  
    }  
  
    private void eat(String s) {  
        tokenizer = new StringTokenizer(s);  
    }  
  
    public String nextLine() {  
        try {  
            return reader.readLine();  
        } catch (Exception e) {  
            return null;  
        }  
    }  
  
    public boolean hasNext() {  
        while (!tokenizer.hasMoreTokens()) {  
            String s = nextLine();  
            if (s == null)  
                return false;  
            eat(s);  
        }  
        return true;  
    }  
  
    public String next() {  
        hasNext();  
        return tokenizer.nextToken();  
    }  
  
    public int nextInt() {  
        return Integer.parseInt(next());  
    }  
  
    public long nextLong() {  
        return Long.parseLong(next());  
    }  
  
    public double nextDouble() {  
        return Double.parseDouble(next());  
    }  
  
    public BigInteger nextBigInteger() {  
        return new BigInteger(next());  
    }  
  
}  




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值