Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem D
Wall Clocks

You are the manager of a chocolate sales team. Your team customarily takes tea breaks every two hours, during which varieties of new chocolate products of your company are served. Everyone looks forward to the tea breaks so much that they frequently give a glance at a wall clock.

Recently, your team has moved to a new office. You have just arranged desks in the office. One team member asked you to hang a clock on the wall in front of her desk so that she will not be late for tea breaks. Naturally, everyone seconded her.

You decided to provide an enough number of clocks to be hung in the field of view of everyone. Your team members will be satisfied if they have at least one clock (regardless of the orientation of the clock) in their view, or, more precisely, within 45 degrees left and 45 degrees right (both ends inclusive) from the facing directions of their seats. In order to buy as few clocks as possible, you should write a program that calculates the minimum number of clocks needed to
meet everyone's demand.

The office room is rectangular aligned to north-south and east-west directions. As the walls are tall enough, you can hang clocks even above the door and can assume one's eyesight is not blocked by other members or furniture. You can also assume that each clock is a point (of size zero), and so you can hang a clock even on a corner of the room.

For example, assume that there are two members. If they are sitting facing each other at positions shown in Figure D.1(A), you need to provide two clocks as they see distinct sections of the wall. If their seats are arranged as shown in Figure D.1(B), their fields of view have a common point on the wall. Thus, you can meet their demands by hanging a single clock at the point. In Figure D.1(C), their fields of view have a common wall section. You can meet their demands with a single clock by hanging it anywhere in the section. Arrangements (A), (B), and (C) in Figure D.1 correspond to Sample Input 1, 2, and 3, respectively.

Input

The input consists of a single test case, formatted as follows.

$n$ $w$ $d$

$x_1$ $y_1$ $f_1$

...

$x_n$ $y_n$ $f_n$

Figure D.1. Arrangements of seats and clocks. Gray area indicates field of view.

All numbers in the test case are integers. The first line contains the number of team members $n$ $(1 \leq n \leq 1,000)$ and the size of the office room $w$ and $d$ $(2 \leq w, d \leq 100,000)$. The office room has its width $w$ east-west, and depth $d$ north-south. Each of the following $n$ lines indicates the position and the orientation of the seat of a team member. Each member has a seat at a distinct position $(x_i, y_i)$ facing the direction $f_i$, for $i = 1, ..., n$. Here $1 \leq x_i \leq w - 1, 1 \leq y_i \leq d - 1$, and $f_i$ is one of N, E, W, and S, meaning north, east, west, and south, respectively. The position $(x, y)$ means $x$ distant from the west wall and $y$ distant from the south wall.

Output

Print the minimum number of clocks needed.

Sample Input 1

2 10 6
4 4 E
6 4 W

Sample Output 1

2

Sample Input 2

2 10 6
2 4 E
6 4 W

Sample Output 2

1

Sample Input 3

2 10 6
3 2 S
6 4 W

Sample Output 3

1

Sample Input 4

6 10 6
1 5 N
7 1 N
8 2 E
9 1 S
4 4 S
3 3 W

Sample Output 4

3

Sample Input 5

4 10 6
4 3 W
2 4 N
4 4 W
3 3 S

Sample Output 5

2

Sample Input 6

4 100000 40000
25000 25000 S
20000 30000 S
75000 25000 S
80000 30000 S

Sample Output 6

1

Solution in C++14 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define rdll(x) scanf("%lld",&x);
int n,w,h;
vector<P>vec;
vector<int>za;
bool in[400005];
bool cmp(P a,P b){
	return a.sc < b.sc;
}
vector<P>zwa;
int main(){
    cin>>n>>w>>h;
    	for(int i=0;i<w;i++){
    		//(i,0)
    		zwa.pb(mp(i,0));
    	}
    	for(int i=0;i<h;i++){
    		//(w,i)
    		zwa.pb(mp(w,i));
    	}
    	for(int i=w;i>0;i--){
    		//(i,h)
    		zwa.pb(mp(i,h));
    	}
    	for(int i=h;i>0;i--){
    		//(0,i)
    		zwa.pb(mp(0,i));
    	}
    rep(i,n){
    	int x,y; string st;
    	cin>>x>>y>>st;
    	int lb1,ub1;
    	if(st == "N" || st == "E"){
    		lb1 = x+y;
    		ub1 = w+h;
    	}
    	else{
    		lb1 = 0;
    		ub1 = x+y;
    	}
    	int lb2,ub2;
    	if(st == "S" || st == "E"){
    		lb2 = x-y;
    		ub2 = w;
    	}
    	else{
    		lb2 = -h;
    		ub2 = x-y;
    	}
    	//x+y; [lb1,ub1] x-y; [lb2,ub2]
    	vector<int>vecc;
    	for(int i=0;i<w;i++){
    		//(i,0)
    		if(i == x+y) vecc.pb(i);
    		if(i == x-y) vecc.pb(i);
    	}
    	for(int i=0;i<h;i++){
    		//(w,i)
    		if(w+i == x+y) vecc.pb(w+i);
    		if(w-i == x-y) vecc.pb(w+i);
    	}
    	for(int i=w;i>0;i--){
    		//(i,h)
    		if(i+h == x+y) vecc.pb(w+h+w-i);
    		if(i-h == x-y) vecc.pb(w+h+w-i);
    	}
    	for(int i=h;i>0;i--){
    		//(0,i)
    		if(i == x+y) vecc.pb(w+h+w+h-i);
    		if(-i == x-y) vecc.pb(w+h+w+h-i);
    	}
    	assert(vecc.size() == 4);
    	SORT(vecc);
    	int u = 0;
    	rep(i,4){
    		int L = vecc[i], R = vecc[(i+1)%4];
    		int w = (L+1)%zwa.size();
    		int X = zwa[w].fi, Y = zwa[w].sc;
    		if(lb1<=X+Y&&X+Y<=ub1&&lb2<=X-Y&&X-Y<=ub2){
	    		vec.pb(mp(L,R));
	    		za.pb(L); za.pb(R);
	    		u++;
	    	}
	}
	assert(u==1);
    }
    SORT(za); ERASE(za);
    rep(i,vec.size()){
    	vec[i].fi = lower_bound(za.begin(),za.end(),vec[i].fi)-za.begin();
    	vec[i].sc = lower_bound(za.begin(),za.end(),vec[i].sc)-za.begin();
    	if(vec[i].fi > vec[i].sc) vec[i].sc+=za.size();
    }
    int ans = INF;
    for(int i=0;i<za.size();i++){
        vector<P>nw;
        int cur = 0, ret = 1;
        rep(j,vec.size()){
            int a=vec[j].fi,b=vec[j].sc;
            {
                if(a<=i && i<=b);
                else if(a<=i+za.size() && i+za.size()<=b);
                else nw.pb(vec[j]);
            }
        }
        sort(nw.begin(),nw.end(),cmp);
        while(cur != nw.size()){
        	int las = nw[cur].sc; ret++;
        	while(cur != nw.size() && las >= nw[cur].fi) cur++;
    	}
    	ans = min(ans,ret);
    }
    cout<<ans<<endl;
}