Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)
来源:互联网 发布:中值滤波快速算法 编辑:程序博客网 时间:2024/06/07 15:15
题目地址:传送门
这题虽然是DIV1的C。。但是挺简单的。。只要用线段树分别维护一下横着和竖着的值就可以了,先离散化再维护。每次查找最大的最小值<=tmp的点,可以直接在线段树里搜,也可以二分去找。
代码如下:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>#include <time.h>using namespace std;#define LL __int64#define pi acos(-1.0)//#pragma comment(linker, "/STACK:1024000000")#define root 0, cnt-1, 1#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;const int MAXN=400000+10;int a[MAXN], c[MAXN], cnt, ha[MAXN];int Min[2][MAXN<<2];struct node{ int x, y, f;}fei[MAXN];void PushUp(int f, int rt){ Min[f][rt]=min(Min[f][rt<<1],Min[f][rt<<1|1]);}void Update(int f, int p, int x, int l, int r, int rt){ if(l==r){ Min[f][rt]=x; return ; } int mid=l+r>>1; if(p<=mid) Update(f,p,x,lson); else Update(f,p,x,rson); PushUp(f,rt);}int seach(int f, int rr, int x, int l, int r, int rt){ if(l==r){ if(Min[f][rt]<=x) return l; return -1; } int ans=-1, mid=l+r>>1; if(rr>mid&&Min[f][rt<<1|1]<=x) ans=seach(f,rr,x,rson); if(ans!=-1) return ans; if(Min[f][rt<<1]<=x) ans=seach(f,rr,x,lson); return ans;}int BS(int x){ int low=0, high=cnt-1, mid; while(low<=high){ mid=low+high>>1; if(c[mid]==x) return mid; else if(c[mid]>x) high=mid-1; else low=mid+1; }}int main(){ int n, q, i, j, x, y, tmpx, tmpy, z; char ch[3]; while(scanf("%d%d",&n,&q)!=EOF){ for(i=0;i<q;i++){ scanf("%d%d",&fei[i].x,&fei[i].y); scanf("%s",ch); if(ch[0]=='U'){ a[i<<1]=fei[i].x; a[i<<1|1]=fei[i].y; fei[i].f=0; } else{ a[i<<1]=fei[i].x; a[i<<1|1]=fei[i].y; fei[i].f=1; } } memset(ha,0,sizeof(ha)); sort(a,a+2*q); c[0]=a[0]; cnt=1; for(i=1;i<2*q;i++){ if(a[i]!=a[i-1]){ c[cnt++]=a[i]; } } memset(Min,INF,sizeof(Min)); for(i=0;i<q;i++){ tmpx=BS(fei[i].x); tmpy=BS(fei[i].y); if(ha[tmpx]){ puts("0"); continue ; } ha[tmpx]=1; if(fei[i].f){ if(tmpx==0){ printf("%d\n",fei[i].x); Update(0,tmpy,0,root); continue ; } z=seach(1,tmpx,tmpy,root); if(z==-1){ printf("%d\n",fei[i].x); Update(0,tmpy,0,root); continue ; } printf("%d\n",fei[i].x-c[z]); Update(0,tmpy,z+1,root); } else{ if(tmpy==0){ printf("%d\n",fei[i].y); Update(1,tmpx,0,root); continue ; } z=seach(0,tmpy,tmpx,root); if(z==-1){ printf("%d\n",fei[i].y); Update(1,tmpx,0,root); continue ; } printf("%d\n",fei[i].y-c[z]); Update(1,tmpx,z+1,root); } } } return 0;}
1 0
- Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)
- Codeforces Round #310 (Div. 1) C Case of Chocolate
- Codeforces Round #310 (Div. 1) C. Case of Chocolate stl应用
- 线段树 Case of Chocolate:CodeForces
- [Codeforces Round #310 DIV1C (CF555C)] Case of Chocolate
- Codeforces Round #310 (Div. 1) B. Case of Fugitive
- Codeforces Round #310 (Div. 1) B. Case of Fugitive
- Codeforces Round #310 (Div. 1) B. Case of Fugitive 贪心
- codeforces 555 C Case of Chocolate
- codeforces 555C Case of Chocolate(map)
- Codeforces 555C Case of Chocolate
- Codeforces Round #257 (Div. 2) 1C Jzzhu and Chocolate
- C. Case of Matryoshkas( Codeforces Round #310 (Div. 2) 模拟)
- Codeforces Round #310 (Div. 2) B. Case of Fake Numbers
- 线段树Codeforces Beta Round #99 (Div. 1)C
- Codeforces Round #225 (Div. 1) C 树状数组 || 线段树
- Codeforces Round #225 (Div. 1)C(dfs+线段树)
- Codeforces Round #225 (Div. 1)-C. Propagating tree(线段树)
- python3中关于编码那些事儿
- Scapy介绍官方文档翻译
- PASSION之CSS3-outline
- JDBC操作LOB
- Android基础知识_Application的用途
- Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)
- 抓包工具Wireshark-----学习篇:9 显示过滤器分析特定数据流
- 位运算的常见操作和题目
- 修复吾爱OD数据窗口双击不出现偏移问题
- 常见的排序算法
- VMware 复制OS后出现:RTNETLINK answers: File exists错误解决方法
- 【leetcode】House Robber && House Robber II
- 抓包工具Wireshark----学习篇:10
- 欢迎使用CSDN-markdown编辑器