Submission #50973

# Submission time Handle Problem Language Result Execution time Memory
50973 2018-06-15T10:19:29 Z model_code None (JOI16_ho_t4) C++17
100 / 100
125 ms 7052 KB
#include <cstdio>
#include <cstdlib>

int N,K,dx,dy;
char moves[100008];
typedef struct {
	long long vx;
	long long vy;
	long long minv;
	long long maxv;
}list_structure;
list_structure lists_temp[100008];
int lists_temp_len=0;
list_structure lists[100008];
int lists_len=1;
void list_add(list_structure *ls,long long sx,long long sy,long long mnv,long long mxv) {
	ls->vx=sx;
	ls->vy=sy;
	ls->minv=mnv;
	ls->maxv=mxv;
}
void add_point(long long sx,long long sy,long long mnv,long long mxv) {
	//mod dx で正しい場所を算出し,その場所に点を追加.
	if(dx==0) {
		list_add(&lists_temp[lists_temp_len],sx,sy,mnv,mxv);
		lists_temp_len++;
	} else {
		if(sx>=dx) {
			long long temp=sx/dx;
			add_point(sx-temp*dx,sy-temp*dy,mnv+temp,mxv+temp);
			return;
		}
		if(sx<0) {
			long long temp=-sx/dx+1;
			add_point(sx+temp*dx,sy+temp*dy,mnv-temp,mxv-temp);
			return;
		}
		list_add(&lists_temp[lists_temp_len],sx,sy,mnv,mxv);
		lists_temp_len++;
	}
}
int comp(const void *ka,const void *kb) {
	list_structure *a = (list_structure *)ka;
	list_structure *b = (list_structure *)kb;
	if(a->vx != b->vx) {
		if(a->vx < b->vx) return -1;
		return 1;
	}
	if(a->vy != b->vy) {
		if(a->vy < b->vy) return -1;
		return 1;
	}
	if(a->minv != b->minv) {
		if(a->minv < b->minv) return -1;
		return 1;
	}
	if(a->maxv < b->maxv) return -1;
	if(a->maxv > b->maxv) return 1;
	return 0;
}
int comp_xy(const void *ka,const void *kb) {
	list_structure *a = (list_structure *)ka;
	list_structure *b = (list_structure *)kb;
	if(a->vx != b->vx) {
		if(a->vx < b->vx) return -1;
		return 1;
	}
	if(a->vy != b->vy) {
		if(a->vy < b->vy) return -1;
		return 1;
	}
	return 0;
}
int findl(long long sx,long long sy) {
	//(sx,sy) に相当する区間がリストにあればその場所を、ないなら -1 を返す.
	//複数あるならそれらの一番左の場所を返す.
	int minr=0; int maxr=lists_len-1;
	list_structure temp; temp.vx=sx; temp.vy=sy;
	while(minr<maxr) {
		int hf=(minr+maxr)/2;
		if(comp_xy(&lists[hf],&temp)<0) {
			minr=hf+1;
		} else {
			maxr=hf;
		}
	}
	if(lists[minr].vx!=sx||lists[minr].vy!=sy) return -1;
	return minr;
}
int findr(long long sx,long long sy) {
	//(sx,sy) に相当する区間がリストにあればその場所を返す.
	//複数あるならそれらの一番右の場所を返す.
	int minr=0; int maxr=lists_len-1;
	list_structure temp; temp.vx=sx; temp.vy=sy;
	while(minr<maxr) {
		int hf=(minr+maxr+1)/2;
		if(comp_xy(&lists[hf],&temp)>0) {
			maxr=hf-1;
		} else {
			minr=hf;
		}
	}
	return minr;
}
int exist(long long vx,long long vy) {
	//(vx,vy) に相当する場所に 1 つ以上区間があるか判定.
	if(dx==0) {
		if(findl(vx,vy)<0) return 0;
		return 1;
	}
	if(vx<0) {
		long long temp=-vx/dx+1;
		return exist(vx+temp*dx,vy+temp*dy);
	}
	if(vx>=dx) {
		long long temp=vx/dx;
		return exist(vx-temp*dx,vy-temp*dy);
	}
	if(findl(vx,vy)<0) return 0;
	return 1;
}
void calcplace(long long vx,long long vy,int *retl,int *retr,long long *retoffset) {
	//(vx,vy) に相当する区間を表す集合 (retl から retr の範囲として返す) を求める.
	//retoffset には、共通部分を取るときに必要な移動を保存.
	*retoffset=0;
	if(dx) {
		if(vx<0) {
			*retoffset=-vx/dx+1;
			*retoffset=-(*retoffset);
		}
		if(vx>=dx) {
			*retoffset=vx/dx;
		}
	}
	*retl=findl(vx-(*retoffset)*dx,vy-(*retoffset)*dy);
	*retr=findr(vx-(*retoffset)*dx,vy-(*retoffset)*dy);
}
int range_minv_a[100008];
int range_maxv_a[100008];
int range_minv_b[100008];
int range_maxv_b[100008];
int range_minv_temp[100008];
int range_maxv_temp[100008];
int range_a_len,range_b_len;
void range_cpya(int fl,int fr,long long offset) {
	//range_*_a に区間列をコピー
	range_a_len=0;
	for(int i=fl;i<=fr;i++) {
		range_minv_a[range_a_len]=lists[i].minv-offset;
		range_maxv_a[range_a_len]=lists[i].maxv-offset;
		range_a_len++;
	}
}
void range_cpyb(int fl,int fr,long long offset) {
	//range_*_b に区間列をコピー
	range_b_len=0;
	for(int i=fl;i<=fr;i++) {
		range_minv_b[range_b_len]=lists[i].minv-offset;
		range_maxv_b[range_b_len]=lists[i].maxv-offset;
		range_b_len++;
	}
}
void merge(void) {
	//2 つの区間列 range_*_a と range_*_b の共通部分となる区間列を計算し、range_*_a に保存.
	int range_temp_len=0;
	int ia=0; int ib=0;
	while(ia<range_a_len&&ib<range_b_len) {
		if(range_minv_a[ia]<range_minv_b[ib]) {
			range_minv_a[ia]=range_minv_b[ib];
			if(range_minv_a[ia]>range_maxv_a[ia]) ia++;
		} else if(range_minv_a[ia]>range_minv_b[ib]) {
			range_minv_b[ib]=range_minv_a[ia];
			if(range_minv_b[ib]>range_maxv_b[ib]) ib++;
		} else {
			range_minv_temp[range_temp_len]=range_minv_a[ia];
			if(range_maxv_a[ia]>range_maxv_b[ib]) {
				range_maxv_temp[range_temp_len]=range_maxv_b[ib];
				ib++;
			} else {
				range_maxv_temp[range_temp_len]=range_maxv_a[ia];
				ia++;
			}
			range_temp_len++;
		}
	}
	for(int i=0;i<range_temp_len;i++) {
		range_minv_a[i]=range_minv_temp[i];
		range_maxv_a[i]=range_maxv_temp[i];
	}
	range_a_len=range_temp_len;
}
int main() {
	scanf("%d%d",&N,&K);
	scanf("%s",moves);
	for(int i=0;i<N;i++) {
		if(moves[i]=='E') dx++;
		if(moves[i]=='N') dy++;
		if(moves[i]=='W') dx--;
		if(moves[i]=='S') dy--;
	}
	if(dx<0) {
		dx=-dx;
		for(int i=0;i<N;i++) {
			if(moves[i]=='E') {
				moves[i]='W';
			} else if(moves[i]=='W') {
				moves[i]='E';
			}
		}
	}
	if(dy<0) {
		dy=-dy;
		for(int i=0;i<N;i++) {
			if(moves[i]=='N') {
				moves[i]='S';
			} else if(moves[i]=='S') {
				moves[i]='N';
			}
		}
	}
	//入力を dx>0 となるように回転,反転させる (dx=dy=0 のとき以外).
	if(dx==0&&dy>0) {
		int temp=dy;
		dy=dx;
		dx=temp;
		for(int i=0;i<N;i++) {
			if(moves[i]=='E') {
				moves[i]='N';
			} else if(moves[i]=='N') {
				moves[i]='E';
			}
			if(moves[i]=='S') {
				moves[i]='W';
			} else if(moves[i]=='W') {
				moves[i]='S';
			}
		}
	}
	//dx=dy=0 のとき,K=1 としておく.
	if(dx==0) K=1;
	add_point(0,0,0,K-1);
	int nx=0; int ny=0;
	for(int i=0;i<N;i++) {
		if(moves[i]=='E') nx++;
		if(moves[i]=='N') ny++;
		if(moves[i]=='W') nx--;
		if(moves[i]=='S') ny--;
		add_point(nx,ny,0,K-1);
	}
	//算出した各区間の重なったもの同士を結合.
	qsort(lists_temp,lists_temp_len,sizeof(list_structure),comp);
	list_add(&lists[0],lists_temp[0].vx,lists_temp[0].vy,lists_temp[0].minv,lists_temp[0].maxv);
	for(int i=1;i<lists_temp_len;i++) {
		if(lists[lists_len-1].vx!=lists_temp[i].vx||lists[lists_len-1].vy!=lists_temp[i].vy) {
			list_add(&lists[lists_len],lists_temp[i].vx,lists_temp[i].vy,lists_temp[i].minv,lists_temp[i].maxv);
			lists_len++;
		} else {
			if(lists[lists_len-1].maxv+1<lists_temp[i].minv) {
				list_add(&lists[lists_len],lists_temp[i].vx,lists_temp[i].vy,lists_temp[i].minv,lists_temp[i].maxv);
				lists_len++;
			} else {
				if(lists[lists_len-1].maxv<lists_temp[i].maxv) lists[lists_len-1].maxv=lists_temp[i].maxv;
			}
		}
	}
	long long sol=0;
	//正方形の左下を固定した場合の個数を計算.
	for(int i=0;i<lists_len;i++) {
		if(i) if(lists[i].vx==lists[i-1].vx&&lists[i].vy==lists[i-1].vy) continue; //既に調べているなら除外.
		int bx=lists[i].vx;
		int by=lists[i].vy;
		if(exist(bx+1,by)+exist(bx,by+1)+exist(bx+1,by+1)==3) {
			int fl,fr;
			long long offset;
			calcplace(bx,by,&fl,&fr,&offset);
			range_cpya(fl,fr,offset);
			calcplace(bx+1,by,&fl,&fr,&offset);
			range_cpyb(fl,fr,offset);
			merge();
			calcplace(bx,by+1,&fl,&fr,&offset);
			range_cpyb(fl,fr,offset);
			merge();
			calcplace(bx+1,by+1,&fl,&fr,&offset);
			range_cpyb(fl,fr,offset);
			merge();
			for(int i=0;i<range_a_len;i++) sol+=range_maxv_a[i]-range_minv_a[i]+1;
		}
	}
	printf("%lld\n",sol);
	return 0;
}

Compilation message

2016_ho_t4.cpp: In function 'int main()':
2016_ho_t4.cpp:193:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&N,&K);
  ~~~~~^~~~~~~~~~~~~~
2016_ho_t4.cpp:194:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s",moves);
  ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 488 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 624 KB Output is correct
6 Correct 3 ms 624 KB Output is correct
7 Correct 2 ms 648 KB Output is correct
8 Correct 2 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 2 ms 648 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 648 KB Output is correct
14 Correct 2 ms 648 KB Output is correct
15 Correct 2 ms 748 KB Output is correct
16 Correct 2 ms 748 KB Output is correct
17 Correct 2 ms 748 KB Output is correct
18 Correct 2 ms 748 KB Output is correct
19 Correct 2 ms 748 KB Output is correct
20 Correct 2 ms 748 KB Output is correct
21 Correct 2 ms 748 KB Output is correct
22 Correct 2 ms 748 KB Output is correct
23 Correct 2 ms 748 KB Output is correct
24 Correct 2 ms 748 KB Output is correct
25 Correct 2 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 488 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 624 KB Output is correct
6 Correct 3 ms 624 KB Output is correct
7 Correct 2 ms 648 KB Output is correct
8 Correct 2 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 2 ms 648 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 648 KB Output is correct
14 Correct 2 ms 648 KB Output is correct
15 Correct 2 ms 748 KB Output is correct
16 Correct 2 ms 748 KB Output is correct
17 Correct 2 ms 748 KB Output is correct
18 Correct 2 ms 748 KB Output is correct
19 Correct 2 ms 748 KB Output is correct
20 Correct 2 ms 748 KB Output is correct
21 Correct 2 ms 748 KB Output is correct
22 Correct 2 ms 748 KB Output is correct
23 Correct 2 ms 748 KB Output is correct
24 Correct 2 ms 748 KB Output is correct
25 Correct 2 ms 748 KB Output is correct
26 Correct 2 ms 748 KB Output is correct
27 Correct 4 ms 892 KB Output is correct
28 Correct 8 ms 1532 KB Output is correct
29 Correct 26 ms 3708 KB Output is correct
30 Correct 34 ms 4988 KB Output is correct
31 Correct 57 ms 6908 KB Output is correct
32 Correct 50 ms 6908 KB Output is correct
33 Correct 53 ms 6944 KB Output is correct
34 Correct 49 ms 6944 KB Output is correct
35 Correct 51 ms 6944 KB Output is correct
36 Correct 46 ms 6944 KB Output is correct
37 Correct 49 ms 6944 KB Output is correct
38 Correct 53 ms 6944 KB Output is correct
39 Correct 50 ms 6944 KB Output is correct
40 Correct 48 ms 6944 KB Output is correct
41 Correct 41 ms 6944 KB Output is correct
42 Correct 43 ms 6944 KB Output is correct
43 Correct 50 ms 6944 KB Output is correct
44 Correct 48 ms 6944 KB Output is correct
45 Correct 43 ms 6944 KB Output is correct
46 Correct 49 ms 7036 KB Output is correct
47 Correct 78 ms 7036 KB Output is correct
48 Correct 25 ms 7036 KB Output is correct
49 Correct 45 ms 7036 KB Output is correct
50 Correct 44 ms 7036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 488 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 624 KB Output is correct
6 Correct 3 ms 624 KB Output is correct
7 Correct 2 ms 648 KB Output is correct
8 Correct 2 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 2 ms 648 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 648 KB Output is correct
14 Correct 2 ms 648 KB Output is correct
15 Correct 2 ms 748 KB Output is correct
16 Correct 2 ms 748 KB Output is correct
17 Correct 2 ms 748 KB Output is correct
18 Correct 2 ms 748 KB Output is correct
19 Correct 2 ms 748 KB Output is correct
20 Correct 2 ms 748 KB Output is correct
21 Correct 2 ms 748 KB Output is correct
22 Correct 2 ms 748 KB Output is correct
23 Correct 2 ms 748 KB Output is correct
24 Correct 2 ms 748 KB Output is correct
25 Correct 2 ms 748 KB Output is correct
26 Correct 2 ms 7036 KB Output is correct
27 Correct 2 ms 7036 KB Output is correct
28 Correct 2 ms 7036 KB Output is correct
29 Correct 2 ms 7036 KB Output is correct
30 Correct 2 ms 7036 KB Output is correct
31 Correct 2 ms 7036 KB Output is correct
32 Correct 2 ms 7036 KB Output is correct
33 Correct 2 ms 7036 KB Output is correct
34 Correct 2 ms 7036 KB Output is correct
35 Correct 2 ms 7036 KB Output is correct
36 Correct 2 ms 7036 KB Output is correct
37 Correct 2 ms 7036 KB Output is correct
38 Correct 2 ms 7036 KB Output is correct
39 Correct 2 ms 7036 KB Output is correct
40 Correct 2 ms 7036 KB Output is correct
41 Correct 2 ms 7036 KB Output is correct
42 Correct 2 ms 7036 KB Output is correct
43 Correct 2 ms 7036 KB Output is correct
44 Correct 2 ms 7036 KB Output is correct
45 Correct 2 ms 7036 KB Output is correct
46 Correct 2 ms 7036 KB Output is correct
47 Correct 2 ms 7036 KB Output is correct
48 Correct 2 ms 7036 KB Output is correct
49 Correct 2 ms 7036 KB Output is correct
50 Correct 2 ms 7036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 488 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 624 KB Output is correct
6 Correct 3 ms 624 KB Output is correct
7 Correct 2 ms 648 KB Output is correct
8 Correct 2 ms 648 KB Output is correct
9 Correct 2 ms 648 KB Output is correct
10 Correct 2 ms 648 KB Output is correct
11 Correct 2 ms 648 KB Output is correct
12 Correct 2 ms 648 KB Output is correct
13 Correct 2 ms 648 KB Output is correct
14 Correct 2 ms 648 KB Output is correct
15 Correct 2 ms 748 KB Output is correct
16 Correct 2 ms 748 KB Output is correct
17 Correct 2 ms 748 KB Output is correct
18 Correct 2 ms 748 KB Output is correct
19 Correct 2 ms 748 KB Output is correct
20 Correct 2 ms 748 KB Output is correct
21 Correct 2 ms 748 KB Output is correct
22 Correct 2 ms 748 KB Output is correct
23 Correct 2 ms 748 KB Output is correct
24 Correct 2 ms 748 KB Output is correct
25 Correct 2 ms 748 KB Output is correct
26 Correct 2 ms 748 KB Output is correct
27 Correct 4 ms 892 KB Output is correct
28 Correct 8 ms 1532 KB Output is correct
29 Correct 26 ms 3708 KB Output is correct
30 Correct 34 ms 4988 KB Output is correct
31 Correct 57 ms 6908 KB Output is correct
32 Correct 50 ms 6908 KB Output is correct
33 Correct 53 ms 6944 KB Output is correct
34 Correct 49 ms 6944 KB Output is correct
35 Correct 51 ms 6944 KB Output is correct
36 Correct 46 ms 6944 KB Output is correct
37 Correct 49 ms 6944 KB Output is correct
38 Correct 53 ms 6944 KB Output is correct
39 Correct 50 ms 6944 KB Output is correct
40 Correct 48 ms 6944 KB Output is correct
41 Correct 41 ms 6944 KB Output is correct
42 Correct 43 ms 6944 KB Output is correct
43 Correct 50 ms 6944 KB Output is correct
44 Correct 48 ms 6944 KB Output is correct
45 Correct 43 ms 6944 KB Output is correct
46 Correct 49 ms 7036 KB Output is correct
47 Correct 78 ms 7036 KB Output is correct
48 Correct 25 ms 7036 KB Output is correct
49 Correct 45 ms 7036 KB Output is correct
50 Correct 44 ms 7036 KB Output is correct
51 Correct 2 ms 7036 KB Output is correct
52 Correct 2 ms 7036 KB Output is correct
53 Correct 2 ms 7036 KB Output is correct
54 Correct 2 ms 7036 KB Output is correct
55 Correct 2 ms 7036 KB Output is correct
56 Correct 2 ms 7036 KB Output is correct
57 Correct 2 ms 7036 KB Output is correct
58 Correct 2 ms 7036 KB Output is correct
59 Correct 2 ms 7036 KB Output is correct
60 Correct 2 ms 7036 KB Output is correct
61 Correct 2 ms 7036 KB Output is correct
62 Correct 2 ms 7036 KB Output is correct
63 Correct 2 ms 7036 KB Output is correct
64 Correct 2 ms 7036 KB Output is correct
65 Correct 2 ms 7036 KB Output is correct
66 Correct 2 ms 7036 KB Output is correct
67 Correct 2 ms 7036 KB Output is correct
68 Correct 2 ms 7036 KB Output is correct
69 Correct 2 ms 7036 KB Output is correct
70 Correct 2 ms 7036 KB Output is correct
71 Correct 2 ms 7036 KB Output is correct
72 Correct 2 ms 7036 KB Output is correct
73 Correct 2 ms 7036 KB Output is correct
74 Correct 2 ms 7036 KB Output is correct
75 Correct 2 ms 7036 KB Output is correct
76 Correct 5 ms 7036 KB Output is correct
77 Correct 5 ms 7036 KB Output is correct
78 Correct 15 ms 7036 KB Output is correct
79 Correct 27 ms 7036 KB Output is correct
80 Correct 36 ms 7036 KB Output is correct
81 Correct 49 ms 7036 KB Output is correct
82 Correct 54 ms 7036 KB Output is correct
83 Correct 39 ms 7036 KB Output is correct
84 Correct 50 ms 7036 KB Output is correct
85 Correct 57 ms 7036 KB Output is correct
86 Correct 49 ms 7036 KB Output is correct
87 Correct 52 ms 7036 KB Output is correct
88 Correct 49 ms 7036 KB Output is correct
89 Correct 52 ms 7036 KB Output is correct
90 Correct 50 ms 7036 KB Output is correct
91 Correct 54 ms 7036 KB Output is correct
92 Correct 53 ms 7036 KB Output is correct
93 Correct 56 ms 7036 KB Output is correct
94 Correct 47 ms 7036 KB Output is correct
95 Correct 46 ms 7036 KB Output is correct
96 Correct 48 ms 7036 KB Output is correct
97 Correct 72 ms 7036 KB Output is correct
98 Correct 31 ms 7052 KB Output is correct
99 Correct 44 ms 7052 KB Output is correct
100 Correct 125 ms 7052 KB Output is correct