F

Old MacDonald has a farm and a large potato field, (1010 + 1) × (1010 + 1) square meters in size. The field is divided into square garden beds, each bed takes up one square meter.
Old McDonald knows that the Colorado potato beetle is about to invade his farm and can destroy the entire harvest. To fight the insects, Old McDonald wants to spray some beds with insecticides.
So Old McDonald went to the field, stood at the center of the central field bed and sprayed this bed with insecticides. Now he’s going to make a series of movements and spray a few more beds. During each movement Old McDonald moves left, right, up or down the field some integer number of meters. As Old McDonald moves, he sprays all the beds he steps on. In other words, the beds that have any intersection at all with Old McDonald’s trajectory, are sprayed with insecticides.
When Old McDonald finished spraying, he wrote out all his movements on a piece of paper. Now he wants to know how many beds won’t be infected after the invasion of the Colorado beetles.
It is known that the invasion of the Colorado beetles goes as follows. First some bed on the field border gets infected. Than any bed that hasn’t been infected, hasn’t been sprayed with insecticides and has a common side with an infected bed, gets infected as well. Help Old McDonald and determine the number of beds that won’t be infected by the Colorado potato beetle.

    Input
    The first line contains an integer n (1 ≤ n ≤ 1000) — the number of Old McDonald's movements.

Next n lines contain the description of Old McDonald’s movements. The i-th of these lines describes the i-th movement. Each movement is given in the format “di xi”, where di is the character that determines the direction of the movement (“L”, “R”, “U” or “D” for directions “left”, “right”, “up” and “down”, correspondingly), and xi (1 ≤ xi ≤ 106) is an integer that determines the number of meters in the movement.

    Output
    Print a single integer — the number of beds that won't be infected by the Colorado potato beetle.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

    Examples

Input

5
R 8
U 9
L 9
D 8
L 2

Output

101

Input

7
R 10
D 2
L 7
U 9
D 2
R 3
D 10

Output

52

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
int n,m,k1,k2;
ll ans;
int dx[4]= {-1,0,1,0};  //方向数组
int dy[4]= {0,-1,0,1};
ll x[2005];     //离散化数组,要用long long,不然就wa,别问我怎么知道的
ll y[2005];
int vis[2005][2005];    //标记数组
struct node         //记录农夫洒过农药的地方
{
    ll x1,x2,y1,y2;
} t[2005];
struct dog
{
    int x,y;
};
int vs(char c)
{
    if(c=='U')
    {
        return 0;
    }
    if(c=='L')
    {
        return 1;
    }
    if(c=='D')
    {
        return 2;
    }
    return 3;
}
int bin(ll key,int g,ll d[])    //离散化中二分查找下标
{
    int l=1,r=g;
    int mid=(l+r)>>1;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(d[mid]==key)
        {
            return mid;
        }
        else if(d[mid]<key)
        {
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    return -1;
}
void bfs()      //害虫侵蚀过程简单bfs
{
    queue<dog> que;
    dog st;
    st.x=0;
    st.y=0;
    vis[0][0]=2;
    que.push(st);
    while(!que.empty())
    {
        dog k=que.front();
        que.pop();
        for(int i=0; i<4; i++)
        {
            int xx=k.x+dx[i];
            int yy=k.y+dy[i];
            dog kk;
            if(xx>=0&&xx<=k1&&yy>=0&&yy<=k2&&vis[xx][yy]==0)
            {
                kk.x=xx;
                kk.y=yy;
                vis[xx][yy]=2;
                que.push(kk);
            }
        }
    }
}
int main()
{
    int x1,y1,x2,y2;
    while(scanf("%d",&n)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        x1=0;
        y1=0;
        int g=0,p,kk=0;
        char c;
        for(int i=0; i<n; i++)
        {
            scanf(" %c %d",&c,&p);
            x2=x1+dx[vs(c)]*p;
            y2=y1+dy[vs(c)]*p;
            x[++g]=min(x1,x2);          //这里注意要判断大小,刚开始默认按顺序赋值x1,x2,样例都过不了
            y[g]=min(y1,y2);
            x[++g]=max(x1,x2)+1;        //这里的+1可以自己想一下为什么
            y[g]=max(y1,y2)+1;
            t[++kk].x1=min(x1,x2);
            t[kk].x2=max(x1,x2)+1;
            t[kk].y1=min(y1,y2);
            t[kk].y2=max(y1,y2)+1;
            x1=x2;
            y1=y2;
        }
        sort(x+1,x+1+g);        //对x,y数组排序去重
        sort(y+1,y+1+g);
        k1=1;
        for(int i=2; i<=g; i++)     //去重操作
        {
            if(x[i]!=x[i-1])
                x[++k1]=x[i];
        }
        k2=1;
        for(int i=2; i<=g; i++)
        {
            if(y[i]!=y[i-1])
                y[++k2]=y[i];
        }
        int p1,p2,q1,q2;
        for(int k=1; k<=kk; k++)    //将农夫走过的路打上标记
        {
            p1=bin(t[k].x1,k1,x);
            p2=bin(t[k].x2,k1,x)-1; //这里的减1也是要注意的地方
            q1=bin(t[k].y1,k2,y);
            q2=bin(t[k].y2,k2,y)-1;
            if(p2<p1)
            {
                p2=p1;
            }
            if(q2<q1)
            {
                q2=q1;
            }
            for(int i=p1; i<=p2; i++)
            {
                for(int j=q1; j<=q2; j++)
                {
                    vis[i][j]=1;
                }
            }
        }
        bfs();
        ans=0;
        for(int i=0; i<=k1; i++)    //暴力求出未被侵蚀的田地
        {
            for(int j=0; j<=k2; j++)
            {
                if(vis[i][j]!=2)
                {
                    ans=ans+(x[i+1]-x[i])*(y[j+1]-y[j]);
                }
            }
        }
        printf("%I64d\n",ans);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值