답안 #988898

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
988898 2024-05-26T15:34:51 Z holagola 원숭이와 사과 나무 (IZhO12_apple) C++17
0 / 100
1 ms 348 KB
#include <bits/stdc++.h>
using namespace std;
#define print(arr) for(auto& x:arr)cout<<x<<" ";cout<<"\n"
#define watch(x) cout<<#x<<"="<<x<<"\n"
#define all(x) x.begin(), x.end()
#define sz(x) ((int) x.size())
#define PB push_back
#define S second
#define F first
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

#include <bits/stdc++.h>
using namespace std;
#define print(arr) for(auto& x:arr)cout<<x<<" ";cout<<"\n"
#define watch(x) cout<<#x<<"="<<x<<"\n"
#define all(x) x.begin(), x.end()
#define sz(x) ((int) x.size())
#define PB push_back
#define S second
#define F first
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

struct node {
	int lz, lzL, lzR, val;
	node *L, *R;
	node(int lz = 0) : lz(lz), lzL(0), lzR(0), val(0), L(0), R(0) {}
	void propagate(int l, int r) {
		if(lz) {
			if(l != r) {
				if(L) L->lz ^= 1;
				else lzL ^= 1;
				if(R) R->lz ^= 1;
				else lzR ^= 1;
			}
			val = (r-l+1);
			lz = 0;
		}
	}
};

int null = 0;
int get_val(node* root, int lz, int a, int b) {
  if(!root) return lz*(b-a+1);
  return root->val;
}

struct SegTree { 
	node* root;
	int n; 
	SegTree(int n) : n(n), root(new node()) {}

	void upd(node* &root, int a, int b, int l, int r, int x, int lz = 0) {
		if((l > b || r < a) && !root) return; // break condition, this line optimizes memory
		if(root == nullptr) root = new node(lz);
		root->propagate(l, r);
		if(l > b || r < a) return;
		if(a <= l && r <= b) {
			root->lz = x;
			root->propagate(l, r);
		} else {
			int m = (l+r) >> 1;
			upd(root->L, a, b, l, m, x, root->lzL);
			upd(root->R, a, b, m+1, r, x, root->lzR);
			root->val = get_val(root->L, root->lzL, l, m) + get_val(root->R, root->lzR, m+1, r);
		}
	}

	int get(node* root, int a, int b, int l, int r, int lz = 0) {
		if(l > b || r < a) return null;
		if(root) root->propagate(l, r);
		if((a <= l && r <= b) || !root) { 
			return get_val(root, lz, l, r);
		} else {
			int m = (l+r) >> 1;
			return get(root->L, a, b, l, m, root->lzL) + get(root->R, a, b, m+1, r, root->lzR);
		}
	}

	int get(int a, int b) { return get(root, a, b, 0, n-1); }
	void upd(int a, int b, int x) { upd(root, a, b, 0, n-1, x); }
};


int main(){
	ios::sync_with_stdio(false);cin.tie(nullptr);
	SegTree st(1e9);
	ll q;cin>>q;
	ll op,l,r,c=0;
	while(q--){
		cin>>op>>l>>r;
		l--;r--;
		if(op==1){
			c=st.get(l+c,r+c);
			cout<<c<<"\n";
		}else{
			st.upd(l+c,r+c,1);
		}
	}
	return 0;
}

Compilation message

apple.cpp: In constructor 'SegTree::SegTree(int)':
apple.cpp:57:6: warning: 'SegTree::n' will be initialized after [-Wreorder]
   57 |  int n;
      |      ^
apple.cpp:56:8: warning:   'node* SegTree::root' [-Wreorder]
   56 |  node* root;
      |        ^~~~
apple.cpp:58:2: warning:   when initialized here [-Wreorder]
   58 |  SegTree(int n) : n(n), root(new node()) {}
      |  ^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -