题意:给定 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. win7 安装redis服务

    Redis官方是不支持windows的,只是 Microsoft Open Tech group 在 GitHub上开发了一个Win64的版本,项目地址是: https://github.com/MS ...

  2. bzoj1925(SCOI2010)地精部落

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1925 要怎样才能想出正解呢? 当然有一维表示从1到 i . 发现最后是递增的方案数=最后是递 ...

  3. undefined和NAN的区别(转)

    Javascript 中 null.NaN和undefined的区别 1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始 ...

  4. springcloud(九) springboot Actuator + admin 监控

    前一章讲的都是Feign项目(调用方)的监控.接下来讲的是服务提供方的监控 一.springboot actuator + springboot admin Spring Boot Admin 是一个 ...

  5. MYSQL, REDIS 等数据库的介绍

    MySQL: 数据库概述 MySql安装和基本管理 MySQl创建用户和授权 初始mysql语句 库的操作 表的操作 数据类型 数据类型(2) 完整性约束 外键的变种 三种关系 数据的增删改 单表查询 ...

  6. pom指定java编译版本和编码

    方法一 <properties> <!-- 文件拷贝时的编码 --> <project.build.sourceEncoding>UTF-8</project ...

  7. PO BO VO DTO POJO DAO概念及其作用

    J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候,三分钟就被人家满口的专业术语喷晕了,PO VO BO DTO POJO DAO,一大堆的就来了(听过老罗对这种现象的批判的朋 ...

  8. Linux 文件管理命令语法、参数、实例全汇总(一)

    命令:cat   cat 命令用于连接文件并打印到标准输出设备上. 使用权限 所有使用者 语法格式 cat [-AbeEnstTuv] [--help] [--version] fileName 参数 ...

  9. Kafka学习之broker配置(0.8.1版)(转)

    broker.id  默认值:无 每一个broker都有一个唯一的id,这是一个非负整数,这个id就是broker的"名字",这样就允许broker迁移到别的机器而不会影响消费者. ...

  10. 迷你MVVM框架 avalonjs 学习教程4、数据填充

    MVVM是前端的究极解决方案,你们可能用过jQuery,但那个写的代码不易维护:你们可以听过说requirejs与seajs,传说中的模块开发,加载器,但它们的最终目标是打包:你们可能听过unders ...