[luogu P2205] [USACO13JAN]画栅栏Painting the Fence

题目描述

Farmer John has devised a brilliant method to paint the long fence next to his barn (think of the fence as a one-dimensional number line). He simply attaches a paint brush to his favorite cow Bessie, and then retires to drink a cold glass of water as Bessie walks back and forth across the fence, applying paint to any segment of the fence that she walks past.

Bessie starts at position 0 on the fence and follows a sequence of N moves (1 <= N <= 100,000). Example moves might be "10 L", meaning Bessie moves 10 units to the left, or "15 R", meaning Bessie moves 15 units to the right. Given a list of all of Bessie's moves, FJ would like to know what area of the fence gets painted with at least K coats of paint. Bessie will move at most 1,000,000,000 units away from the origin during her walk.

Farmer John 想出了一个给牛棚旁的长围墙涂色的好方法。(为了简单起见,我们把围墙看做一维的数轴,每一个单位长度代表一块栅栏)他只是简单的把刷子蘸满颜料,系在他最喜欢的奶牛Bessie上,然后让Bessie来回地经过围墙,自己则在一旁喝一杯冰镇的凉水。(……-_-|||) Bessie 经过的所有围墙都会被涂上一层颜料。Bessie从围墙上的位置0出发,并将会进行N次移动(1 <= N <= 100,000)。比如说,“10 L”的意思就是Bessie向左移动了10个单位。再比如说“15 R”的意思就是Bessie向右移动了15个单位。给出一系列Bessie移动的清单。FJ 想知道有多少块栅栏涂上了至少K层涂料。注意:Bessie最多会移动到离原点1,000,000,000单位远的地方。

输入输出格式

输入格式:

  • 第1行: 两个整数: N K

  • 第2...N+1 行: 每一行都描述了Bessie的一次移动。 (比如说 “15 L")

输出格式:

  • 一个整数:被至少涂上K层涂料的栅栏数

(注意:输出的最后一定要输出换行符!否则会WA)

输入输出样例

输入样例#1: 复制

6 2
2 R
6 L
1 R
8 L
1 R
2 R 
输出样例#1: 复制

6

说明

PS1:来源:usaco jan silver P01 想看原题的请戳http://www.usaco.org/index.php?page=viewproblem2&cpid=226)

PS2:测试数据也可以在在http://www.usaco.org/index.php?page=jan13problems上下载,还可以看到题解(不过是英文的:-D)

PS3:如果有翻译的问题或题目的不理解,可以在问答后面留言的说。

这道题很早就写过,当时用离散+差分就水过去了。

今天心血来潮写了一颗链表指针版的线段树。。

这真是个好东西。。

还有usaco竟然可以载数据,很妙啊。。

哦还有就是,今天终于会用unique,lower_bound和upper_bound了。

code:

 %:pragma GCC optimize()
 #include<bits/stdc++.h>
 using namespace std;
 ;
 int n,k,las,cnt,ans,b[N]; char ch;
 struct line {int x,y;}a[N];
 class node {
     private:
         int v,t; node *l,*r;
     public:
         #define m ((l)+(r)>>1)
         node() {l=r=,v=t=;}
         inline void pushup(node* cu) {
             cu->v=;
             ) cu->v+=cu->l->v;
             ) cu->v+=cu->r->v;
         }
         inline void pushdown(node *cu) {
             ) cu->l->t+=cu->t,cu->l->v+=cu->t;
             ) cu->r->t+=cu->t,cu->r->v+=cu->t;
             cu->t=;
         }
         inline void setup(node* &cu,int l,int r) {
             cu=new node;
             ==r) {cu->v=cu->t=; return;}
             setup(cu->l,l,m),setup(cu->r,m,r);
             pushup(cu);
         }
         inline void update(node* &cu,int l,int r,int aiml,int aimr) {
             if (l>=aiml&&r<=aimr) {cu->v++,cu->t++; return;}
             pushdown(cu);
             if (aimr<=m) update(cu->l,l,m,aiml,aimr); else
             if (aiml>=m) update(cu->r,m,r,aiml,aimr);
             else update(cu->l,l,m,aiml,aimr),update(cu->r,m,r,aiml,aimr);
             pushup(cu);
         }
         inline int answer(node* &cu,int l,int r,int x) {
             ==r) return cu->v;
             pushdown(cu);
             return x<m?answer(cu->l,l,m,x):answer(cu->r,m,r,x);
         }
 }t,*root;
 inline int read() {
     ,f=; ch=getchar();
     :,ch=getchar();
     +ch-',ch=getchar();
     return x*f;
 }
 int main() {
     n=read(),k=read(),ans=las=,b[]=;
     ,x; i<=n; i++) {
         x=read();
         while (ch!='L'&&ch!='R') ch=getchar();
         if (ch=='L') a[i].x=las-x,a[i].y=las,b[i]=las-x,las=a[i].x;
         else a[i].x=las,a[i].y=las+x,b[i]=las+x,las=a[i].y;
     }
     sort(b,b++n);
     cnt=unique(b,b++n)-b;
     root=,t.setup(root,,cnt);
     ,l,r; i<=n; i++) {
         l=a[i].x,r=a[i].y;
         l=lower_bound(b,b+cnt,l)-b,l++;
         r=lower_bound(b,b+cnt,r)-b,r++;
         t.update(root,,cnt,l,r);
     }
     ,s; i<cnt; i++)
         s=t.answer(root,,cnt,i),ans+=(b[i]-b[i-])*(s>=k);
     cout<<ans<<endl;
     ;
 }

[luogu P2205] [USACO13JAN]画栅栏Painting the Fence的更多相关文章

  1. 洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence

    题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of t ...

  2. 洛谷 P2205 [USACO13JAN]画栅栏Painting the Fence

    传送门 题目大意: 开始站在原点,给出一系列操作 x L/R,表示向左或向右走几步. 最多会移动到离原点1,000,000,000单位远的地方. n次操作,n<=100000 问走过k次的地方有 ...

  3. 洛谷 画栅栏Painting the Fence 解题报告

    P2205 画栅栏Painting the Fence 题目描述 \(Farmer\) \(John\) 想出了一个给牛棚旁的长围墙涂色的好方法.(为了简单起见,我们把围墙看做一维的数轴,每一个单位长 ...

  4. 洛谷 P2205 [USACO13JAN]画栅栏

    这题其实没什么,但用到的算法都十分有用.做一个不恰当的比喻,这是一只必须用牛刀杀的鸡,但因为我这个蒟蒻杀不死牛,所以只能找只鸡来练练手. 题目描述 Farmer John 想出了一个给牛棚旁的长围墙涂 ...

  5. 洛谷 P3079 [USACO13MAR]农场的画Farm Painting

    P3079 [USACO13MAR]农场的画Farm Painting 题目描述 After several harsh winters, Farmer John has decided it is ...

  6. Painting The Fence(贪心+优先队列)

    Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...

  7. [Swift]LeetCode587. 安装栅栏 | Erect the Fence

    There are some trees, where each tree is represented by (x,y) coordinate in a two-dimensional garden ...

  8. 【luogu P2731 骑马修栅栏】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2731 这个题是欧拉回路的模板题,那么在这里给出一个hierholzer的做法. 对于求欧拉回路的问题,有Fl ...

  9. USACO 2013 January Silver Painting the Fence /// oj23695

    题目大意: 输入n,k :n次操作 找到覆盖次数在k及以上的段的总长 一开始位置在0 左右活动范围为1-1000000000 接下来n行描述每次操作的步数和方向 Sample Input 6 22 R ...

随机推荐

  1. Android Studio--》Gradle Scripts配置说明

    什么是Gradle? Gradle是一种依赖管理工具,基于Groovy语言,面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言. 安装 ...

  2. Centos下搭建邮件服务器

    一.协议 SMTP:用于发送邮件 POP3:用于接收邮件,接收后会将服务器上邮件删除 IMAP:用于接收邮件,接收后不会删除服务器邮件 二.几个重要的角色 MUA:可以理解为收取邮件的工具,比如thu ...

  3. python tkinter Label

    """小白随笔,大佬勿喷""" #Label标签 用于可显示文本或图片,不可编辑 import tkinter as tk #初始化窗口 w ...

  4. Kafka实践1--Producer

    一.Kafka设计原理参考: http://blog.csdn.net/suifeng3051/article/details/48053965?locationNum=2 http://www.cn ...

  5. 【UML】NO.53.EBook.5.UML.1.013-【UML 大战需求分析】- 组合结构图(Composition Structure Diagram)

    1.0.0 Summary Tittle:[UML]NO.52.EBook.1.UML.1.012-[UML 大战需求分析]- 交互概览图(Interaction Overview Diagram) ...

  6. android studio中编译单个文件

    网上搜到比较全的是这个:https://blog.csdn.net/u011368551/article/details/51980678 另外关于gradle如何编译单个文件,参考 https:// ...

  7. 第四章 jQuery节点操作

    1.DOM操作分为三类:(1)DOM Core:任何一种支持DOM的编程语言都可以使用用它如:getElementById()(2)HTML-DOM:用于处理HTML文档,如document,form ...

  8. 斐讯面试记录—TCP滑动窗口及拥塞控制

    TCP协议作为一个可靠的面向流的传输协议,其可靠性是由流量控制和滑动窗口协议保证,而拥塞控制则由控制窗口结合一系列的控制算法实现. 一.滑动窗口协议 1. “窗口”对应的是一段可以被发送者发送的字节序 ...

  9. Lucky 7 (容斥原理 + 中国剩余定理)

    题意:求满足7的倍数,不满足其他条件num % p == a 的num的个数. 思路:利用中国剩余定理我i们可以求出7的倍数,但是多算了不满足约定条件又得减去一个,但是又发现多减了,又得加回来.如此, ...

  10. CentOS7查看systemctl 控制的服务的相关配置

    例如,启动配置文件 [root@Docker_Machine_192.168.31.130 ~]# systemctl show --property=FragmentPath docker Frag ...