Tavas and Karafs
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)
input
2 1 4
1 5 3
3 3 10
7 10 2
6 4 8
output
4
-1
8
-1
input
1 5 2
1 5 10
2 7 4
output
1
2

今天才发现比赛时候的算法是对的。。。只不过数据爆了,改成long long就过了,还重新找题解写了一次。。。。

不过新写的更快一点,130ms,二分查找,下界为L,二分查上界,上界的初始值可以算出来。

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <ctime>
using namespace std; long long bsearch(long long,long long,long long); long long A,B;
int main(void)
{
long long l,t,m,r;
long long n; scanf("%lld%lld%lld",&A,&B,&n);
while(n --)
{
scanf("%lld%lld%lld",&l,&t,&m);
if(t < A + (l - ) * B)
{
puts("-1");
continue;
}
r = bsearch(l,t,m);
printf("%lld\n",r);
} return ;
} long long bsearch(long long l,long long t,long long m)
{
long long low = l;
long long high = (t - A) / B + ; while(low <= high)
{
long long mid = (low + high) / ;
long long box = (A + (l - ) * B + A + (mid - ) * B) * (mid - l + ) / ; if(box <= m * t)
low = mid + ;
else
high = mid - ;
} return low - ;
}

CF Tavas and Karafs (二分)的更多相关文章

  1. C. Tavas and Karafs 二分查找+贪心

    C. Tavas and Karafs #include <iostream> #include <cstdio> #include <cstring> #incl ...

  2. Tavas and Karafs 二分+结论

    二分比较容易想到 #include<map> #include<set> #include<cmath> #include<queue> #includ ...

  3. CodeForces 535C Tavas and Karafs —— 二分

    题意:给出一个无限长度的等差数列(递增),每次可以让从l开始的m个减少1,如果某个位置已经是0了,那么可以顺延到下一位减少1,这样的操作最多t次,问t次操作以后从l开始的最长0序列的最大右边界r是多少 ...

  4. CF 535c Tavas and Karafs

    Tavas and Karafs Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  5. Codeforces 535C - Tavas and Karafs

    535C - Tavas and Karafs 思路:对于满足条件的r,max(hl ,hl+1 ,hl+2 ,......,hr )<=t(也就是hr<=t)且∑hi<=t*m.所 ...

  6. Codeforces Round #299 (Div. 1) A. Tavas and Karafs 水题

    Tavas and Karafs Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/536/prob ...

  7. 二分搜索 Codeforces Round #299 (Div. 2) C. Tavas and Karafs

    题目传送门 /* 题意:给定一个数列,求最大的r使得[l,r]的数字能在t次全变为0,每一次可以在m的长度内减1 二分搜索:搜索r,求出sum <= t * m的最大的r 详细解释:http:/ ...

  8. CF 84D Doctor(二分)

    题目链接: 传送门 Doctor time limit per test:1 second     memory limit per test:256 megabytes Description Th ...

  9. ProbS CF matlab源代码(二分系统)(原创作品,转载注明出处,谢谢!)

    %ProbS clear all;%% 数据读入与预处理 data = load('E:\network_papers\u1.base');test = load('E:\network_papers ...

随机推荐

  1. caldera

    Caldera International星期一宣布将公司名称变更为SCO Group,交易代码则改为SCOX,希望SCO可以在客户群当中建立更好的品牌认同. Caldera除了有自己的Linux版本 ...

  2. java 字符串String操作工具类

    /** * StrKit. */ public class StrKit { /** * 首字母变小写 */ public static String firstCharToLowerCase(Str ...

  3. iOS 将图片保存到本地

    //将图片保存到本地 + (void)SaveImageToLocal:(UIImage*)image Keys:(NSString*)key {     NSUserDefaults* prefer ...

  4. POJ 3694 Network (tarjan + LCA)

    题目链接:http://poj.org/problem?id=3694 题意是给你一个无向图n个点,m条边,将m条边连接起来之后形成一个图,有Q个询问,问将u和v连接起来后图中还有多少个桥. 首先用t ...

  5. ECSHOP数据表结构完整仔细说明教程

    From:http://www.ecshop119.com/ecshopjc-868.html s_account_log //用户账目日志表 字段 类型 Null 默认 注释 log_id medi ...

  6. Meta键盘

    由于著名的编辑器Emacs中用到Meta键,但如今大多国人所用键盘上实际并无此键,想必多有不明之处,故多方收集资料撰写此文,简要描述了Meta键及相关键盘的发展始末,至于在Emacs上如何使用国人键盘 ...

  7. quartz 2.2.1 jdbc 连接池参数配置

    /** The JDBC database driver. */指定连接驱动 public static final String DB_DRIVER = "driver"; /* ...

  8. delphi ExecWB

    TWebBrowser.ExecWB 关键点 procedure ExecWB(cmdID: OLECMDID; cmdexecopt: OLECMDEXECOPT); overload; 实现过程 ...

  9. [Angular 2] Value Providers & @Inject

    Dependecies aren’t always objects created by classes or factory functions. Sometimes, all we really ...

  10. Metadata Lock原理4

     http://blog.chinaunix.net/uid-28212952-id-3400571.html    Alibaba  今天发生一个故障,MM复制结构(主备库),备库slave del ...