結果

問題 No.230 Splarraay スプラレェーイ
ユーザー uafr_csuafr_cs
提出日時 2017-11-04 09:13:24
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,963 bytes
コンパイル時間 3,667 ms
コンパイル使用メモリ 82,820 KB
実行使用メモリ 81,904 KB
最終ジャッジ日時 2024-11-22 17:00:03
合計ジャッジ時間 20,088 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 176 ms
42,552 KB
testcase_01 AC 179 ms
42,464 KB
testcase_02 AC 180 ms
42,476 KB
testcase_03 AC 180 ms
42,436 KB
testcase_04 AC 178 ms
42,552 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 1,042 ms
70,556 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 1,427 ms
69,292 KB
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

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