Codeforces 535C Tavas and Karafs

题目链接:CF - 535C

Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs.

Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.

For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.

Now SaDDas asks you n queries. In each query he gives you numbers lt and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.

Input

The first line of input contains three integers AB and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105).

Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.

Output

For each query, print its answer in a single line.

题目描述:给出一个等差数列,操作严格要求从最左边不为零的连续m个数减去1,最多执行t次后问离最左边最远的位置在哪里。

算法分析:我们可以二分位置,仔细想想这两个条件:max(hl,hl+1,hl+2,,,,hr)<=t && hl+(hl+1)+(hl+2),,,,hr<=m*t

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 typedef long long LL;
10 const LL maxn=1000000+10;
11 
12 LL A,B,n;
13 LL l,t,m;
14 
15 LL getValue(LL u) {return A+(u-1)*B; }
16 LL getSeg(LL u,LL v)
17 {
18     return (getValue(u)+getValue(v))*(v-u+1)/2;
19 }
20 
21 int main()
22 {
23     while (scanf("%I64d%I64d%I64d",&A,&B,&n)!=EOF)
24     {
25         while (n--)
26         {
27             scanf("%I64d%I64d%I64d",&l,&t,&m);
28             LL left=A+(l-1)*B;
29             if (left>t) printf("-1\n");
30             else
31             {
32                 LL L=l,R=(t-A)/B+1;
33                 while (L<=R)
34                 {
35                     LL mid=(L+R)>>1;
36                     if (getSeg(l,mid)<=m*t) L=mid+1;
37                     else R=mid-1;
38                 }
39                 printf("%I64d\n",L-1);
40             }
41         }
42     }
43     return 0;
44 }

 

posted @ 2015-04-16 02:19  huangxf  阅读(622)  评论(0编辑  收藏  举报