题意:给定 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. TortoiseSVN比较工具设置为BeyondCompare 4

    打开TortoiseSVN的Setting,选择左边的Diff Viewer 设置如下: "D:\Program Files\Beyond Compare 4\BComp.exe" ...

  2. 使用anaconda安装tensorflow (windows10环境)

    版权声明:勤学 修德 明辨 笃实 - CSDN周雄伟 https://blog.csdn.net/ebzxw/article/details/80701613 已有环境:python3.7.1 ana ...

  3. <meta name="viewport" content="width=device-width, initial-scale=1.0">的说明

    今天在做适配手机版时,chrome调到手机版,但是还是显示PC端的样式,无法展现出手机端的样式: 开始的时候还以为是chrome版本的问题,最新版本的chrome62.0是有很多变化的,而之前工作中使 ...

  4. mysql-3 数据表的创建、增删改查

    1.创建数据表 通用语法:CREATE TABLE table_name (column_name column_type); CREATE TABLE IF NOT EXISTS `csj_tbl` ...

  5. 高斯白噪声(white Gaussian noise,WGN)

    本文科普一下高斯白噪声(white Gaussian noise,WGN). 百度百科上解释为“高斯白噪声,幅度分布服从高斯分布,功率谱密度服从均匀分布”,听起来有些晦涩难懂,下面结合例子通俗而详细地 ...

  6. 关于rand 与 randn

    rand:0-1均匀分布.均值m=(a+b)/2; 方差=(b-a).^2/12;   randn:0均值,方差1.     只有当rand和randn生成较大的数据时,均值和方差才会成立.比如N&g ...

  7. Readme.MD 例子

    了解一个项目,恐怕首先都是通过其Readme文件了解信息.如果你以为Readme文件都是随便写写的那你就错了.github,oschina git gitcafe的代码托管平台上的项目的Readme. ...

  8. x264改变输出分辨率的算法<转>

    x264改变输出分辨率的算法 在某些应用场景下,x264的输入视频分辨率与接收端输出的视频分辨率不同.例如编码端摄像头采集到的YUV数据为1280x720,而接收端视频显示窗口为640x480.对于这 ...

  9. VLC接收网络串流缓冲时间的计算 (转)

    原帖地址:http://blog.csdn.net/coroutines/article/details/7472743 VLC版本2.0.1 最近研究IP-STB音视频同步问题,发现方案自带的自动S ...

  10. SIEBEL GET最新时提示表异常

    无法GET最新,则将此表GET一次,CHECK OUT下来,再UNDO CHECK IN即可