Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)
题目地址:传送门
这题尽管是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 (线段树)的更多相关文章
- 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 Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers
题目传送门 /* 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 构造:先求出使第1个指向0要多少 ...
- 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones
题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...
- 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 ...
- 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. ...
- 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 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- 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. 这题 ...
随机推荐
- 文本三剑客之sed
sed是一个流编辑器(sed是stream editor的缩写),它可以对从标准输入流中得到的数据进行处理,然后把处理以后得到的结果输出到标准输出,而标准输出通常会关联到终端屏幕,因此处理后的结果也会 ...
- QEMU支持的几种常见的镜像文件格式
qemu-img支持非常多种的文件格式,可以通过"qemu-img -h"查看其命令帮助得到,它支持二十多种格式:blkdebug.blkverify.bochs.cloop.c ...
- JS模块之AMD, CMD, CommonJS、UMD和ES6模块
CommonJS 传送门 同步加载,适合服务器开发,node实现了commonJS.module.exports和require 判断commonJS环境的方式是(参考jquery源码): if ( ...
- Java基础学习总结(91)——阿里巴巴Java开发手册公开版
1.不要嫌名字长 无论是方法,变量,还是函数的取名,不要嫌弃名称太长,只要能够表示清楚含义就可以了. 2.String[] args而不是String args[] 中括号是数组类型的一部分,数组定义 ...
- 五、PL/SQL循环、游标、函数和过程
--PL/SQL基础知识学习 --一.PL/SQL语句块,基础语法格式 DECLARE --变量声明列表 info varchar(25); --变量声明 stu_unm integer := 15; ...
- HDU 5487 Difference of Languages
Difference of Languages Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. ...
- Python内置函数—bytearray
英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cla ...
- Leetcode 313.超级丑数
超级丑数 编写一段程序来查找第n个超级丑数. 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数. 示例: 输入: n = 12, primes = [2,7,13,19] ...
- Dream City(线性DP)
描述 JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the ya ...
- 【POJ1149&BZOJ1280】PIGS(最大流)
题意:Emmy在一个养猪场工作.这个养猪场有M个锁着的猪圈,但Emmy并没有钥匙. 顾客会到养猪场来买猪,一个接着一个.每一位顾客都会有一些猪圈的钥匙,他们会将这些猪圈打开并买走固定数目的猪. 所有顾 ...