离散化+线段树 codefores555C Case of Chocolate
2016-04-18 19:48
337 查看
传送门:点击打开链接
题意:阶梯形的巧克力,每次选择边缘的一个格子,然后向上吃或者向左吃,直到边界或者空白位置停止,吃过的位置之后就是空白。
思路:n很大,先把q查询的那些位置离散化一下,注意x+1和y+1也需要离散化一下。
之后y会限制后来的x,x会限制后来的y,搞两个分开的线段树分别维护x和y就行了
还要弄个vis来判断某一行或某一列是否已经放过,就ok了
总的来说只要想清楚了细节,不难处理
题意:阶梯形的巧克力,每次选择边缘的一个格子,然后向上吃或者向左吃,直到边界或者空白位置停止,吃过的位置之后就是空白。
思路:n很大,先把q查询的那些位置离散化一下,注意x+1和y+1也需要离散化一下。
之后y会限制后来的x,x会限制后来的y,搞两个分开的线段树分别维护x和y就行了
还要弄个vis来判断某一行或某一列是否已经放过,就ok了
总的来说只要想清楚了细节,不难处理
#include <map> #include <set> #include <cmath> #include <ctime> #include <stack> #include <queue> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> #include <functional> #define fuck(x) cout<<"["<<x<<"]" #define FIN freopen("input.txt","r",stdin) #define FOUT freopen("output.txt","w+",stdout) using namespace std; typedef long long LL; typedef pair<int, int> PII; const int MX = 4e5 + 5; char op[MX][2]; int n, q, xsz, ysz; int X[MX], Y[MX], BX[MX], BY[MX]; int MAX[2][MX << 2], col[2][MX << 2]; int visx[MX], visy[MX]; #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 int bsx(int x) { return lower_bound(BX + 1, BX + 1 + xsz, x) - BX; } int bsy(int x) { return lower_bound(BY + 1, BY + 1 + ysz, x) - BY; } void push_up(int z, int rt) { MAX[z][rt] = max(MAX[z][rt << 1], MAX[z][rt << 1 | 1]); } void upset(int z, int rt, int c) { MAX[z][rt] = max(MAX[z][rt], c); col[z][rt] = max(col[z][rt], c); } void push_down(int z, int rt) { if(col[z][rt]) { upset(z, rt << 1, col[z][rt]); upset(z, rt << 1 | 1, col[z][rt]); col[z][rt] = 0; } } int query(int z, int p, int l, int r, int rt) { if(l == r) { return MAX[z][rt]; } int m = (l + r) >> 1; push_down(z, rt); if(p <= m) return query(z, p, lson); else return query(z, p, rson); } void update(int z, int L, int R, int x, int l, int r, int rt) { if(L <= l && r <= R) { upset(z, rt, x); return; } int m = (l + r) >> 1; push_down(z, rt); if(L <= m) update(z, L, R, x, lson); if(R > m) update(z, L, R, x, rson); push_up(z, rt); } int main() { //FIN; scanf("%d%d", &n, &q); for(int i = 1; i <= q; i++) { scanf("%d%d%s", &X[i], &Y[i], op[i]); swap(X[i], Y[i]); BX[++xsz] = X[i]; BX[++xsz] = X[i] + 1; BY[++ysz] = Y[i]; BY[++ysz] = Y[i] + 1; } sort(BX + 1, BX + 1 + xsz); xsz = unique(BX + 1, BX + 1 + xsz) - BX - 1; sort(BY + 1, BY + 1 + ysz); ysz = unique(BY + 1, BY + 1 + ysz) - BY - 1; for(int i = 1; i <= q; i++) { if(op[i][0] == 'L') { if(visx[bsx(X[i])]) { printf("0\n"); continue; } visx[bsx(X[i])] = 1; int t = query(0, bsx(X[i]), 1, xsz, 1); update(1, bsy(t + 1), bsy(Y[i]), X[i], 1, ysz, 1); printf("%d\n", Y[i] - t); } else { if(visy[bsy(Y[i])]) { printf("0\n"); continue; } visy[bsy(Y[i])] = 1; int t = query(1, bsy(Y[i]), 1, ysz, 1); update(0, bsx(t + 1), bsx(X[i]), Y[i], 1, xsz, 1); printf("%d\n", X[i] - t); } } return 0; }
相关文章推荐
- uva 10304 - Optimal Binary Search Tree(区间dp)
- Linux操作提示:“Cant open file for writing”或“operation not permitted”的解决办法
- 嵌入式平台下的ldd
- CPU acceleration status:HAXM must be updated(version 1.1.1<6.0.1)
- 获得执行jar的运行路径
- 中国大陆CDN的测量与分析
- Labview队列操作
- hdu 1142 最短路径+dfs记忆化搜索
- 【小笨鸟看JDK1.7集合源码之一】JAVA集合整体框架
- codeforces_304C_数学题
- iOS:实现图片的无限轮播之使用第三方库SDCycleScrollView
- subline的node.js安装和快捷键
- 解决android-Ultra-Pull-To-Refresh 和ViewPager冲突的问题
- 结对编程
- LeetCode|Majority Element *
- 不用工具就可以在虚拟机安好系统后,并只有c盘的情况下增加分区
- 图结构练习——最小生成树
- 第七周
- RBM/DBN训练中的explaining away概念
- Spring-Security