题目地址:传送门

这题尽管是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. MYSQL数据库攻防与加固

    这是“官方”原本的模样搬过来的..写的很粗略啊.还有篇详细的请查看:MySQL安全加固题目及答案参考解析 启动xserver-mysql,进入xserver-mysql,开始实验,实验步骤如下: 1. ...

  2. docker使用阿里云镜像仓库docker

    1:阿里云docker仓库 https://dev.aliyun.com/search.html 2:进去注册帐号后,点击自己的管理中心. 3:在管理中心点击加速器,右边面板会有你的加速地址,右边面板 ...

  3. python 2018/8/25

    # 含多空格字符串的分割 hello = "hello python hello"print(a.split(" ")) # ['hello', 'python ...

  4. 条款36:绝不重新定义继承而来的non-virtual函数(Never redefine an inherited non-virtual function)

    NOTE: 1.绝对不要重新定义继承而来的non-virtual函数.

  5. LayUI分页基于ASP.NET MVC

    ---恢复内容开始--- 今天写了挺久的分页,百度了很多都没有很好的.Net实例,今天我来更新一期关于layuiTable分页 首先你得理解layui的官方文档的Table分页部分,我在这里附上地址 ...

  6. FZU-1881-Problem 1881 三角形问题,打表二分查找~~

    B - 三角形问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descripti ...

  7. Problem 2125 简单的等式(FZU),,数学题。。。

    Problem 2125 简单的等式 Time Limit: 1000 mSec Memory Limit : 32768 KB  Problem Description 现在有一个等式如下:x^2+ ...

  8. Go循环语句

    package main import ( "fmt" "strconv" "os" "bufio" ) //for的条 ...

  9. VIM键盘图

  10. 【dp】codeforces C. Vladik and Memorable Trip

    http://codeforces.com/contest/811/problem/C [题意] 给定一个自然数序列,在这个序列中找出几个不相交段,使得每个段的异或值之和相加最大. 段的异或值这样定义 ...