# JZOJ5597.红绿灯

## Solution:

30%数据:

50%数据:

100%数据:

(x+g+rxmod(g+r)+di)mod(g+r)

dimod(g+r)

(SumjSumi)mod(g+r)[g,g+r)

## Code:

50%数据:

# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
ll d[N],ans[N];
ll r,g;
int n,q;
int main()
{
freopen("light.in","r",stdin);
freopen("light.out","w",stdout);
scanf("%d%lld%lld",&n,&g,&r);
for (int i = 1 ; i <= n + 1 ; ++i) scanf("%lld",d + i);
scanf("%d",&q);
if (g <= 100 && r <= 100)
{
for (int i = 0 ; i < g + r ; ++i)
{
ans[i] = i;
for (int j = 1 ; j <= n ; ++j)
{
ans[i] += d[j];
if (ans[i] % (g + r) >= g) ans[i] += (g + r) - ans[i] % (g + r);
}
ans[i] += d[n + 1] - i;
}
while (q--)
{
ll x; scanf("%lld",&x);
printf("%lld\n",x + ans[x % (g + r)]);
}
}else
{
while (q--)
{
ll x; scanf("%lld",&x);
for (int i = 1 ; i <= n ; ++i)
{
x += d[i];
if (x % (g + r) >= g) x += (g + r) - x % (g + r);
}
printf("%lld\n",x + d[n + 1]);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}

100%数据:

# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;
typedef long long ll;
const int N = 5e4 + 5;
const int inf = 0x3f3f3f3f;
int ls[N << 1],rs[N << 1],val[N << 1];
ll d[N],f[N],sum[N],b[N],c[N];
ll g,r;
int n,q,rt,cnt,m;
inline void ins(int &x,int l,int r,int pos,int w)
{
if (!x) x = ++cnt;
if (l == r) { val[x] = min(val[x],w); return; }
int mid = (l + r) >> 1;
if (pos <= mid) ins(ls[x],l,mid,pos,w);
else ins(rs[x],mid + 1,r,pos,w);
val[x] = min(val[ls[x]],val[rs[x]]);
}
inline int qry(int x,int l,int r,int l1,int r1)
{
if (!x || l1 > r1) return inf;
if (l >= l1 && r <= r1) return val[x];
ll mid = (l + r) >> 1; int ret = inf;
if (l1 <= mid) ret = qry(ls[x],l,mid,l1,r1);
if (r1 > mid) ret = min(ret,qry(rs[x],mid + 1,r,l1,r1));
return ret;
}
int main()
{
freopen("light.in","r",stdin);
freopen("light.out","w",stdout);
memset(val,0x3f,sizeof(val));
scanf("%d%lld%lld",&n,&g,&r);
for (int i = 1 ; i <= n + 1 ; ++i) scanf("%lld",d + i);
for (int i = 1 ; i <= n + 1 ; ++i) sum[i] = sum[i - 1] + d[i],c[i] = b[i] = sum[i] % (g + r);
c[n + 2] = 0,c[n + 3] = g + r;
sort(c + 1,c + n + 4);
m = unique(c + 1,c + n + 4) - c - 1;
for (int i = n ; i >= 1 ; --i)
{
int p;
ll lc = (b[i] + g) % (g + r),rc = (g + r - 1 + b[i]) % (g + r);
int l1 = lower_bound(c + 1,c + m + 1,lc) - c;
int r1 = upper_bound(c + 1,c + m + 1,rc) - c - 1;
if (lc <= rc) p = qry(rt,1,m,l1,r1);
else p = min(qry(rt,1,m,l1,m),qry(rt,1,m,1,r1));
if (p < inf) f[i] = sum[p] - sum[i] + g + r - (sum[p] - sum[i]) % (g + r) + f[p];
else f[i] = sum[n + 1] - sum[i];
ins(rt,1,m,lower_bound(c + 1,c + m + 1,b[i]) - c,i);
}
scanf("%d",&q);
while (q--)
{
ll t,y; int p;
scanf("%lld",&t); y = (g + r - t % (g + r)) % (g + r);
ll lc = (y + g) % (g + r),rc = (g + r - 1 + y) % (g + r);
int l1 = lower_bound(c + 1,c + m + 1,lc) - c;
int r1 = upper_bound(c + 1,c + m + 1,rc) - c - 1;
if (lc <= rc) p = qry(rt,1,m,l1,r1);
else p = min(qry(rt,1,m,l1,m),qry(rt,1,m,1,r1));
if (p < inf) printf("%lld\n",sum[p] + f[p] + g + r - (sum[p] + t) % (g + r) + t);
else printf("%lld\n",sum[n + 1] + t);
}
fclose(stdin);
fclose(stdout);
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120