这里注意最后的结果值可能很大,要用long long类型的变量
#include <stdio.h>
long long JudgeLight(long long time,int *t,int r,int y,int g){
int sum=r+y+g;
long long now=(*t+time)%sum;
if(now>=0&&now<r){//RED
*t=r-now;
return 0;
}
else if(now>=r&&now<r+g){//GREEN
return 1;
}
else{//YELLOW
*t=sum-now+r;
return 0;
}
}
int main(){
int r,y,g,n,i,k,t;
long long time=0;
scanf("%d%d%d%d",&r,&y,&g,&n);
for(i=0;i<n;i++){
scanf("%d %d",&k,&t);
if(k==0){
time+=t;
}
else{
if(k==1)
t=r-t;
else if(k==2)
t=y+g+r-t;
else
t=r+g-t;
if(!JudgeLight(time,&t,r,y,g)){
time+=t;
}
}
}
printf("%lld\n",time);
return 0;
}