传送门

题意

给出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;
}

最新文章

  1. 在线OJ实用技巧(转载)
  2. java selenium (八) Selenium IDE 用法
  3. 33Mybatis------Mapper的编写
  4. Mesh系列文章 - 自定义Mesh
  5. selenium webdriver
  6. 使用定时器实现JavaScript的延期执行或重复执行
  7. BroadcastReceiver应用详解
  8. 教了几天C语言 C语言竞赛------家长们你们为什么这么急!!
  9. 转:Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择,及详解五种运行模式。
  10. Python下载漫画
  11. VC实用小知识总结 (一),转http://blog.csdn.net/myiszjf/article/details/10007431
  12. php生成json和js解析json
  13. 关于模型的合法性,Entity.IsValid()合理吗?
  14. python抓取NBA现役球员基本信息数据
  15. 下载visual studio 的离线包
  16. c++ --&gt; const关键字总结
  17. post 和 get 的区别,直指本质
  18. Chrome_高亮显示当前改变的区域
  19. 知识点总结——STL相关(持续补充)
  20. Nginx 反向代理如何连接上游服务器

热门文章

  1. java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
  2. 通过k8s(Kubernetes)搭建jmeter的压测环境master-slave架构,实现弹性伸缩
  3. Openwrt 安装软件到U盘或硬盘
  4. [React] {svg, css module, sass} support in Create React App 2.0
  5. spring 学习资料
  6. ZOJ 3316 Game 一般图最大匹配带花树
  7. 【机器学习具体解释】SVM解二分类,多分类,及后验概率输出
  8. 从PRISM开始学WPF(一)WPF?
  9. 通过通过url routing解决UIViewController跳转依赖
  10. udhcp详解源码(序)