답안 #4851

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
4851 2014-01-05T08:39:58 Z cki86201 원숭이와 사과 나무 (IZhO12_apple) C++
100 / 100
60 ms 1208 KB
#include<stdio.h>
#include<algorithm>

struct segment{
	int x;
	segment *link[2];
	void update(int s,int d,int l,int r)
	{
		if(x == d-s+1)return;
		if(l<=s && d<=r){
			x = d-s+1;
			return;
		}
		int m = (s+d)>>1;
		if(l<=m){
			if(!link[0])link[0] = new segment();
			link[0]->update(s,m,l,r);
		}
		if(r>m){
			if(!link[1])link[1] = new segment();
			link[1]->update(m+1,d,l,r);
		}
		x = (link[0]?link[0]->x:0) + (link[1]?link[1]->x:0);
	}
	int read(int s,int d,int l,int r)
	{
		if(l<=s && d<=r)return x;
		if(x == d-s+1)return std::min(d,r) - std::max(s,l) + 1;
		int m = (s+d)>>1, ret = 0;
		if(l<=m)ret += link[0]?link[0]->read(s,m,l,r):0;
		if(r>m)ret += link[1]?link[1]->read(m+1,d,l,r):0;
		return ret;
	}
}root;

int main(){
	int n,c=0;
	scanf("%d",&n);
	while(n--){
		int d,x,y;
		scanf("%d%d%d",&d,&x,&y);
		if(d==1)printf("%d\n",c = root.read(1,1e9,x+c,y+c));
		else root.update(1,1e9,x+c,y+c);
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1208 KB Output is correct
2 Correct 0 ms 1208 KB Output is correct
3 Correct 0 ms 1208 KB Output is correct
4 Correct 4 ms 1208 KB Output is correct
5 Correct 4 ms 1208 KB Output is correct
6 Correct 4 ms 1208 KB Output is correct
7 Correct 0 ms 1208 KB Output is correct
8 Correct 28 ms 1208 KB Output is correct
9 Correct 52 ms 1208 KB Output is correct
10 Correct 52 ms 1208 KB Output is correct
11 Correct 48 ms 1208 KB Output is correct
12 Correct 40 ms 1208 KB Output is correct
13 Correct 60 ms 1208 KB Output is correct
14 Correct 52 ms 1208 KB Output is correct
15 Correct 52 ms 1208 KB Output is correct
16 Correct 56 ms 1208 KB Output is correct
17 Correct 40 ms 1208 KB Output is correct
18 Correct 52 ms 1208 KB Output is correct
19 Correct 40 ms 1208 KB Output is correct
20 Correct 52 ms 1208 KB Output is correct