题目地址:传送门

这题尽管是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|1
const 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;
}

Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)的更多相关文章

  1. 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/ ...

  2. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  3. 构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers

    题目传送门 /* 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 构造:先求出使第1个指向0要多少 ...

  4. 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

    题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...

  5. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  6. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  7. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  8. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  9. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

随机推荐

  1. python基础002

    1.pycharm安装与添加解释器 专业版.英文界面,不要汉化—一定要尊重知识产权 算术运算符:+ - * / // % ** ^ 布尔运算符:== > < >= <= 逻辑运 ...

  2. Spring核心技术(九)——Spring管理的组件和Classpath扫描

    Spring管理的组件和Classpath的扫描 在前文描述中使用到的Spring中的Bean的定义,都是通过指定的XML来配置的.而前文中描述的注解的解析则是在源代码级别来提供配置元数据的.在那些例 ...

  3. ArrayList练习之存储自定义对象并遍历

    新建一个Student.java类 Student.java /* * 这是一个学生类 */ public class Student { private String name; private i ...

  4. Android弹幕编程设计实现的解决方案(一)

     Android弹幕编程设计实现的解决方案(一) 在现在的一些视频类网站.视频类直播网站,比如A站和B站,当视频在播放的时候,会在屏幕上出现一些滚动的字幕,这些字幕是UGC,通常是用户的评论,称之 ...

  5. hdu4352 XHXJ's LIS(数位DP + LIS + 状态压缩)

    #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire ...

  6. Python的另一种开发环境--Anaconda中的Spyder

    本文作者LucyGill,转载请注明出处(虽然我觉得并不会有人转载). 刚开始学Python的时候,我用的是其自带的idle(安装Python后,在开始菜单里可以找到),后来发现在eclipse中设置 ...

  7. JS变量写到HTML页面中并修改变量值(前台处理数据序号问题)

    有时候我们在前台需要对序号进行处理,我们需要将JS变量写到页面中进行显示. 第一种方式:后台处理 第二种方式:JS中定义全局变量,然后进行显示,可以书写一个JS函数对不同的需要进行不同的替换,也就可以 ...

  8. ubuntu 14.04 安装docker,docker-compose

    通过阿里的镜像安装 curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | s ...

  9. Mysql 数据库允许远程连接 服务器连接错误 Host 'XXX' is not allowed to connect to this MySQL server

    如果连接数据库的时候出现这个问题 Host 'XXX' is not allowed to connect to this MySQL server 说明 Mysql数据库 不允许远程连接, 需要修改 ...

  10. makefile的语法及写法

    什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makefile还是要 ...