题意:给定 n 个物品,然后有 m 个人买东西,他们有 x 元钱,然后从 l - r 这个区间内买东西,对于每个物品都尽可能多的买,问你最少剩下多少钱。

析:对于物品,尽可能多的买的意思就是对这个物品价格取模,但是对于价格比我的钱还多,那么就没有意义,对取模比我的钱少的,那取模至少减少一半,所以最多只要60多次就可以结束,为了快速找到第一个比我的钱少的,使用线段树。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 50;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} LL minv[maxn<<2];
LL a[maxn]; void push_up(int rt){ minv[rt] = min(minv[rt<<1], minv[rt<<1|1]); } void build(int l, int r, int rt){
if(l == r){ scanf("%I64d", minv + rt); a[l] = minv[rt]; return ; }
int m = l + r >> 1;
build(lson);
build(rson);
pu(rt);
} int query(int L, int R, LL val, int l, int r, int rt){
if(minv[rt] > val) return -1;
if(l == r) return minv[rt] <= val ? l : -1;
int m = l + r >> 1;
int ans = -1;
if(L <= m){
if(minv[rt<<1] <= val) ans = query(L, R, val, lson);
}
if(R > m && ans == -1){
if(minv[rt<<1|1] <= val) ans = query(L, R, val, rson);
}
return ans;
} int main(){
scanf("%d %d", &n, &m);
build(all);
while(m--){
LL money;
int l, r;
scanf("%I64d %d %d", &money, &l, &r);
while (money && l <= r){
int cur = query(l, r, money, all);
if (cur == -1) break;
money %= a[cur];
l = cur + 1;
}
printf("%I64d\n", money);
}
return 0;
}

  

Gym 101201J Shopping (线段树+取模)的更多相关文章

  1. 【线段树】Gym - 101201J - Shopping

    题意:n个数,m次询问,每次给你一个询问v,l,r,问你v%a[l]%a[l+1]%...%a[r]是多少. a%b,结果要么不变,要么至少缩小到a的一半,于是用线段树,每次询问当前区间最靠左侧的小于 ...

  2. hdu-5475 An easy problem---线段树+取模

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5475 题目大意: 给X赋初值1,然后给Q个操作,每个操作对应一个整数M: 如果操作是1则将X乘以对应 ...

  3. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  4. Tunnel Warfare(线段树取连续区间)

    emmmmmmmm我菜爆了 思路来自:https://blog.csdn.net/chudongfang2015/article/details/52133243 线段树最难的应该就是要维护什么东西 ...

  5. Hacker Cups and Balls Gym - 101234A 二分+线段树

    题目:题目链接 题意:有编号从1到n的n个球和n个杯子. 每一个杯子里有一个球, 进行m次排序操作,每次操作给出l,r. 如果l<r,将[l,r]范围内的球按升序排序, 否则降序排, 问中间位置 ...

  6. Gym - 101982F 扫描线+线段树

    题目链接:https://codeforces.com/gym/101982/attachments 要你求覆盖奇数次的矩形面积并,每次更新时减去原先的值即可实现奇数次有效,下推时为保证线段长度不变左 ...

  7. D - Lis on Circle Gym - 102441D (LIS + 线段树)

    There are nn people at the round gaming table. Each of them has a set of cards. Every card contains ...

  8. 2019.03.09 codeforces620E. New Year Tree(线段树+状态压缩)

    传送门 题意:给一棵带颜色的树,可以给子树染色或者问子树里有几种不同的颜色,颜色值不超过606060. 思路:颜色值很小,因此状压一个区间里的颜色用线段树取并集即可. 代码: #include< ...

  9. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模

    D. The Child and Sequence   At the children's day, the child came to Picks's house, and messed his h ...

随机推荐

  1. zedgraph控件怎么取得鼠标位置的坐标值(转帖)

    我想取得zedgraph控件上任意鼠标位置的坐标值,IsShowCursorValues可以显示鼠标位置的值但是不能提取赋值给其他的变量.用PointValueEvent这个事件又只能得到已经画出的点 ...

  2. vue2.0实现一个模态弹框,内容自定义(使用slot)

    定义模态框:合理使用插槽 model.vue <!-- 模态弹窗 --> <template> <div class="self-modal" v-s ...

  3. An Introduction to Greta

    I was surprised by greta. I had assumed that the tensorflow and reticulate packages would eventually ...

  4. 关于 ImageLoader 说的够细了。。。

    简介ImageLoader(一) 分类: android 开源及第三方项目2014-05-30 12:14 14126人阅读 评论(0) 收藏 举报 ImageLoader 使用该开源项目的之前,先给 ...

  5. Centos生成SSL证书的步骤

    1.yum install openssl安装openssl组件2.生成KEY的流程步骤如下 1. 创建根证书密钥文件(自己做CA)root.key: openssl genrsa -out root ...

  6. Selenium Webdriver——使用reportng

    ReportNG is a simple HTML reporting plug-in for the TestNG unit-testing framework. It is intended as ...

  7. FireMoneky 画图 Point 赋值

    VCL 的 Canvas.Pen 对应FMX: Canvas.Stroke;VCL到 Canvas.Brush 对应FMX: Canvas.Fill. TCircle 圆形控件 Inkscape 0. ...

  8. PL/SQL Developer Initialization erro

    PL/SQL Developer---------------------------Initialization errorSQL*Net not properly installed Oracle ...

  9. Python Geoip 获取IP地址经度、纬度

    简介: 除了一些免费的 API 接口,例如 http://ipinfo.io/223.155.166.172 可以得到一些信息外,还可以通过 python-geoip 库来解决这个问题. shell ...

  10. Html生成控件

    HtmlHelper:获取或设置 System.Web.Mvc.HtmlHelper 对象,该对象用于呈现 HTML 元素. 以下是Html的写法与生成的结果的对比 @Html.Label(" ...