結果

問題 No.879 Range Mod 2 Query
ユーザー 37zigen37zigen
提出日時 2020-08-10 16:36:13
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 2,868 bytes
コンパイル時間 2,375 ms
コンパイル使用メモリ 79,496 KB
実行使用メモリ 607,816 KB
最終ジャッジ日時 2024-04-16 20:05:15
合計ジャッジ時間 10,365 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
47,248 KB
testcase_01 AC 246 ms
46,852 KB
testcase_02 AC 267 ms
46,988 KB
testcase_03 AC 258 ms
47,428 KB
testcase_04 AC 281 ms
48,996 KB
testcase_05 AC 214 ms
44,864 KB
testcase_06 AC 209 ms
45,000 KB
testcase_07 AC 269 ms
47,580 KB
testcase_08 AC 272 ms
47,204 KB
testcase_09 AC 216 ms
45,444 KB
testcase_10 AC 243 ms
47,720 KB
testcase_11 MLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	class Tree {
		int n=1;
		int n_;
		long[] sum;
		long[] sum_parity;		
		ArrayDeque<Long>[] lazy;
		long erase=-1;
		
		public Tree(int n_) {
			while (n<n_) n*=2;
			this.n_=n_;
			sum=new long[2*n-1];
			sum_parity=new long[2*n-1];
			lazy=new ArrayDeque[2*n-1];
			for (int i=0;i<lazy.length;++i) lazy[i]=new ArrayDeque<Long>();
		}
		
		void push(int k,int l,int r) {
			while (!lazy[k].isEmpty()) {
				long key=lazy[k].pollFirst();
				if (key==erase) {
					sum[k]=sum_parity[k];
				} else {
					sum[k]+=key*(r-l);
					if (key%2==1) {
						sum_parity[k]=r-l-sum_parity[k];
					}
				}
				if (2*k+1<sum.length) {
					if (key==erase) {
						while (!lazy[2*k+1].isEmpty() && lazy[2*k+1].peekLast()==erase) {
							lazy[2*k+1].pollLast();
						}
						while (!lazy[2*k+1].isEmpty() && lazy[2*k+1].peekFirst()==erase) {
							lazy[2*k+1].pollFirst();
						}
						while (!lazy[2*k+2].isEmpty() && lazy[2*k+2].peekLast()==erase) {
							lazy[2*k+2].pollLast();
						}
						while (!lazy[2*k+2].isEmpty() && lazy[2*k+2].peekLast()==erase) {
							lazy[2*k+2].pollLast();
						}
						lazy[2*k+1].addLast(erase);
						lazy[2*k+2].addLast(erase);
					} else {
						if (!lazy[2*k+1].isEmpty() && lazy[2*k+1].peekLast()!=erase) {
							lazy[2*k+1].add(lazy[2*k+1].pollLast()+key);
						} else {
							lazy[2*k+1].add(key);
						}
						if (!lazy[2*k+2].isEmpty() && lazy[2*k+2].peekLast()!=erase) {
							lazy[2*k+2].add(lazy[2*k+2].pollLast()+key);
						} else {
							lazy[2*k+2].add(key);
						}
					}
				}
			}
		}
		
		void erase(int a,int b) {
			query(0,n,a,b,0,erase);
		}
		
		void add(int a,int b,long val) {
			query(0,n,a,b,0,val);
		}
		
		long query(int a,int b) {
			return query(0,n,a,b,0,0);
		}
		
		long query(int l,int r,int a,int b,int k,long add) {
			push(k,l,r);
			if (r<=a||b<=l) return 0;
			else if (a<=l&&r<=b) {
				lazy[k].addLast(add);
				push(k,l,r);
				return sum[k];
			} else {
				long vl=query(l,(l+r)/2,a,b,2*k+1,add);
				long vr=query((l+r)/2,r,a,b,2*k+2,add);
				sum[k]=sum[2*k+1]+sum[2*k+2];
				sum_parity[k]=sum_parity[2*k+1]+sum_parity[2*k+2];
				return vl+vr;
			}
		}
		
	}
	
	void run() {
		Scanner sc = new Scanner(System.in);
		int N=sc.nextInt();
		int Q=sc.nextInt();
		long[] A=new long[N];
		Tree tree=new Tree(N);
		for (int i=0;i<N;++i) {
			A[i]=sc.nextLong();
			tree.add(i, i+1, A[i]);
		}
		PrintWriter pw=new PrintWriter(System.out);
		for (int i=0;i<Q;++i) {
			int t=sc.nextInt();
			int l=sc.nextInt()-1;
			int r=sc.nextInt();
			if (t==1) {
				tree.erase(l,r);
			} else if (t==2) {
				long x=sc.nextLong();
				tree.add(l, r, x);
			} else {
				pw.println(tree.query(l, r));
			}
		}
		pw.flush();
	}
}
0