답안 #37063

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
37063 2017-12-20T21:17:25 Z IvanC 원숭이와 사과 나무 (IZhO12_apple) C++14
100 / 100
63 ms 2016 KB
#include <bits/stdc++.h>
using namespace std;
typedef struct node* pnode;
struct node{
	pnode l,r;
	int val,ac;
	node() : l(NULL),r(NULL),val(0),ac(0){}
	void update(int left,int right,int i,int j){
		if(ac) return;
		if(left >= i && right <= j){
			ac = 1;
			val = right - left + 1;
			return;
		}
		else{
			int mid = (left+right)/2;
			if(j <= mid){
				if(l == NULL) l = new node;
				l->update(left,mid,i,min(j,mid));
			}
			else if(i >= mid + 1){
				if(r == NULL) r = new node;
				r->update(mid+1,right,max(mid+1,i),j);
			}
			else{
				if(l == NULL) l = new node;
				l->update(left,mid,i,min(j,mid));
				if(r == NULL) r = new node;
				r->update(mid+1,right,max(mid+1,i),j);
			}
			val = (l == NULL ? 0 : l->val) + (r == NULL ? 0 :  r->val);
		}
	}
	int query(int left,int right,int i,int j){
		if(ac){
			return j - i + 1;
		}
		if(left >= i && right <= j) return val;
		int mid = (left+right)/2;
		if(j <= mid){
			return l == NULL ? 0 : l->query(left,mid,i,min(j,mid));
		}
		else if(i >= mid + 1){
			return r == NULL ? 0 : r->query(mid+1,right,max(mid+1,i),j);
		}
		else{
			return (l == NULL ? 0 : l->query(left,mid,i,min(j,mid))) + (r == NULL ? 0 : r->query(mid+1,right,max(mid+1,i),j));
		}
	}
};
int main(){
	int lo = 1,hi = 1e9, last = 0,m;
	scanf("%d",&m);
	pnode raiz = new node;
	while(m--){
		int op,x,y;
		scanf("%d %d %d",&op,&x,&y);
		x += last;y+=last;
		if(op == 2){
			raiz->update(lo,hi,x,y);
		}
		else if(op == 1){
			last = raiz->query(lo,hi,x,y);
			printf("%d\n",last);
		}
	}
	return 0;
} 

Compilation message

apple.cpp: In function 'int main()':
apple.cpp:53:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&m);
                ^
apple.cpp:57:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d",&op,&x,&y);
                              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2016 KB Output is correct
2 Correct 0 ms 2016 KB Output is correct
3 Correct 0 ms 2016 KB Output is correct
4 Correct 3 ms 2016 KB Output is correct
5 Correct 3 ms 2016 KB Output is correct
6 Correct 3 ms 2016 KB Output is correct
7 Correct 3 ms 2016 KB Output is correct
8 Correct 29 ms 2016 KB Output is correct
9 Correct 49 ms 2016 KB Output is correct
10 Correct 53 ms 2016 KB Output is correct
11 Correct 49 ms 2016 KB Output is correct
12 Correct 59 ms 2016 KB Output is correct
13 Correct 59 ms 2016 KB Output is correct
14 Correct 63 ms 2016 KB Output is correct
15 Correct 46 ms 2016 KB Output is correct
16 Correct 59 ms 2016 KB Output is correct
17 Correct 49 ms 2016 KB Output is correct
18 Correct 49 ms 2016 KB Output is correct
19 Correct 49 ms 2016 KB Output is correct
20 Correct 53 ms 2016 KB Output is correct