Codeforces 555 C. Case of Chocolate
\(>Codeforces \space 555 C. Case of Chocolate<\)
题目大意 : 有一块 \(n \times n\) 的倒三角的巧克力,有一个人要吃 \(q\) 次,每一次从一个在对角线上的点出发,向左或者向上吃若干块,直到吃到已经被吃掉的格子为止,对于每一次吃巧克力的操作,输出这一次被吃掉了多少巧克力。(文字表达不够清晰,可以配合图片理解)
\(1 ≤ n ≤ 10^9\ 1 ≤ q ≤ 2 \times 10^5\)
解题思路 :
询问可以转化为找到某一行或一列第一个被吃掉的位置,那么可以对于每一次修改,用数据结构维护这个东西
离散化后,维护每一行/列被吃掉的编号最大的格子,问题转化为了一个单点询问,区间修改的问题
可以对于行和列各建一棵线段树,对于吃掉行就修改列的线段树,对于吃掉列就修改行的线段树
对于所有询问操作就直接查对应点的答案
对于修改部分,以吃第 \(x\) 行为例,设查询到当前行被吃掉的列编号最大的的编号为 \(y\)
那么对于第 \(y + 1\) 到第 \(n - x + 1\) 列都要对 \(x\) 取 \(max\) 直接区间修改列的线段树即可, 吃列的操作同理
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define lson (u << 1)
#define rson (u << 1 | 1)
#define int ll
#define N (600005)
char t[10];
map<int, int> mp[3];
int s[N], xx[N], yy[N], op[N], ff[N], gg[N], n, q, col;
struct Point{ int x, op, id; } X[N], Y[N];
inline bool cmp(Point A, Point B){ return A.x < B.x; }
struct SegmentTree{
int tag[N<<2], mx[N<<2];
inline void pushdown(int u){
if(!tag[u]) return;
mx[lson] = Max(mx[lson], tag[u]);
mx[rson] = Max(mx[rson], tag[u]);
tag[lson] = Max(tag[lson], tag[u]);
tag[rson] = Max(tag[rson], tag[u]), tag[u] = 0;
}
inline void change(int u, int l, int r, int L, int R, int v){
if(l >= L && r <= R){
mx[u] = Max(mx[u], v), tag[u] = Max(tag[u], v); return;
}
int mid = l + r >> 1; pushdown(u);
if(L <= mid) change(lson, l, mid, L, R, v);
if(mid < R) change(rson, mid + 1, r, L, R, v);
mx[u] = Max(mx[lson], mx[rson]);
}
inline int query(int u, int l, int r, int pos){
if(l == r) return mx[u];
int mid = l + r >> 1; pushdown(u);
if(pos <= mid) return query(lson, l, mid, pos);
else return query(rson, mid + 1, r, pos);
}
}R, C;
inline void calcX(int x){
xx[X[x].id] = col, op[X[x].id] = X[x].op, ff[col] = X[x].x;
}
inline void calcY(int x){
yy[Y[x].id] = col, op[Y[x].id] = Y[x].op, gg[col] = Y[x].x;
}
main(){
read(n), read(q);
for(int i = 1, x, y; i <= q; i++){
read(x), read(y), scanf("%s", t);
if(t[0] == 'U') X[i] = (Point){x, 1, i}, Y[i] = (Point){y, 1, i};
if(t[0] == 'L') X[i] = (Point){x, 2, i}, Y[i] = (Point){y, 2, i};
}
sort(X + 1, X + q + 1, cmp), ++col, calcX(1);
for(int i = 2; i <= q; i++){ if(X[i].x > X[i-1].x) ++col; calcX(i); }
int m = col; col = 0;
sort(Y + 1, Y + q + 1, cmp), ++col, calcY(1);
for(int i = 2; i <= q; i++){ if(Y[i].x > Y[i-1].x) ++col; calcY(i); }
m = max(m, col) + 100000;
for(int i = 1; i <= q; i++){
if(op[i] == 1){
if(mp[1][xx[i]]){ puts("0"); continue; }
int ls = R.query(1, 1, m, xx[i]); mp[1][xx[i]] = 1;
C.change(1, 1, m, ls + 1, yy[i], xx[i]);
printf("%lld\n", n - ff[xx[i]] + 1 - gg[ls]);
}
if(op[i] == 2){
if(mp[2][yy[i]]){ puts("0"); continue; }
int ls = C.query(1, 1, m, yy[i]); mp[2][yy[i]] = 1;
R.change(1, 1, m, ls + 1, xx[i], yy[i]);
printf("%lld\n", n - gg[yy[i]] + 1 - ff[ls]);
}
}
return 0;
}
Codeforces 555 C. Case of Chocolate的更多相关文章
- Codeforces 555 B. Case of Fugitive
\(>Codeforces \space 555 B. Case of Fugitive<\) 题目大意 : 有 \(n\) 个岛屿有序排列在一条线上,第 \(i\) 个岛屿的左端点为 \ ...
- 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/ ...
- Codeforces 555C Case of Chocolate 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/9272797.html 题目传送门 - CF555C 题意 给定一个 $n\times n(n\leq 10^9 ...
- codeforces 555c// Case of Chocolate// Codeforces Round #310(Div. 1)
题意:直角边为n的网格巧克力,一格为一块,选择斜边上一点,从左或上吃,直到吃到空气,称为一次操作.给出几个操作,问各能吃几块.如果x是当前要吃的横坐标,在已经吃过的中找x1>=x的第一个x1,即 ...
- Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)
题目地址:传送门 这题尽管是DIV1的C. . 可是挺简单的. .仅仅要用线段树分别维护一下横着和竖着的值就能够了,先离散化再维护. 每次查找最大的最小值<=tmp的点,能够直接在线段树里搜,也 ...
- 【35.37%】【codeforces 556C】Case of Matryoshkas
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【66.47%】【codeforces 556B】Case of Fake Numbers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 556A:Case of the Zeros and Ones
A. Case of the Zeros and Ones time limit per test 1 second memory limit per test 256 megabytes input ...
- codeforces 678C C. Joty and Chocolate(水题)
题目链接: C. Joty and Chocolate time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- 【BZOJ】1635: [Usaco2007 Jan]Tallest Cow 最高的牛
[题意]n头牛,其中最高h.给定r组关系a和b,要求满足h[b]>=h[a]且a.b之间都小于min(h[a],h[b]),求第i头牛可能的最高高度. [算法]差分 [题解]容易发现r组关系只能 ...
- 简易微信小程序签到功能
一.效果图 点击签到后 二.数据库 用一张数据表存用户签到的信息,每次用户签到都会往表中添加一条记录了用户id和签到日期的数据,如下图 三.后端 后端写两个接口,一个用于查询用户今日是否签到和签到记录 ...
- python dlib 面部轮廓实时检测
1.dlib 实现动态人脸检测及面部轮廓检测 模型下载连接 : http://dlib.net/files/ # coding:utf-8 import cv2 import os import dl ...
- Maven学习笔记(一)
我们暂且可以把Maven理解成是一个项目构建与依赖管理的工具 为什么选用maven? 约定(惯例)优先原则,默认限定了项目目录结构 提供三方依赖管理(解决了依赖维护的问题) 提供了一致的项目构建管 ...
- Msfvenom学习总结-MSF反弹webshell
1. –p (- -payload-options) 添加载荷payload. 载荷这个东西比较多,这个软件就是根据对应的载荷payload生成对应平台下的后门,所以只有选对payload,再填 ...
- libSVM笔记之(一)在matlab环境下安装配置libSVM
本文为原创作品,转载请注明出处 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing 台湾林智仁教 ...
- python实战===国内很简单实用的一些开源的api以及开源项目
原创 2017年03月25日 15:40:59 标签: api / 开源项目 / app / 免费接口 声明 以下所有 API 均由产品公司自身提供,本人皆从网络获取.获取与共享之行为或有侵犯产品 ...
- 4.FireDAC组件快照 二
TFDUpdateSQL 生成添加,删除,修改SQL语句 TFDMetaInfoQuery 查询数据源信息 TFDEventAlerter 负责处理数据库事件通知 使用TFDEventAlerter类 ...
- 动画基础--基于Core Animation(3)
参考:https://zsisme.gitbooks.io/ios-/content/ 前面的文章动画基础--基于Core Animation(1),动画基础--基于Core Animation(2) ...
- C语言比较巧妙的字符串分割程序
在解析字符串时,能够解析的给出每个字符串的长度.内容.以及每个字符串的第一个字符的地址. short i; ; //切割之后的字符串的个数 ,ItemLen[],Idx[], ThCommandLen ...
