点击打开zoj1961Let's Chat


Time Limit:
1 Second      Memory Limit:65536 KB


ACM (ACMers' Chatting Messenger) is a famous instant messaging software developed by Marjar Technology Company. To attract more users, Edward, the boss of Marjar Company, has recently added a new feature to the software. The
new feature can be described as follows:

If two users, A and B, have been sending messages toeach other on the lastmconsecutive
days
, the "friendship point" between them will be increased by 1 point.

More formally, if user A sent messages to user B on each day between the (i -m
+ 1)-th day and thei-th day (both inclusive), and user B also sent messages to user A on each day between the (i
- m + 1)-th day and thei-th day (also both inclusive), the "friendship point" between A and
B will be increased by 1 at the end of thei-th day.

Given the chatting logs of two users A and B duringn consecutive days, what's the number of the friendship points between them at the end of then-th
day (given that the initial friendship point between them is 0)?

Input

There are multiple test cases. The first line of input contains an integerT (1 ≤T
≤ 10), indicating the number of test cases. For each test case:

The first line contains 4 integers n (1 ≤n ≤ 109),m
(1 ≤mn),x
andy (1 ≤x,y
≤ 100). The meanings ofn andm are described above, whilex
indicates the number of chatting logs about the messages sent by A to B, andy indicates the number of chatting logs about the messages sent by B to A.

For the following x lines, thei-th line contains 2 integersla,i
andra,i (1 ≤la,i
ra,in),
indicating that A sent messages to B on each day between thela,i-th
day and thera,i-th
day (both inclusive).

For the following y lines, thei-th line contains 2 integerslb,i
andrb,i (1 ≤lb,i
rb,in),
indicating that B sent messages to A on each day between thelb,i-th
day and therb,i-th
day (both inclusive).

It is guaranteed that for all 1 ≤ i <x,ra,i
+ 1 <la,i + 1
and for all 1 ≤i <y,rb,i
+ 1 <lb,i + 1.

Output

For each test case, output one line containing one integer, indicating the number of friendship points between A and B at the end of then-th day.

Sample Input

2
10 3 3 2
1 3
5 8
10 10
1 8
10 10
5 3 1 1
1 2
4 5

Sample Output

3
0

Hint

For the first test case, user A and user B send messages to each other on the 1st, 2nd, 3rd, 5th, 6th, 7th, 8th and 10th day. Asm = 3, the friendship
points between them will be increased by 1 at the end of the 3rd, 7th and 8th day. So the answer is 3.


Author: WENG, Caizhi

Source: The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple



本题是:扫描线思想

对于此类题需要注意:

   一:可能有区间合并

   二:可能有区间排序

   三:可能有不合法区间

   四:对于判断交区间,并不需要很多个if语句判断,具体的,

我们设L=max(a.l,b.l),R=min(a.r.b.r),则做差为并期间,当R-L>0时为我们常见的具体的并区间。

幸运的是本题良心题,不需要排序合并,而且数据小,即便O(xy)也能过

//我的原始代码,拒绝不思考就引用
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int n,m,x,y,ans;
int l1[105],r1[105],l2[105],r2[105];
void _in()
{
cin>>n>>m>>x>>y;
for(int i=1;i<=x;i++)
cin>>l1[i]>>r1[i];
for(int i=1;i<=y;i++)
cin>>l2[i]>>r2[i];
}
void _find(int a,int b)
{
int temp,L,R;
L=max(l1[a],l2[b]);
R=min(r1[a],r2[b]);
temp=R-L+1;
if(temp>=m) ans+=temp-m+1;
}
void _solve()
{
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++){
_find(i,j);
}
cout<<ans<<endl;
}
int main()
{
int t,T;
cin>>T;
for(t=1;t<=T;t++){
ans=0;
_in();
_solve();
}
return 0;
}

理解起来应该很简单

引申一下,对于这一类扫描线思想题目。

我们的大概步骤是:

1,排序(假定按左a.l为关键词排序)

2,合并(如区间[1,3],[3,5]合并成[1,5].而[1,3],[2,4]合并成[1,4])

3,按右a.r为关键词向右扫描,则本题的复杂度可以降到O(x+y) (应用师兄的代码http://blog.csdn.net/DongChengRong/article/details/70496302)

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100+20;
int n,m,x,y; struct Node
{
int start,endd;
}A[maxn],B[maxn]; /*int cmp(struct Node s1,struct Node s2)
{
return s1.start<s2.start;
}*/ int main()
{
int test;
scanf("%d",&test);
for(int i=0;i<test;i++)
{
scanf("%d%d%d%d",&n,&m,&x,&y);
for(int j=1;j<=x;j++) scanf("%d%d",&A[j].start,&A[j].endd);
for(int j=1;j<=y;j++) scanf("%d%d",&B[j].start,&B[j].endd);
//sort(A+1,A+1+x,cmp);
//sort(B+1,B+1+y,cmp);
int ans=0,j=1,k=1;
while(k<=y && j<=x)
{
int a,b;
if(B[k].start>A[j].endd) { j++; continue; }
if(B[k].endd<A[j].start) { k++; continue; }
a=max(B[k].start,A[j].start);
b=min(B[k].endd,A[j].endd);
if(a>n || b>n) break;
int date=b-a+1;
int point=date+1-m;
if(point>=1) ans+=point;
if(A[j].endd<B[k].endd) j++;
else if(A[j].endd==B[k].endd) { j++; k++;}
else k++;
}
printf("%d\n",ans);
}
return 0;
}
(当然,本题任然不需要排序)

如有错误,感谢指出

zoj3961(区间问题)的更多相关文章

  1. ASP.NET Core应用针对静态文件请求的处理[2]: 条件请求与区间请求

    通过调用ApplicationBuilder的扩展方法UseStaticFiles注册的StaticFileMiddleware中间件帮助我们处理针对文件的请求.对于StaticFileMiddlew ...

  2. SQL Server 随机数,随机区间,随机抽取数据rand(),floor(),ceiling(),round(),newid()函数等

    在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会取随机整数.那就看下面 ...

  3. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  4. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  5. [LeetCode] Find Right Interval 找右区间

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

  6. [LeetCode] Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  7. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  8. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  9. [LeetCode] Summary Ranges 总结区间

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

随机推荐

  1. 机器学习实战之 第10章 K-Means(K-均值)聚类算法

    第 10 章 K-Means(K-均值)聚类算法 K-Means 算法 聚类是一种无监督的学习, 它将相似的对象归到一个簇中, 将不相似对象归到不同簇中.相似这一概念取决于所选择的相似度计算方法.K- ...

  2. poj 3683 2-sat建图+拓扑排序输出结果

    发现建图的方法各有不同,前面一题连边和这一题连边建图的点就不同,感觉这题的建图方案更好. 题意:给出每个婚礼的2个主持时间,每个婚礼的可能能会冲突,输出方案. 思路:n个婚礼,2*n个点,每组点是对称 ...

  3. poj 1882完全背包变形

    题意:给出一个上限硬币数量s,给出n套硬币价值,求一套硬币能用不大于s数量的硬币组成从1开始连续的区间价值,其中,如果其最大值相同,输出数量小的和价值小的. 思路:很明显的完全背包,纠结后面最大值相同 ...

  4. 201521123083《Java程序设计》第8周学习总结

    1. 本周学习总结 这周因为一些事情耽搁了,几乎没什么打java代码,这几天尽量补过来 2. 书面作业 1.List中指定元素的删除 1.1 实验总结 不贴大段代码了,简要总结一下.切割成数组,主要用 ...

  5. 201521123112《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 在做PTA5.3的时候一段看起来比较复杂的代码: List<En ...

  6. 201521123023《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4 ...

  7. 201521123070 《JAVA程序设计》第3周学习总结

    1. 本章学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. http:/ ...

  8. 解决liblzo2.so缺失

    系统:CentOS5.8 提示错误: error while loading shared libraries: liblzo2.so.2: cannot open shared object fil ...

  9. Eclipse rap 富客户端开发总结(14) :rap 图片、数据缓存处理

    一.概述 在进行了 rap 的基本学习之后,您对 rap 的理解是否进入了更高的一个层次呢,个人觉得,对学习 rap 的人来说,进行 rap 的学习是一个探索的过程,在编程中不断的对其进行理解和分析, ...

  10. Struts2第十二篇【模型驱动】

    什么是模型驱动 在Struts2中模型驱动就是用来封装数据的..完成数据的自动封装. 为什么要使用模型驱动? 我们之前就使用过Sturts2的数据自动封装功能,是用params拦截器完成的-既然有了p ...