当前位置:网站首页>CSP certification 202203-2 travel plan (multiple solutions)
CSP certification 202203-2 travel plan (multiple solutions)
2022-04-23 09:58:00 【Don't change your name if you don't take the examination of Zhe】
The solution is a pre knowledge : Difference array
Solution II pre knowledge : Line segment tree
| Question number : | 202203-2 |
| The title of the test question : | Travel plan |
| The time limit : | 1.5s |
| Memory limit : | 512.0MB |
| Problem description : | Problem descriptionRecently, all places on West Aifu island must have a negative certificate of nucleic acid test within a certain time limit . Specific time of arrival , If in t Nucleic acid test at all times , After a period of time, we can get the negative proof of nucleic acid test . Here we assume that waiting for nucleic acid test results requires k Unit time , That is to say t+k Results can be obtained at any time . If a place requires 24 The nucleic acid test results are included in a unit time , Then, based on the above nucleic acid test results , In the first t+k It's time to t+k+23 Always enter the place . Small C List the next... In chronological order n A travel plan , Among them the first i term (1≤i≤n) Can be summed up as : In order to reasonably arrange the time of nucleic acid detection , Try according to small C Travel plan for , Answer the following query :
Such queries share m individual , Respectively q1,q2,⋯,qm; Queries do not affect each other . Input formatThe first line of input contains three positive integers separated by spaces n、m and k, Respectively represent the number of travel plans 、 The number of queries and the time required to wait for nucleic acid test results . Next input n That's ok , Each line contains two positive integers separated by spaces ti、ci, Indicates a travel plan ; Travel plans are given in chronological order , Satisfy 0<t1≤t2≤⋯≤tn≤2×105. Last input m That's ok , Each line contains only one positive integer qi, Represents a query .m Queries are also given in chronological order , Satisfy 0<q1<q2<⋯<qm≤2×105. Output formatThe output, m That's ok , One integer per row , Indicates the answer to the corresponding query . The sample input Data Sample output Data Sample explanationmoment 1 Make a test , Can meet the third 、 Four 、 Six travel plans ; moment 2 Make a test , Can meet the fourth 、 5、 ... and 、 Six travel plans . The subtasks40% The test data meet 0<n,k≤1000、m=1; 70% The test data meet 0<n,m,k≤1000; All test data meet 0<n,m,k≤105. |
The nature of the topic : Each travel plan represents an interval . Each query is to query the current time x stay k After time ( namely x+k) How many intervals are included .
Solution 1 : Difference array ( Best think 、 The least code solution )
The difference array is suitable for multiple interval modifications , Very few interval queries, or modify the interval first and then query the interval , This is not the case of querying while modifying . This problem uses a differential array , Each travel plan is the operation of the effective interval . Finally, prefix and Statistics , Then you can query .
/*
Differential array version , Five minutes AC, It's much better than the line segment tree
*/
#include <cstdio>
#define fi(i,l,r) for(int i=(l);i<=(r);++i)
#define fd(i,r,l) for(int i=(r);i>=(l);--i)
using namespace std;
int n,m,t;
int x,y;
int a[300005];
template<typename T>void Read(T &x)
{
x=0;T k=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') k=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
x*=k;
}
template<typename T>void Print(T x)
{
if(x<0){putchar('-');Print(-x);return;}// This return Very important , Something can go wrong
if(x>9) Print(x/10);
putchar((x%10)^48);
}
int main (void)
{
Read(n);
Read(m);
Read(t);
while(n--)
{
Read(x);
Read(y);
a[x-y<0?1:x-y+1]++;
a[x+1]--;
}
fi(i,1,300004)
a[i]+=a[i-1];
while(m--)
{
Read(x);
Print(a[x+t]);
putchar('\n');
}
return 0;
}
Solution 2 : Line segment tree ( More yards )
Segment tree interval modification and interval query are in log Level , It also meets the requirements . This question only needs to write a single point query , Therefore, it is different from the general segment tree template .
#include <cstdio>
#define fi(i,l,r) for(int i=(l);i<=(r);++i)
#define fd(i,r,l) for(int i=(r);i>=(l);--i)
#define lc (rt<<1)
#define rc ((rt<<1)|1)
#define mid ((R+L)>>1)
using namespace std;
const int N=300005;
int n,m,t;
int x,y;
struct arr{
int add,val;
}str[N<<2];// The line segment tree is usually four times larger
template<typename T>void Read(T &x)
{
x=0;T k=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') k=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
x*=k;
}
template<typename T>void Print(T x)
{
if(x<0){putchar('-');Print(-x);return;}// This return Very important , Something can go wrong
if(x>9) Print(x/10);
putchar((x%10)^48);
}
void pushUp(int rt)
{
str[rt].val=str[lc].val+str[rc].val;
}
void pushDown(int rt,int L,int R)
{
str[lc].val+=str[rt].add*(mid-L+1);
str[rc].val+=str[rt].add*(R-mid);
str[lc].add+=str[rt].add;
str[rc].add+=str[rt].add;
str[rt].add=0;
}
void Upd(int rt,int L,int R,int ul,int ur,int addVal)
{
if(L>ur||R<ul) return;
if(ul<=L&&ur>=R)
{
str[rt].add+=addVal;
str[rt].val+=addVal*(R-L+1);
return;
}
if(str[rt].add!=0) pushDown(rt,L,R);
if(ul<=mid) Upd(lc,L,mid,ul,ur,addVal);
if(ur>mid) Upd(rc,mid+1,R,ul,ur,addVal);
pushUp(rt);
}
int Query(int rt,int L,int R,int x)
{
if(L==x&&R==x) return str[rt].val;
if(str[rt].add!=0) {pushDown(rt,L,R);pushUp(rt);}
if(x<=mid) return Query(lc,L,mid,x);
else return Query(rc,mid+1,R,x);
}
int main (void)
{
Read(n);
Read(m);
Read(t);
while(n--)
{
Read(x);
Read(y);
Upd(1,1,300000,x-y<0?1:x-y+1,x,1);
}
while(m--)
{
Read(x);
Print(Query(1,1,300000,x+t));
putchar(10);
}
return 0;
}
版权声明
本文为[Don't change your name if you don't take the examination of Zhe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230952163555.html
边栏推荐
- 第二章 Oracle Database In-Memory 体系结构(上) (IM-2.1)
- (Extended) bsgs and higher order congruence equation
- The central control learning infrared remote control module supports network and serial port control
- 通过流式数据集成实现数据价值(3)- 实时持续数据收集
- SAP RFC_ CVI_ EI_ INBOUND_ Main BP master data creation example (Demo customer only)
- [Niuke practice match 68] fans of Niuniu (matrix fast power cycle matrix optimization)
- Pyqt5与通信
- 打印页面的功能实现
- Sim Api User Guide(8)
- Sim Api User Guide(4)
猜你喜欢
MapReduce计算流程详解

元宇宙时代的职业规划与执行

Windows安装redis并将redis设置成服务开机自启

lnmp的配置

Career planning and implementation in the era of meta universe
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

工业元宇宙平台规划与建设

How to use SQL statement union to get another column of another table when the content of a column in a table is empty

Sim Api User Guide(5)

Number theory blocking (integer division blocking)
随机推荐
Realizing data value through streaming data integration (4) - streaming data pipeline
Using idea to develop Spark Program
MapReduce计算流程详解
代码源每日一题 div1 (701-707)
101. Symmetric Tree
Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
杰理之通常程序异常情况有哪些?【篇】
Computer network security experiment II DNS protocol vulnerability utilization experiment
通过流式数据集成实现数据价值(3)- 实时持续数据收集
art-template 模板引擎
Yarn资源调度器
[COCI] Vje š TICA (subset DP)
[educational codeforces round 80] problem solving Report
防疫登记小程序
0704、ansible----01
Go language practice mode - functional options pattern
工业元宇宙平台规划与建设
DBA common SQL statements (1) - overview information
杰理之用户如何最简单的处理事件【篇】
Pyqt5 and communication