ZOJ5593:Let's Chat(双指针)
传送门
题意
给出x个a区间和y个b区间,询问a和b交区间的子区间长度为m的个数
分析
类似于双指针,具体见代码
trick
代码
#include <bits/stdc++.h>
using namespace std;
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
#pragma comment(linker, "/STACK:102400000,102400000")
inline void read(int &x){x=0; char ch=getchar();while(ch<'0') ch=getchar();while(ch>='0'){x=x*10+ch-48; ch=getchar();}}
struct node
{
int l,r;
}a[101],b[101];
int t,n,m,x,y;
int loca,locb;
int ans;
int main()
{
for(scanf("%d",&t);t--;)
{
scanf("%d %d %d %d",&n,&m,&x,&y);
for(int i=1;i<=x;++i) scanf("%d %d",&a[i].l,&a[i].r);
for(int i=1;i<=y;++i) scanf("%d %d",&b[i].l,&b[i].r);
ans=0;locb=1;
for(int i=1;i<=x;++i)
{
if(locb>y) break;
if(a[i].r<b[locb].l) continue;
while(a[i].l>b[locb].r) locb++;
while(locb<=y&&b[locb].l<=a[i].r)
{
int rr=min(b[locb].r,a[i].r),ll=max(b[locb].l,a[i].l);
if(rr-ll+1>=m) ans+=(rr-ll+2-m);
if(b[locb].l>=a[i].l&&b[locb].r>=a[i].r) break;
if(b[locb].l<=a[i].l&&b[locb].r<=a[i].r) {locb++;continue;}
if(b[locb].l>=a[i].l&&b[locb].r<=a[i].r) {locb++;continue;}
if(b[locb].l<=a[i].l&&b[locb].r>=a[i].r) break;
locb++;
}
}
printf("%d\n",ans);
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>;
using namespace std;
struct points
{
int l,r;
};
vector <points> a,b;
int main()
{
int cases,n,m,x,y,i,j,s;
points now;
bool flag;
int m1,m2;
scanf("%d",&cases);
while (cases--)
{
scanf("%d%d%d%d",&n,&m,&x,&y);
a.clear(); b.clear();
for (i=1;i<=x;i++)
{
scanf("%d%d",&now.l,&now.r);
if (now.r-now.l+1>=m)
{
a.push_back(now);
}
}
for (i=1;i<=y;i++)
{
scanf("%d%d",&now.l,&now.r);
if (now.r-now.l+1>=m)
{
b.push_back(now);
}
}
if (a.size()==0 || b.size()==0) printf("0\n");
else
{
m1=0; m2=0; flag=true; s=0;
while (m1<a.size() && m2<b.size())
{
while (flag && a[m1].r<b[m2].l )
{
m1++;
if (m1==a.size()) {flag=false; break;}
}
while (flag && b[m2].r<a[m1].l )
{
m2++;
if (m2==b.size()) {flag=false; break;}
}
if (flag)
{
x=max(a[m1].l,b[m2].l);
y=min(a[m1].r,b[m2].r);
if (y-x+1>=m) s+=(y-x+1-m+1);
if (a[m1].r<b[m2].r) m1++;
else if (a[m1].r>b[m2].r) m2++;
else {m1++; m2++; }
}
}
printf("%d\n",s);
}
}
return 0;
}
ZOJ5593:Let's Chat(双指针)的更多相关文章
- 三周,用长轮询实现Chat并迁移到Azure测试
公司的OA从零开始进行开发,继简单的单点登陆.角色与权限.消息中间件之后,轮到在线即时通信的模块需要我独立去完成.这三周除了逛网店见爱*看动漫接兼职,基本上都花在这上面了.简单地说就是用MVC4基于长 ...
- Socket programing(make a chat software) summary 1:How to accsess LAN from WAN
First we should know some basic conceptions about network: 1.Every PC is supposed to have its own IP ...
- Node聊天程序实例03:chat.js
作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. chat.j ...
- Fake chat script for website download
Are you searching for free fake webchat script then you are at the right place go get download your ...
- IRC(Internet Relay Chat Protocol) Protocal Learning && IRC Bot
catalogue . Abstract . INTRODUCTION . 通信协议Connection Registration Action . 通信协议Channel operations Ac ...
- HDU 5071 Chat(2014鞍山赛区现场赛B题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 解题报告:一个管理聊天窗口的程序,一共有八种操作,然后要注意的就是Top操作只是把编号为u的窗口 ...
- ocket.chat 使用 Meteor 开发的实时协作工具,类似 丁丁。
ocket.chat 使用 Meteor 开发的实时协作工具,类似 丁丁. https://rocket.chat/
- 局域网聊天Chat(马士兵视频改进版)
Github地址: https://github.com/BenDanChen/Chat Chat 小小的聊天系统,主要是跟着网上的马士兵老师的公开视频然后再自己反思有什么地方需要改进的地方,然后大体 ...
- Dig out deleted chat messages of App Skype
Last month Candy was arrested on suspicion of having doing online porn webcam shows, but Candy refus ...
随机推荐
- 洛谷—— P1785 漂亮的绝杀
https://www.luogu.org/problem/show?pid=1785 题目背景 话说absi2011的企鹅在和斗神塔第60层的Boss战斗 不好,这局要输了,企鹅还剩4血了Boss还 ...
- foobar2000实现用手机远程控制PC命令行版
实现此功能主要是在手机上使用Telnet命令实现下一首播放. 1.安装Telnet守护进程插件:http://www.foobar2000.org/components/view/foo_telnet ...
- JAVA原始的导出excel文件,快捷通用 方便 还能够导出word文档哦
如今导出excel基本上都是用poi了,当报表格式非常负责的时候 开发难度会加大 假设报表有格式有变化 那就更复杂了,先发现一个非常老的技术.能够解决格式复杂的报表. 实例代码例如以下: <%@ ...
- SQL2012 尝试读取或写入受保护的内存。这通常指示其它内存已损坏
今天打开SQL2012,突然就连接不了数据库.一開始还以为是某个server崩溃了.结果试了好几个.都还是如此,弹出提演示样例如以下: 尝试读取或写入受保护的内存.这通常仅仅是其它内存已损坏.(Sys ...
- Codeforces Round #253 (Div. 1) A Borya and Hanabi
A. Borya and Hanabi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 到底该不该使用存储过程 MySQL查询性能优化一则
到底该不该使用存储过程 看到<阿里巴巴java编码规范>有这样一条 关于这条规范,我说说我个人的看法 用不用存储过程要视所使用的数据库和业务场景而定的,不能因为阿里巴巴的技术牛逼,就视 ...
- 一起talk C栗子吧(第一百二十四回:C语言实例--内置宏)
各位看官们,大家好,上一回中咱们说的是显示变量和函数地址的样例,这一回咱们说的样例是:内置宏.闲话休提,言归正转.让我们一起talk C栗子吧! 看官们,我们在编译程序的时候,假设有语法错误,编译器就 ...
- WWDC笔记:2011 Session 125 UITableView Changes, Tips and Tricks
What’s New Automatic Dimensions - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSect ...
- sql 统计 url字符串处理
SELECT SUBSTRING_INDEX(url,'/',1) AS wed_domain,COUNT(1),SUM(no_open_times),SUM(no_ad_times),SUM(ok_ ...
- Linux设备驱动--块设备(二)之相关结构体
上回最后面介绍了相关数据结构,下面再详细介绍 块设备对象结构 block_device 内核用结构block_device实例代表一个块设备对象,如:整个硬盘或特定分区.如果该结构代表一个分区,则其成 ...