Codeforces Round #310 (Div. 1) C Case of Chocolate

思路:对于每个点而言、只与它相邻的两个点有关系、所以可以用stl或者线段树来找到它的相邻点、

代码:187ms(开挂之后貌似是最快的- -)


#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;

const int N = 200000 + 1;

int x[N], y[N], t[N];

//适用于正负整数
template <class T>
inline bool scan_d(T &ret) {
   char c; int sgn;
   if(c = getchar(),c == EOF) return 0; //EOF
   while(c != '-' && (c < '0' || c > '9')) c = getchar();
   sgn = (c == '-') ? -1 : 1;
   ret = (c == '-') ? 0 : (c - '0');
   while(c = getchar(),c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
   ret *= sgn;
   return 1;
}

inline void out(int x) {
   if(x > 9) out(x / 10);
   putchar(x % 10 + '0');
}

int main() {
    int n, q;
    scanf("%d%d", &n, &q);
    x[q] = y[q] = 0;
    map <int, int> mp;
    mp[0] = q;
    mp[n + 1] = q;
    for (int i = 0; i < q; ++ i) {
        char buffer[2];
        scan_d(x[i]);
        scan_d(y[i]);
        scanf("%s",buffer);
        t[i] = *buffer == 'U';
        map <int, int>::iterator iterator;
        iterator = mp.lower_bound(x[i]);
        if (x[i] == iterator->first) {
            puts("0"); continue;
        }
        if (!t[i]) iterator --;
        int j = iterator->second;
        mp[x[i]] = i;
        if (t[i]) {
            out(y[i] - y[j]);
            putchar('\n');
            y[i] = y[j];
        } else {
            out(x[i] - x[j]);
            putchar('\n');
            x[i] = x[j];
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

N诺计算机考研

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值