zoukankan      html  css  js  c++  java
  • Codeforces Round #310 (Div. 1) C. Case of Chocolate set

    C. Case of Chocolate

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/555/problem/C

    Description

    Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.

    A bar of chocolate can be presented as an n × n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.

    After each action, he wants to know how many pieces he ate as a result of this action.

    Input

    The first line contains integers n (1 ≤ n ≤ 109) and q (1 ≤ q ≤ 2·105) — the size of the chocolate bar and the number of actions.

    Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≤ xi, yi ≤ n, xi + yi = n + 1) — the numbers of the column and row of the chosen cell and the character that represents the direction (L — left, U — up).

    Output

    Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.

    Sample Input

    6 5
    3 4 U
    6 1 L
    2 5 L
    1 6 U
    4 3 U

    Sample Output

    4
    3
    2
    1
    2

    HINT

    Pictures to the sample tests:

    The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.

    In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.

    题意

    见hint,图很清楚,注意x+y==n+1

    题解:

    比B题简单呀……直接set搞一搞就好了

    U的就找L的

    L就找U的……

    然后搞一搞就好了~

    代码

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef unsigned long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 2000001
    #define mod 1000000007
    #define eps 1e-9
    int Num;
    char CH[20];
    const int inf=0x3f3f3f3f;
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    
    //**************************************************************************************
    
    set<pair<int,int> > s;
    map<int,int>m;
    
    int main()
    {
        int n=read(),q=read();
        s.insert(make_pair(0,1));
        s.insert(make_pair(n+1,0));
        for(int i=0;i<q;i++)
        {
            int x=read(),y=read();
            char c;scanf("%c",&c);
            if(m.count(x))
            {
                puts("0");
                continue;
            }
            if(c=='U')
            {
                pair<int,int> p=*s.lower_bound(pair<int,int>(x,-1));
                if(p.second==1)
                    m[x]=p.first-x+m[p.first];
                else
                    m[x]=p.first-x;
                s.insert(pair<int,int>(x,1));
            }
            else
            {
                set<pair<int,int> >::iterator it=--s.lower_bound(pair<int,int>(x,-1));
                if(it->second==0)
                    m[x]=x-it->first+m[it->first];
                else
                    m[x]=x-it->first;
                s.insert(pair<int,int>(x,0));
            }
            printf("%d
    ",m[x]);
        }
    }
  • 相关阅读:
    POJ1769 Minimizing maximizer(DP + 线段树)
    ZOJ3201 Tree of Tree(树形DP)
    POJ3613 Cow Relays(矩阵快速幂)
    POJ3635 Full Tank?(DP + Dijkstra)
    ZOJ3195 Design the city(LCA)
    POJ3368 Frequent values(RMQ线段树)
    POJ3686 The Windy's(最小费用最大流)
    HDU4871 Shortest-path tree(最短路径树 + 树的点分治)
    POJ3013 Big Christmas Tree(最短路径树)
    Gym100685G Gadget Hackwrench(倍增LCA)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4605317.html
Copyright © 2011-2022 走看看