結果

問題 No.230 Splarraay スプラレェーイ
ユーザー uafr_csuafr_cs
提出日時 2017-11-04 09:31:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,406 ms / 5,000 ms
コード長 3,959 bytes
コンパイル時間 2,567 ms
コンパイル使用メモリ 81,056 KB
実行使用メモリ 70,496 KB
最終ジャッジ日時 2024-05-02 05:49:43
合計ジャッジ時間 17,031 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 160 ms
42,204 KB
testcase_01 AC 160 ms
42,312 KB
testcase_02 AC 154 ms
42,316 KB
testcase_03 AC 141 ms
42,140 KB
testcase_04 AC 154 ms
42,404 KB
testcase_05 AC 242 ms
44,248 KB
testcase_06 AC 391 ms
48,840 KB
testcase_07 AC 239 ms
45,352 KB
testcase_08 AC 276 ms
48,176 KB
testcase_09 AC 1,017 ms
58,500 KB
testcase_10 AC 1,129 ms
56,700 KB
testcase_11 AC 787 ms
53,092 KB
testcase_12 AC 1,004 ms
58,600 KB
testcase_13 AC 430 ms
49,528 KB
testcase_14 AC 966 ms
70,372 KB
testcase_15 AC 1,213 ms
69,108 KB
testcase_16 AC 1,365 ms
70,232 KB
testcase_17 AC 1,406 ms
70,452 KB
testcase_18 AC 1,254 ms
69,248 KB
testcase_19 AC 1,056 ms
70,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	public static class LazySegTree{
		int n;
		long[] plus_sum, minus_sum;
		long[] lazy_plus, lazy_minus;
		int[] sign;

		public LazySegTree(int n_) {
			int n = 1;
			while(n < n_){ n *= 2;} this.n = n;
			
			plus_sum  = new long[this.n * 2 - 1];
			minus_sum = new long[this.n * 2 - 1];
			lazy_plus  = new long[this.n * 2 - 1];
			lazy_minus = new long[this.n * 2 - 1];
			sign = new int[this.n * 2 - 1];
			
			for(int i = 0; i < this.n * 2 - 1 ; i++){
				plus_sum[i] = 0;
				lazy_plus[i] = 0;
				
				minus_sum[i] = 0;
				lazy_minus[i] = 0;
				sign[i] = 0;
			}
		}
		
		private void evaluate_node(int k, int a, int b){
			if(sign[k] == 0){ return; }
			
			if(sign[k] == 1){
				plus_sum[k]  = lazy_plus[k] * (b - a);
				minus_sum[k] = 0;
			}else if(sign[k] == -1){
				plus_sum[k]  = 0;
				minus_sum[k] = lazy_minus[k] * (b - a);
			}
			
			if(k < n - 1){
				sign[k * 2 + 1] = sign[k];
				sign[k * 2 + 2] = sign[k];
				
				if(sign[k] == 1){
					lazy_plus[k * 2 + 1]  = lazy_plus[k];
					lazy_plus[k * 2 + 2]  = lazy_plus[k];
					lazy_minus[k * 2 + 1] = 0;
					lazy_minus[k * 2 + 2] = 0;
				}else if(sign[k] == -1){
					lazy_plus[k * 2 + 1]  = 0;
					lazy_plus[k * 2 + 2]  = 0;
					lazy_minus[k * 2 + 1] = lazy_minus[k];
					lazy_minus[k * 2 + 2] = lazy_minus[k]; 
				}
			}
			
			sign[k] = 0;
			lazy_plus[k] = lazy_minus[k] = 0;
		}
		
		public void update_node(int k){
			plus_sum[k] = plus_sum[2 * k + 1] + plus_sum[2 * k + 2];
			minus_sum[k] = minus_sum[2 * k + 1] + minus_sum[2 * k + 2];
		}
		
		public void update(long v, int a, int b, int k, int l, int r){
			evaluate_node(k, l, r);
			final int signum = Long.signum(v);
			
			if(r <= a || b <= l){ return; 
			}else if(a <= l && r <= b){
				sign[k] = signum;
				if(signum == 1){
					lazy_plus[k]  = v;
					lazy_minus[k] = 0;
				}else if(signum == -1){
					lazy_plus[k]  = 0;
					lazy_minus[k] = v;
				}
				evaluate_node(k, l, r);
			}else{
				update(v, a, b, 2 * k + 1, l, (l + r) / 2);
				update(v, a, b, 2 * k + 2, (l + r) / 2, r);
				update_node(k);
			}			
		}
		
		public long get_plus(int a, int b, int k, int l, int r){
			evaluate_node(k, l, r);

			if(r <= a || b <= l){ return 0;
			}else if(a <= l && r <= b){ return plus_sum[k];
			}else {
				long v1 = get_plus(a, b, k * 2 + 1, l, (l + r) / 2);
				long v2 = get_plus(a, b, k * 2 + 2, (l + r) / 2, r);
				update_node(k); return v1 + v2;
			}
		}

		public long get_minus(int a, int b, int k, int l, int r){
			evaluate_node(k, l, r);

			if(r <= a || b <= l){ return 0;
			}else if(a <= l && r <= b){ return minus_sum[k];
			}else {
				long v1 = get_minus(a, b, k * 2 + 1, l, (l + r) / 2);
				long v2 = get_minus(a, b, k * 2 + 2, (l + r) / 2, r);
				update_node(k); return v1 + v2;
			}
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int Q = sc.nextInt();
		
		long a_score = 0, b_score = 0;
		
		LazySegTree seg = new LazySegTree(N);
		for(int i = 0; i < Q; i++){
			final int x = sc.nextInt();
			final int l = sc.nextInt();
			final int r = sc.nextInt() + 1;
			
			if(x == 0){
				final long a_team = seg.get_plus(l, r, 0, 0, seg.n);
				final long b_team = -seg.get_minus(l, r, 0, 0, seg.n);
				
				if(a_team > b_team){
					a_score += a_team;
				}else if(a_team < b_team){
					b_score += b_team;
				}
				
				//System.out.println(a_team + " " + b_team);
			}else if(x == 1){
				seg.update( 1, l, r, 0, 0, seg.n);
			}else if(x == 2){
				seg.update(-1, l, r, 0, 0, seg.n);
			}
			
			//System.out.println(i + " plus  " + seg.get_plus(0, N, 0, 0, seg.n));
			//System.out.println(i + " minus " + seg.get_minus(0, N, 0, 0, seg.n));
		}
		
		a_score += seg.get_plus(0, N, 0, 0, seg.n);
		b_score -= seg.get_minus(0, N, 0, 0, seg.n);
		
		System.out.println(a_score + " " + b_score);
	}
}
0