結果

問題 No.230 Splarraay スプラレェーイ
ユーザー uwiuwi
提出日時 2015-06-19 22:41:23
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 6,265 bytes
コンパイル時間 3,829 ms
コンパイル使用メモリ 89,960 KB
実行使用メモリ 65,428 KB
最終ジャッジ日時 2024-07-07 04:07:30
合計ジャッジ時間 8,476 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
38,780 KB
testcase_01 AC 85 ms
39,356 KB
testcase_02 AC 79 ms
39,660 KB
testcase_03 AC 85 ms
39,576 KB
testcase_04 AC 80 ms
38,056 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 214 ms
56,624 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 306 ms
55,448 KB
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

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 cover[cur];
				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