当前位置:网站首页>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
边栏推荐
- Introduction to sap pi / PO login and basic functions
- 通过流式数据集成实现数据价值(5)- 流处理
- 代码源每日一题 div1 (701-707)
- Epidemic prevention registration applet
- Using idea to develop Spark Program
- The central control learning infrared remote control module supports network and serial port control
- 杰理之更准确地确定异常地址【篇】
- DBA常用SQL语句(1)— 概况信息
- 《Redis设计与实现》
- Solving Lucas number and combination theorem
猜你喜欢

自定义登录失败处理

Number theory blocking (integer division blocking)
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

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

ABAP implementation publishes restful services for external invocation example

The sap export excel file opens and shows that the file format and extension of "XXX" do not match. The file may be damaged or unsafe. Do not open it unless you trust its source. Do you still want to

C language: expression evaluation (integer promotion, arithmetic conversion...)

Failureforwardurl and failureurl
MapReduce核心和基础Demo

C语言:表达式求值(整型提升、算术转换 ...)
随机推荐
Prefix sum of integral function -- Du Jiao sieve
通过流式数据集成实现数据价值(1)
2022年广东省安全员A证第三批(主要负责人)考试试题及答案
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
Mobius inversion
DBA common SQL statements (1) - overview information
Es aggregation aggregation analysis
一文读懂PlatoFarm新经济模型以及生态进展
通过流式数据集成实现数据价值(5)- 流分析
Function realization of printing page
Sim Api User Guide(6)
杰理之AES能256bit吗【篇】
Computer network security experiment II DNS protocol vulnerability utilization experiment
Juc并发编程06——深入剖析队列同步器AQS源码
SAP ECC connecting SAP pi system configuration
ARM调试(1):两种在keil中实现printf重定向到串口的方法
GCD of p2257 YY (Mobius inversion)
Realize data value through streaming data integration (2)
SQL调优系列文章之—SQL调优简介
0704、ansible----01