結果

問題 No.674 n連勤
ユーザー uafr_csuafr_cs
提出日時 2018-04-14 14:45:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,548 ms / 2,000 ms
コード長 4,736 bytes
コンパイル時間 3,011 ms
コンパイル使用メモリ 90,008 KB
実行使用メモリ 87,872 KB
最終ジャッジ日時 2024-06-26 22:09:02
合計ジャッジ時間 14,867 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
54,456 KB
testcase_01 AC 137 ms
54,024 KB
testcase_02 AC 125 ms
53,348 KB
testcase_03 AC 139 ms
54,256 KB
testcase_04 AC 139 ms
54,380 KB
testcase_05 AC 141 ms
54,320 KB
testcase_06 AC 143 ms
54,596 KB
testcase_07 AC 141 ms
54,492 KB
testcase_08 AC 135 ms
54,192 KB
testcase_09 AC 137 ms
54,340 KB
testcase_10 AC 133 ms
54,256 KB
testcase_11 AC 421 ms
60,032 KB
testcase_12 AC 398 ms
59,840 KB
testcase_13 AC 871 ms
63,068 KB
testcase_14 AC 1,235 ms
84,668 KB
testcase_15 AC 1,149 ms
74,936 KB
testcase_16 AC 1,441 ms
87,872 KB
testcase_17 AC 1,438 ms
87,228 KB
testcase_18 AC 1,252 ms
79,504 KB
testcase_19 AC 1,548 ms
86,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
	
	public static class LazySetSumSegmentTree {
		int n;		
		boolean[] dat, lazy;
		
		ArrayList<Long> dist;
		long[] l_max, c_max, r_max;
		
		boolean[] push;

		public LazySetSumSegmentTree(int n_, ArrayList<Long> dist) {
			int n = 1;
			while(n < n_){ n *= 2;} this.n = n;
			
			l_max = new long[this.n * 2 - 1];
			c_max = new long[this.n * 2 - 1];
			r_max = new long[this.n * 2 - 1];
			
			this.dist = dist;
			
			dat = new boolean[this.n * 2 - 1];
			lazy = new boolean[this.n * 2 - 1];
			push = new boolean[this.n * 2 - 1];
		}

		private void evaluate_lazy(int k, int l, int r){
			if(!push[k]){ return; }
			
			dat[k] = lazy[k];
			if(dat[k]){
				//System.out.println(r + " " + l + " -> " + (dist.get(r) - 1) + " " + dist.get(l) + " -> " + ((dist.get(r) - 1) - dist.get(l) + 1));
				c_max[k] = l_max[k] = r_max[k] = (dist.get(r) - dist.get(l));
			}else{
				l_max[k] = c_max[k] = r_max[k] = 0;
			}
			
			if(k < n - 1){
				lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
				push[k * 2 + 1] = push[k * 2 + 2] = true;
			}
			lazy[k] = false; push[k] = false;
		}
		
		private void update_node(int k){ 
			final int l = k * 2 + 1;
			final int r = k * 2 + 2;
			final long l_all_max = Math.max(Math.max(l_max[l], r_max[l]), c_max[l]);
			final long r_all_max = Math.max(Math.max(l_max[r], r_max[r]), c_max[r]);
			final long children_max = Math.max(l_all_max, r_all_max);
			
			c_max[k] = Math.max(children_max, (l_max[r] + r_max[l]));
			l_max[k] = l_max[l] + (dat[l] ? l_max[r] : 0);
			r_max[k] = r_max[r] + (dat[r] ? r_max[l] : 0);
			
			//System.out.println("l -> " + l_max[l] + " " + c_max[l] + " " + r_max[l]);
			//System.out.println("r -> " + l_max[r] + " " + c_max[r] + " " + r_max[r]);
			//System.out.println("k -> " + l_max[k] + " " + c_max[k] + " " + r_max[k]);
			
			dat[k] = dat[k*2+1] && dat[k*2+2];
		}
		
		public void set(boolean v, int a, int b){ set(v, a, b, 0, 0, this.n); }
		public void set(boolean v, int a, int b, int k, int l, int r){
			evaluate_lazy(k, l, r);
			if(r <= a || b <= l){ 
				return;
			}else if(a <= l && r <= b){
				lazy[k] = v; push[k] = true;
				evaluate_lazy(k, l, r);
			}else{
				set(v, a, b, k * 2 + 1, l, (l + r) / 2);
				set(v, a, b, k * 2 + 2, (l + r) / 2, r);
				update_node(k);
			}
		}
		
		public long[] max(int a, int b){ return max(a, b, 0, 0, this.n); }
		public long[] max(int a, int b, int k, int l, int r){
			evaluate_lazy(k, l, r);
			if(r <= a || b <= l){ 
				/* 通らない予定 */
				return new long[]{0, 0, 0};
			}else if(a <= l && r <= b){
				return new long[]{l_max[k], c_max[k], r_max[k]};
			}else{
				final long[] l_max_ret = max(a, b, k * 2 + 1, l, (l + r) / 2);
				final long[] r_max_ret = max(a, b, k * 2 + 2, (l + r) / 2, r);
				
				final long l_all_max = Math.max(Math.max(l_max_ret[0], l_max_ret[1]), l_max_ret[2]);
				final long r_all_max = Math.max(Math.max(r_max_ret[0], r_max_ret[1]), r_max_ret[2]);
				final long children_max = Math.max(l_all_max, r_all_max);
				
				final boolean l_cont_all = l_max_ret[1] == (dist.get((l + r) / 2) - dist.get(l));
				final boolean r_cont_all = r_max_ret[1] == (dist.get(r) - dist.get((l + r) / 2));
				
				final long fst = l_max_ret[0] + Math.max(0, l_cont_all ? r_max_ret[0]: 0);
				final long snd = Math.max(children_max, (l_max_ret[2] + r_max_ret[0]));
				final long thd = r_max_ret[2] + Math.max(0, r_cont_all ? l_max_ret[2]: 0);
				
				update_node(k); 
				return new long[]{fst, snd, thd};
			}
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final long D = sc.nextLong();
		final int Q = sc.nextInt();
		
		long[] as = new long[Q];
		long[] bs = new long[Q];
		TreeSet<Long> coord = new TreeSet<Long>();
		for(int i = 0; i < Q; i++){
			as[i] = sc.nextLong();
			bs[i] = sc.nextLong();
			coord.add(as[i]);
			coord.add(bs[i] + 0);
			coord.add(bs[i] + 1);	
		}
		
		ArrayList<Long> coord_list = new ArrayList<Long>(coord);
		final int size = coord_list.size();
		for(int i = 0; i < size; i++){ coord_list.add(Long.MAX_VALUE); }
		
		
		LazySetSumSegmentTree seg = new LazySetSumSegmentTree(size, coord_list);
		for(int q = 0; q < Q; q++){
			final int a_index = Collections.binarySearch(coord_list, as[q]);
			final int b_index = Collections.binarySearch(coord_list, bs[q] + 1);
			
			//System.out.println(as[q] + " "  + bs[q] + " -> " + (bs[q] - as[q] + 1));
			//System.out.println(a_index + " " + b_index);
			seg.set(true, a_index, b_index);
			System.out.println(Arrays.stream(seg.max(0, size)).max().getAsLong());
		}
	}
}
0