답안 #409297

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
409297 2021-05-20T14:09:46 Z codebuster_10 원숭이와 사과 나무 (IZhO12_apple) C++17
100 / 100
458 ms 188612 KB
#include <bits/stdc++.h>

using namespace std ;

//#define int int64_t //be careful about this 
#define endl "\n"
#define f(i,a,b) for(int i=int(a);i<int(b);++i)

#define pr pair
#define ar array
#define fr first
#define sc second
#define vt vector
#define pb push_back
#define eb emplace_back
#define LB lower_bound  
#define UB upper_bound
#define PQ priority_queue

#define sz(x) ((int)(x).size())
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define mem0(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))

template<class A> void rd(vt<A>& v);
template<class T> void rd(T& x){ cin >> x; }
template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;}
template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;}

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

template<typename T>
void __p(T a) {
  cout<<a; 
}
template<typename T, typename F>
void __p(pair<T, F> a) {
  cout<<"{";
  __p(a.first);
  cout<<",";
  __p(a.second);
  cout<<"}\n"; 
}
template<typename T>
void __p(std::vector<T> a) {
  cout<<"{";
  for(auto it=a.begin(); it<a.end(); it++)
    __p(*it),cout<<",}\n"[it+1==a.end()]; 
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
  __p(a1);
  __p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cout<<name<<" : ";
  __p(arg1);
  cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
  int bracket=0,i=0;
  for(;; i++)
    if(names[i]==','&&bracket==0)
      break;
    else if(names[i]=='(')
      bracket++;
    else if(names[i]==')')
      bracket--;
  const char *comma=names+i;
  cout.write(names,comma-names)<<" : ";
  __p(arg1);
  cout<<" | ";
  __f(comma+1,args...);
}

void setIO(string s = "") {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
  cin.exceptions(cin.failbit); 
	cout.precision(15);	cout << fixed;
  #ifdef ONLINE_JUDGE
  if(sz(s)){
  	freopen((s+".in").c_str(),"r",stdin);
  	freopen((s+".out").c_str(),"w",stdout);
  }
  #define __f(...) 0
  #endif
}

/*
	Description :- Sparse segment tree
	Source :- USACO 
	Verification :- 

*/
struct Node {
	int sum, lazy, tl, tr, l, r;// node cover [tl,tr], also lazy is not added to current node;
	Node() : sum(0), lazy(0), l(-1), r(-1) {}
};

const int MAXN = 123456;
Node st[64 * MAXN];
int cnt = 2;


void propogate(int x) {
	if (st[x].lazy) {
		st[x].sum = st[x].tr - st[x].tl + 1;
		int mid = (st[x].tl + st[x].tr) / 2;
		if (st[x].l == -1) {
			st[x].l = cnt++;
			st[st[x].l].tl = st[x].tl;
			st[st[x].l].tr = mid;
		}
		if (st[x].r == -1) {
			st[x].r = cnt++;
			st[st[x].r].tl = mid + 1;
			st[st[x].r].tr = st[x].tr;
		}
		st[st[x].l].lazy = st[st[x].r].lazy = 1;
		st[x].lazy = 0;
	}
}

void update(int x, int l, int r) {
	propogate(x);
	if (l == st[x].tl && r == st[x].tr) {
		st[x].lazy = 1;
		propogate(x);
	} else {
		int mid = (st[x].tl + st[x].tr) / 2;
		if (st[x].l == -1) {
			st[x].l = cnt++;
			st[st[x].l].tl = st[x].tl;
			st[st[x].l].tr = mid;
		}
		if (st[x].r == -1) {
			st[x].r = cnt++;
			st[st[x].r].tl = mid + 1;
			st[st[x].r].tr = st[x].tr;
		}

		if (l > mid) update(st[x].r, l, r);
		else if (r <= mid) update(st[x].l, l, r);
		else {
			update(st[x].l, l, mid);
			update(st[x].r, mid + 1, r);
		}

		propogate(st[x].l);
		propogate(st[x].r);
		st[x].sum = st[st[x].l].sum + st[st[x].r].sum;
	}
}

int query(int x, int l, int r) {
	propogate(x);
	if (l == st[x].tl && r == st[x].tr) return st[x].sum;
	else {
		int mid = (st[x].tl + st[x].tr) / 2;
		if (st[x].l == -1) {
			st[x].l = cnt++;
			st[st[x].l].tl = st[x].tl;
			st[st[x].l].tr = mid;
		}
		if (st[x].r == -1) {
			st[x].r = cnt++;
			st[st[x].r].tl = mid + 1;
			st[st[x].r].tr = st[x].tr;
		}

		if (l > mid) return query(st[x].r, l, r);
		else if (r <= mid) return query(st[x].l, l, r);
		else return query(st[x].l, l, mid) + query(st[x].r, mid + 1, r);
	}
}

signed main(){
  setIO() ;
  int q; rd(q);
  st[1].sum = 0; st[1].lazy = 0;
  st[1].tl = 1, st[1].tr = int(2e9);
  int c = 0;
  while(q--){
  	int t,l,r; rd(t,l,r);
  	if(t == 1){
  		c = query(1, l + c, r + c);
  		cout << c << endl;
  	}else{
  		update(1,l + c, r + c);
  	}
  }
}

# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 185756 KB Output is correct
2 Correct 87 ms 185772 KB Output is correct
3 Correct 85 ms 185764 KB Output is correct
4 Correct 99 ms 185912 KB Output is correct
5 Correct 104 ms 186028 KB Output is correct
6 Correct 102 ms 186036 KB Output is correct
7 Correct 104 ms 185956 KB Output is correct
8 Correct 214 ms 186928 KB Output is correct
9 Correct 359 ms 187944 KB Output is correct
10 Correct 366 ms 188032 KB Output is correct
11 Correct 368 ms 187996 KB Output is correct
12 Correct 371 ms 188080 KB Output is correct
13 Correct 336 ms 188300 KB Output is correct
14 Correct 343 ms 188348 KB Output is correct
15 Correct 447 ms 188528 KB Output is correct
16 Correct 458 ms 188612 KB Output is correct
17 Correct 335 ms 188412 KB Output is correct
18 Correct 338 ms 188532 KB Output is correct
19 Correct 451 ms 188444 KB Output is correct
20 Correct 451 ms 188500 KB Output is correct