Cod sursa(job #500932)

Utilizator cont_de_testeCont Teste cont_de_teste Data 13 noiembrie 2010 19:22:46
Problema Zota & Chidil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.33 kb
# include <algorithm>
# include <cstdio>
# include <vector>
using namespace std ;

# define x 	first
# define y 	second
# define pb push_back
# define mp make_pair
# define sr(V, val) V.begin (), V.end (), val
# define all(v) 	v.begin(), v.end()

typedef pair < int, int > PR ;
const int di[] = { -2, -1, -1, -1,  0,  0, 0, 0, 0,  1, 1, 1, 2 } ;
const int dj[] = {  0, -1,  0,  1, -2, -1, 0, 1, 2, -1, 0, 1, 0 } ;
const int MAX = 100005 ;

PR A[MAX], now, next;
vector < PR > X, Y;
int N, M, Nxy, Sol;

void baga_marfa ( int x, int y ) {
    for ( int i = 0; i <= 12; ++i ) {
        if ( x + di[i] || y + dj[i] ) {
            X.push_back ( make_pair ( x + di[i], y + dj[i] ) ) ;
            Y.push_back ( make_pair ( y + dj[i], x + di[i] ) ) ;
        }
    }
}

void do_it ( char c, int val ) {
    next = now;
    vector < PR > :: iterator it1, it2;
    PR tmp;

    if (c == 'N') {
        next.y += val;
        it1 = upper_bound(sr( X, now ));
        it2 = upper_bound(all(X), next);
        Sol += abs(it2 - it1);
    } else if (c == 'S') {
        next.y -= val;
        tmp = now;
        it1 = lower_bound(all(X), next);
        it2 = lower_bound(all(X), tmp);
        Sol += abs(it2 - it1);
    } else if (c == 'V') {
        next.x -= val;
        tmp = make_pair ( next.y, next.x ) ;
        it1 = lower_bound(all(Y), tmp);
        tmp = make_pair ( now.y, now.x ) ;
        it2 = lower_bound(all(Y), tmp);
        Sol += abs(it2 - it1);
    } else {
        next.x += val;
        tmp = make_pair ( now.y, now.x ) ;
        it1 = upper_bound(all(Y), tmp);
        tmp = make_pair ( next.y, next.x ) ;
        it2 = upper_bound(all(Y), tmp);
        Sol += abs(it2 - it1);
    }
    now = next;
}
int main () {
    int i, val;
    vector < PR > :: iterator it;
    char c;

    freopen("zc.in", "r", stdin);
    freopen("zc.out", "w", stdout);

    scanf("%d%d", &N, &M);

    for (i = 1; i <= N; ++ i) {
        scanf("%d%d", &A[i].x, &A[i].y);
        baga_marfa ( A[i].x, A[i].y ) ;
    }

    sort (X.begin(), X.end());
    X.resize(unique(X.begin(), X.end()) - X.begin());

    sort (Y.begin(), Y.end());
    Y.resize(unique(Y.begin(), Y.end()) - Y.begin());

    for (; M -- ;) {
        scanf(" %c %d", &c, &val);
        do_it (c, val);
    }

    printf("%d\n", Sol);
}