传送门

题意

给出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(双指针)的更多相关文章

  1. 三周,用长轮询实现Chat并迁移到Azure测试

    公司的OA从零开始进行开发,继简单的单点登陆.角色与权限.消息中间件之后,轮到在线即时通信的模块需要我独立去完成.这三周除了逛网店见爱*看动漫接兼职,基本上都花在这上面了.简单地说就是用MVC4基于长 ...

  2. 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 ...

  3. Node聊天程序实例03:chat.js

    作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. chat.j ...

  4. 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 ...

  5. IRC(Internet Relay Chat Protocol) Protocal Learning && IRC Bot

    catalogue . Abstract . INTRODUCTION . 通信协议Connection Registration Action . 通信协议Channel operations Ac ...

  6. HDU 5071 Chat(2014鞍山赛区现场赛B题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 解题报告:一个管理聊天窗口的程序,一共有八种操作,然后要注意的就是Top操作只是把编号为u的窗口 ...

  7. ocket.chat 使用 Meteor 开发的实时协作工具,类似 丁丁。

    ocket.chat  使用 Meteor 开发的实时协作工具,类似 丁丁. https://rocket.chat/

  8. 局域网聊天Chat(马士兵视频改进版)

    Github地址: https://github.com/BenDanChen/Chat Chat 小小的聊天系统,主要是跟着网上的马士兵老师的公开视频然后再自己反思有什么地方需要改进的地方,然后大体 ...

  9. 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 ...

随机推荐

  1. IText 生成pdf,处理table cell列跨页缺失的问题

    /**     * 创建(table)PDF,处理cell 跨页处理     * @param savePath(需要保存的pdf路径)     * @param pmbs (数据库查询的数据)    ...

  2. B. Restaurant--cf579B (贪心)

    http://codeforces.com/problemset/problem/597/B 把右节点从小到大排序  在跑一遍就行了 #include <iostream> #includ ...

  3. POJ 1062 【带约束的最短路问题】

    中文题题意不写. 建图: 我自己想到的建图方式是把每个物品看作两个点,编号分别是i和i+n,然后每个物品两个点之间边的权值是物品本身的价值.然后从第i个点往外连边,目标是可替代品顶点编号较小的点,权值 ...

  4. idea2019设置智能提示忽略大小写

    2019的设置和2018的不太一样,话不多说,直接上干货.setting --> Editor --> General --> Code Completion 直接把这个选项前面的勾 ...

  5. 转 常见hash算法的原理

    散列表,它是基于快速存取的角度设计的,也是一种典型的“空间换时间”的做法.顾名思义,该数据结构可以理解为一个线性表,但是其中的元素不是紧密排列的,而是可能存在空隙. 散列表(Hash table,也叫 ...

  6. Spring基于Java的配置

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html: 基于Java的配置选项,可以使你在不用 ...

  7. maven之发布项目到nexus【clean deploy命令】

    原文:http://m.blog.csdn.net/article/details?id=49667971 当我们的项目开发完成以后,可能要进行发布(如果是独立的项目,就不需要发布啦,如果是模块项目, ...

  8. SQLAlchemy的group_by和order_by的区别

    1.官网解释: group_by(*criterion) apply one or more GROUP BY criterion to the query and return the newly ...

  9. 【Nginx】Nginx基础架构

    调用HTTP模块的流程: Worker进程会在一个for循环语句中反复调用事件模块检测网络事件.当事件模块检测到某个客户端发起的TCP请求时(接收到SYN包),将会为它建立TCP连接,成功建立连接后根 ...

  10. gbk转utf-8 iconv 编码转换

    linux以下有时候 字符须要进行编码转换(爬虫将gbk转为utf-8编码...).一般能够选择iconv函数. 终端以下  输入 man 3 iconv 得到  iconv函数的用法. 个人看习惯了 ...