codeforces535C:Tavas and Karafs(二分)

来源:互联网 发布:java底层书籍 编辑:程序博客网 时间:2024/06/11 18:32

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 ≤ rand 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 ≤ 1061 ≤ 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.

Sample test(s)
input
2 1 41 5 33 3 107 10 26 4 8
output
4-18-1
input
1 5 21 5 102 7 4
output
12
英语渣渣看题都看半天
首先给出a,b,n
这是一个由a开头,b为公差,长度无限的等差数列
然后n个询问
输入l,t,m
取这个数列从l开始,m个数,每一次这个数列中所有的数字-1
当首尾变成0的时候,可以向后移,问最后t次之后,最长的0序列的右边界是多少
那么我们首先可以确定,对于这个最终的序列而言,假设右边界为r
那么max(h1,h2,...hr)<=t && h1+h2+...hr<=t*m
对于这两个条件,我们进行二分即可
#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <algorithm>using namespace std;#define ls 2*i#define rs 2*i+1#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,x) memset(a,x,sizeof(a))#define w(a) while(a)#define LL long longconst double pi = acos(-1.0);#define Len 63#define mod 19999997const int INF = 0x3f3f3f3f;LL a,b,n;LL l,t,m;LL cal(LL x){    return a+(x-1)*b;}LL getsum(LL r){    return (cal(r)+cal(l))*(r-l+1)/2;}int main(){    LL i,j,k,maxn;    w(~scanf("%I64d%I64d%I64d",&a,&b,&n))    {        w(n--)        {            scanf("%I64d%I64d%I64d",&l,&t,&m);            if(cal(l)>t)            {                printf("-1\n");                continue;            }            LL ll  = l,lr = (t-a)/b+1,mid;            w(ll<=lr)            {                LL mid = (ll+lr)/2;                if(getsum(mid)<=t*m) ll = mid+1;                else lr = mid-1;            }            printf("%d\n",ll-1);        }    }    return 0;}


0 0