#include<cstdio>
#include<vector>
using namespace std;
struct Node{
int s,e;long long v;int left,right;
};
vector<Node>Tree;
long long INF=-1e18,x,y,A,X[100005],E[100005];
int T[100005],i,B[100005],m,Y[100005],k,D,N;
void update(int num,int pos,long long val)
{
Node t=Tree[num];
if(t.v<val)
{
Tree[num].v=val;
}
long long h=(t.s+t.e-1)/2;
if(t.s==t.e-1)
{
return ;
}
if(h+1>pos)
{
if(t.left==-1)
{
Tree.push_back({t.s,h+1,INF,-1,-1});
Tree[num].left=Tree.size()-1;
}
update(Tree[num].left,pos,val);
}
else
{
if(t.right==-1)
{
Tree.push_back({h+1,t.e,INF,-1,-1});
Tree[num].right=Tree.size()-1;
}
update(Tree[num].right,pos,val);
}
}
long long rmq(int num,int p,int q)
{
Node t=Tree[num];
if(t.e<p||q<t.s)return INF;
if(t.s>=p&&t.e<=q)return t.v;
long long res=INF;
if(t.left>=0)res=max(res,rmq(t.left,p,q));
if(t.right>=0)res=max(res,rmq(t.right,p,q));
return res;
}
main()
{
scanf("%d%d%d%lld%d",&k,&m,&D,&A,&N);
for(i=1;i<=N;i++)
{
scanf("%d%d",&T[i],&B[i]);
T[i]-=k;
X[i]=T[i]/D;
Y[i]=T[i]%D;
}
T[N+1]=m-k;
X[N+1]=T[N+1]/D;
Y[N+1]=T[N+1]%D;
B[0]=B[N+1]=0;
Tree.push_back({0,D,INF,-1,-1});
E[0]=0;
update(0,Y[0],X[0]*A);
for(i=1;i<=N+1;i++)
{
x=rmq(0,0,Y[i]);
y=rmq(0,Y[i],D);
E[i]=max(x-A,y)-X[i]*A+B[i];
update(0,Y[i],E[i]+X[i]*A);
}
printf("%lld",E[N+1]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4528 KB |
Output is correct |
2 |
Correct |
0 ms |
5104 KB |
Output is correct |
3 |
Correct |
2 ms |
4528 KB |
Output is correct |
4 |
Correct |
0 ms |
4332 KB |
Output is correct |
5 |
Correct |
0 ms |
3948 KB |
Output is correct |
6 |
Correct |
0 ms |
3948 KB |
Output is correct |
7 |
Correct |
0 ms |
3948 KB |
Output is correct |
8 |
Correct |
0 ms |
3948 KB |
Output is correct |
9 |
Correct |
1 ms |
5104 KB |
Output is correct |
10 |
Correct |
0 ms |
4528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
3948 KB |
Output is correct |
2 |
Correct |
73 ms |
3948 KB |
Output is correct |
3 |
Correct |
14 ms |
3948 KB |
Output is correct |
4 |
Correct |
71 ms |
3948 KB |
Output is correct |
5 |
Correct |
27 ms |
3948 KB |
Output is correct |
6 |
Correct |
25 ms |
3948 KB |
Output is correct |
7 |
Correct |
48 ms |
3948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
13168 KB |
Output is correct |
2 |
Correct |
152 ms |
22384 KB |
Output is correct |
3 |
Correct |
153 ms |
40816 KB |
Output is correct |
4 |
Correct |
155 ms |
77680 KB |
Output is correct |
5 |
Correct |
99 ms |
13168 KB |
Output is correct |
6 |
Correct |
71 ms |
13168 KB |
Output is correct |