結果

問題 No.255 Splarrraaay スプラーレェーーイ
ユーザー koyumeishikoyumeishi
提出日時 2015-07-25 06:00:33
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 5,667 bytes
コンパイル時間 1,023 ms
コンパイル使用メモリ 87,932 KB
実行使用メモリ 814,688 KB
最終ジャッジ日時 2024-07-16 04:42:01
合計ジャッジ時間 5,599 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:236:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  236 |         scanf("%lld%lld", &n,&q);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp:253:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  253 |                 scanf("%lld%lld%lld", &x,&l,&r);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
#include <stack>
using namespace std;

struct unko{
	long long add;
	bool zero;

	unko(long long add_, bool zero_) : add(add_), zero(zero_) {

	}
	unko():add(0),zero(false){

	}

	long long get(){
		if(zero) add = 0;
		return add;
	}

	unko operator +(const unko& x){
		unko ret;

		if(x.zero){	//zero fill
			ret.zero = true;
			return ret;
		}

		//add
		if(zero){
			add = 0;
			zero = false;
		}
		ret.add = add + x.add;
		return ret;
	}

	long long operator *(const long long& x){
		if(zero) return 0;
		return add * x;
	}
};

//V := type of node value
//U := type of lazy value
template<class V=int, class U=int>
class Dynamic_SegmentTree{
	struct node{	//[l,r)
		long long lb;
		long long ub;
		node* left;
		node* right;
		V val;
		U lazy;
		bool is_lazy;
		node(long long l, long long r, V v, U u) : 
			lb(l), ub(r),
			left(nullptr), right(nullptr),
			val(v), lazy(u),
			is_lazy(false)
		{
		}
	};

	vector<node*> pool;
	node* root;
	V default_v;
	U default_l;

	//range add
	V evaluate_vv(V a, V b){
		return a+b;
	}
	U evaluate_ll(U& a, U& b){
		return a+b;
	}
	V evaluate_vl(node* t){
		if(t->ub - t->lb == 1){
			if(t->is_lazy) return t->lazy.get();
			return t->val;
		}else{
			if(t->is_lazy){
				if(t->lazy.zero) t->val = 0;
				return t->val + t->lazy * (t->ub - t->lb);
			}
			return t->val;
		}
	}


	inline void set_children(node* t){
		if(t->left == nullptr){
			t->left = new node(t->lb, (t->lb+t->ub)/2, default_v, default_l);
			t->right = new node((t->lb+t->ub)/2, t->ub, default_v, default_l);

			pool.push_back(t->left);
			pool.push_back(t->right);

		}
	}

	void propagete(node* t){
		if(t->is_lazy == false) return;
		if(t->ub - t->lb == 1){
			t->val = evaluate_vl(t);
		}else{
			set_children(t);
			for(node* ch : {t->left, t->right}){
				if(ch->is_lazy){
					ch->lazy = evaluate_ll(ch->lazy, t->lazy);
				}else{
					ch->lazy = t->lazy;
				}
				ch->is_lazy = true;

				//ch->val = default_v;	//range fill
			}

		}

		t->lazy = default_l;
		t->is_lazy = false;
		if(t->left != nullptr) t->val = evaluate_vv(evaluate_vl(t->left), evaluate_vl(t->right));
	}

	void range_update(node* t, long long lb, long long ub, U new_val){
		if(ub <= t->lb || t->ub <= lb) return;	//out of range
		propagete(t);
		if(lb <= t->lb && t->ub <= ub){	// fully covered
			t->lazy = new_val;
			t->is_lazy = true;
			
			//t->val = default_v;	//range fill
			return;
		}

		//partially covered
		set_children(t);

		range_update(t->left, lb,ub, new_val);
		range_update(t->right, lb,ub, new_val);

		t->val = evaluate_vv(evaluate_vl(t->left), evaluate_vl(t->right));
	}

	V range_evaluate(node* t, long long lb, long long ub){

		if(ub <= t->lb || t->ub <= lb) return default_v;	//out of range
		propagete(t);

		if(lb <= t->lb && t->ub <= ub){	// fully covered
			return evaluate_vl(t);
		}

		//partially covered
		set_children(t);

		V l_val = range_evaluate(t->left, lb,ub);
		V r_val = range_evaluate(t->right, lb,ub);
		return evaluate_vv(l_val, r_val);
	}

public:
	Dynamic_SegmentTree(long long size, V default_value, U default_lazy) :
		default_v(default_value), default_l(default_lazy)
	{
		long long sz = 1;
		while(sz < size) sz <<= 1LL;

		root = new node(0,sz, default_value, default_lazy);
		pool.push_back(root);
	}

	~Dynamic_SegmentTree(){
		for(int i=0; i<pool.size(); i++){
			delete(pool[i]);
		}
	}

	// A[pos] <- set_val
	void update(long long pos, V set_val){
		node* t = root;
		stack<node*> s;
		s.push(t);
		while(t->ub - t->lb != 1){
			set_children(t);
			propagete(t);

			if ( pos < (t->ub + t->lb) / 2 ){
				t = t->left;
			}else{
				t = t->right;
			}
			s.push(t);
		}

		propagete(t);
		s.top() -> val = set_val;
		s.pop();

		while(s.size()){
			t = s.top();
			s.pop();
			t->val = evaluate_vv(evaluate_vl(t->left), evaluate_vl(t->right));
		}
	}

	void range_update(long long lb, long long ub, U new_val){
		range_update(root, lb, ub, new_val);
	}

	V range_evaluate(long long lb, long long ub){
		return range_evaluate(root, lb, ub);
	}

	void dbg(){
		for(auto t: pool){
			cerr << "[ " << t->lb << " - " << t->ub  << " ) : " << t->val << " " << t->lazy << endl;
		}
	}
};

#define MOD 1000000000000000009

int main(){
	long long n,q;
	scanf("%lld%lld", &n,&q);
	Dynamic_SegmentTree<long long, unko> a(n+1, 0, unko());
	Dynamic_SegmentTree<long long, unko> b(n+1, 0, unko());
	Dynamic_SegmentTree<long long, unko> c(n+1, 0, unko());
	Dynamic_SegmentTree<long long, unko> d(n+1, 0, unko());
	Dynamic_SegmentTree<long long, unko> e(n+1, 0, unko());

	vector<unsigned long long> ans(5,0);
	vector<Dynamic_SegmentTree<long long, unko>*> seg(5);
	seg[0] = &a;
	seg[1] = &b;
	seg[2] = &c;
	seg[3] = &d;
	seg[4] = &e;

	for(int i=0; i<q; i++){
		long long x,l,r;
		scanf("%lld%lld%lld", &x,&l,&r);

		if(x==0){
			vector<unsigned long long> cnt(5,0);
			unsigned long long my_max = 0;
			for(int j=0; j<5; j++){
				cnt[j] = seg[j]->range_evaluate(l,r+1);
				my_max = max(my_max, cnt[j]);
			}
			vector<int> max_ele;
			for(int j=0; j<5; j++){
				if(cnt[j] == my_max) max_ele.push_back(j);
			}

			if(max_ele.size() != 1) continue;
			ans[max_ele[0]] += my_max;

			if(ans[max_ele[0]] >= MOD) ans[max_ele[0]] -= MOD;

		}else{
			for(int j=0; j<5; j++){
				seg[j]->range_update(l,r+1, x-1==j?(unko){1,false}:(unko){0,true});
			}
		}
	}

	for(int i=0; i<5; i++){
		unsigned long long cnt = seg[i]->range_evaluate(0,n);
		ans[i] += cnt;
		if(ans[i] >= MOD) ans[i] -= MOD;
		cout << ans[i] << (i==4?"\n":" ");
	}

	return 0;
}
0