結果

問題 No.230 Splarraay スプラレェーイ
ユーザー uwiuwi
提出日時 2015-06-19 22:44:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 306 ms / 5,000 ms
コード長 6,309 bytes
コンパイル時間 4,119 ms
コンパイル使用メモリ 83,060 KB
実行使用メモリ 55,760 KB
最終ジャッジ日時 2024-07-07 04:08:10
合計ジャッジ時間 7,731 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
38,516 KB
testcase_01 AC 79 ms
38,000 KB
testcase_02 AC 78 ms
37,904 KB
testcase_03 AC 79 ms
38,592 KB
testcase_04 AC 77 ms
39,756 KB
testcase_05 AC 89 ms
39,888 KB
testcase_06 AC 138 ms
43,112 KB
testcase_07 AC 86 ms
39,684 KB
testcase_08 AC 114 ms
42,304 KB
testcase_09 AC 218 ms
48,956 KB
testcase_10 AC 206 ms
48,500 KB
testcase_11 AC 199 ms
44,992 KB
testcase_12 AC 213 ms
48,724 KB
testcase_13 AC 145 ms
42,676 KB
testcase_14 AC 212 ms
55,504 KB
testcase_15 AC 306 ms
55,396 KB
testcase_16 AC 306 ms
55,448 KB
testcase_17 AC 290 ms
55,760 KB
testcase_18 AC 282 ms
55,524 KB
testcase_19 AC 295 ms
49,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Q625 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni();
		int Q = ni();
		int[][] qs = new int[Q][];
		for(int i = 0;i < Q;i++){
			qs[i] = new int[]{ni(), ni(), ni()};
		}
		
		long[][] count = new long[Q][];
		long sa = 0, sb = 0;
		{
			SegmentTreeOverwriteRMQ st = new SegmentTreeOverwriteRMQ(new int[n]);
			for(int i = 0;i < Q;i++){
				int x = qs[i][0];
				int l = qs[i][1];
				int r = qs[i][2];
				if(x == 0){
					count[i] = new long[]{st.sum(l, r+1), 0};
				}else if(x == 1){
					st.update(l, r+1, 1);
				}else{
					st.update(l, r+1, 0);
				}
			}
			sa += st.st[1];
		}
		{
			SegmentTreeOverwriteRMQ st = new SegmentTreeOverwriteRMQ(new int[n]);
			for(int i = 0;i < Q;i++){
				int x = qs[i][0];
				int l = qs[i][1];
				int r = qs[i][2];
				if(x == 0){
					count[i][1] = st.sum(l, r+1);
				}else if(x == 1){
					st.update(l, r+1, 0);
				}else{
					st.update(l, r+1, 1);
				}
			}
			sb += st.st[1];
		}
		for(int i = 0;i < Q;i++){
			if(count[i] != null){
				if(count[i][0] > count[i][1]){
					sa += count[i][0];
				}else if(count[i][0] < count[i][1]){
					sb += count[i][1];
				}
			}
		}
		out.println(sa + " " + sb);
		
	}
	
	public static class SegmentTreeOverwriteRMQ {
		public int M, H, N;
		public long[] st;
		public int[] cover;
		private int I = Integer.MAX_VALUE;
		
		public SegmentTreeOverwriteRMQ(int[] a)
		{
			N = a.length;
			M = Integer.highestOneBit(Math.max(N-1, 1))<<2;
			H = M>>>1;
			cover = new int[H];
			Arrays.fill(cover, I);
			st = new long[M];
			for(int i = 0;i < N;i++){
				st[H+i] = a[i];
			}
			for(int i = H-1;i >= 1;i--){
				propagate(i);
			}
		}
		
		private void propagate(int i)
		{
			if(cover[i] == I){
				st[i] = st[2*i] + st[2*i+1];
			}else{
				st[i] = (long)cover[i] * (H/Integer.highestOneBit(i));
			}
		}
		
		public void update(int l, int r, int v){ update(l, r, v, 0, H, 1); }
		
		private void update(int l, int r, int v, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				if(cr == cl+1){
					st[cur] = v;
				}else{
					cover[cur] = v;
					propagate(cur);
				}
			}else{
				int mid = cl+cr>>>1;
				boolean bp = false;
				if(cover[cur] != I){ // back-propagate
					if(2*cur >= H){
						int hh = (H/Integer.highestOneBit(2*cur));
						st[2*cur] = (long)cover[cur] * hh;
						st[2*cur+1] = (long)cover[cur] * hh;
					}else{
						cover[2*cur] = cover[2*cur+1] = cover[cur];
						bp = true;
					}
					cover[cur] = I;
				}
				if(cl < r && l < mid){
					update(l, r, v, cl, mid, 2*cur);
				}else if(bp){
					propagate(2*cur);
				}
				if(mid < r && l < cr){
					update(l, r, v, mid, cr, 2*cur+1);
				}else if(bp){
					propagate(2*cur+1);
				}
				propagate(cur);
			}
		}
		
		public long sum(int l, int r){ return sum(l, r, 0, H, 1); }
		
		private long sum(int l, int r, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				return st[cur];
			}else{
				if(cover[cur] != I)return (long)cover[cur] * (Math.min(cr, r) - Math.max(cl, l));
				int mid = cl+cr>>>1;
				long ret = 0;
				if(cl < r && l < mid){
					ret += sum(l, r, cl, mid, 2*cur);
				}
				if(mid < r && l < cr){
					ret += sum(l, r, mid, cr, 2*cur+1);
				}
				return ret;
			}
		}
		
//		public int[] toArray() { return toArray(1, 0, H, new int[H]); }
//		
//		private int[] toArray(int cur, int l, int r, int[] ret)
//		{
//			if(r-l == 1){
//				ret[cur-H] = st[cur];
//			}else if(cover[cur] != I){
//				Arrays.fill(ret, l, r, cover[cur]);
//			}else{
//				toArray(2*cur, l, l+r>>>1, ret);
//				toArray(2*cur+1, l+r>>>1, r, ret);
//			}
//			return ret;
//		}
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
	}
	
	public static void main(String[] args) throws Exception { new Q625().run(); }
	
	private byte[] inbuf = new byte[1024];
	private int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0