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. 这题 ...
随机推荐
- centeros 6 远程升级ssl ssh 的shell脚本
变量说明 SSL_N=openssl-1.0.2p #ssl 版本SSH_N=openssh-7.9p1 #ssh 版本ZLIB_N=zlib-1.2.11 # zlib 版本 脚本分为两个,因为升级 ...
- (6) openssl passwd(生成加密的密码)
该伪命令用于生成加密的密码 [root@docker121 ssl]# man -f passwd passwd (1) - update user's authentication tokens p ...
- Python爬虫-Scrapy-CrawlSpider与ItemLoader
一.CrawlSpider 根据官方文档可以了解到, 虽然对于特定的网页来说不一定是最好的选择, 但是 CrwalSpider 是爬取规整的网页时最常用的 spider, 而且有很好的可塑性. 除了继 ...
- 分享下找到的Github上大神的EmpireofCode进攻策略:反正我是用了没反应,改了代码后单位不进攻,蠢站在那里,我自己的策略调调能打败不少人,这个日后慢慢研究吧,Github上暂时找到的唯一策略
from queue import PriorityQueue from enum import Enum from battle import commander from battle impor ...
- jquery怎么获得ul中li的个数
- python006 Python3 运算符
Python3 运算符什么是运算符?本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符.Python语言 ...
- loadrunner 并发操作集合点配置
在loadrunner的虚拟用户中,术语concurrent(并发)和simultaneous(同时)存在一些区别,concurrent 是指虚拟场景中参于运行的虚拟用户.而simultaneous与 ...
- Rem 字体设置学习一
JS方法动态计算根元素的字体大小: [淘宝首页:m.taobao.com] (function (doc, win) { var docEl = doc.documentElement, resize ...
- 第18章:MYSQL分区
第18章:分区 目录 18.1. MySQL中的分区概述 18.2. 分区类型 18.2.1. RANGE分区 18.2.2. LIST分区 18.2.3. HASH分区 18.2.4. KEY分区 ...
- Python从文件中读取字符串,用正则表达式匹配中文字符的问题
2013-07-27 21:01:37| 在Windows下,用Python从.txt文件中读取字符串,并用正则表达式匹配中文,在网上看了方法,用的时候发现中文没有被匹配. ...