Gym 101201J Shopping (线段树+取模)

题意:给定 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;
}

  

时间: 2024-10-10 05:17:59

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

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

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5475 题目大意: 给X赋初值1,然后给Q个操作,每个操作对应一个整数M: 如果操作是1则将X乘以对应的M, 如果是2则除以第M次操作对应的M',求每次操作后X的值对给定值取摸的结果. 解题思路: 第一眼看这道题,以为就是水题,直接模拟暴力呀,但是发现这样是错误的,因为这里有除法,对除法取模,就应该是逆元,但是逆元不一定存在 想了之后发现可以用线段树保存每一个要乘以的数字,对于操作一就加入数字即可,

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

题目:题目链接 题意:有编号从1到n的n个球和n个杯子. 每一个杯子里有一个球, 进行m次排序操作,每次操作给出l,r. 如果l<r,将[l,r]范围内的球按升序排序, 否则降序排, 问中间位置的数是多少. 思路: 暴力复杂度为m*nlog(n), 不能暴力排序 二分答案, 对于当前mid, 我们将大于等于mid的数记为1, 否则记0, 则排序就是将某个区间的数改为1或0, 通过线段树区间更新可以方便的做到, 对排序后的结果查询判断二分区间应该取左还是取右, 若中间的数是1, 则说明答案大于等于

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 some number xx. Players make turns consecutively, one after another, starting from the player number 1. A player in his turn can either skip his turn

Codeforces 338E Optimize! 线段树

Optimize! 这个题目代码看了我半天.. 我们把终点关注在b数组, 我们先将b[ i ] 变成 h - b[ i ]并排好序, 对于一个a[ j ]来说如果它能和b[ i ]匹配, 那么它能和b[ k ], k < i, 匹配. 什么情况下能匹配成功呢, 就是b数组中 前 i 个数至少能和 len  - i + 1, 个数匹配,这个随便想想就知道, 或者通过霍尔定理也能很快得到, 那么就能用线段树取维护了. #include<bits/stdc++.h> #define LL lo

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 house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks. Fortunately, Picks remembers how to

K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子树上的所有节点的权值的乘积x,及x的因数个数. 2.SEED:将节点u的权值乘以x. 思路: 比赛时少看了因数不大于13这句话,然后本题难度增加数倍,肝了两个小时都没肝出来,对不起队友啊,今天的组队训练赛实力背锅…… 这题一眼线段树,由于是对一棵子树进行处理,因此我们采用常规套路,借助dfs序将子树

CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935D Description standard input/outputStatements Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers.

Codeforces GYM 100114 D. Selection 线段树维护DP

D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description When selecting files in an application dialog, Vasya noted that he can get the same selection in different ways. A simple mouse click selects a sing

fzu 2136 取糖果(线段树)

题目链接:fzu 2136 2136 取糖果 题目大意:略. 解题思路:线段树区间合并.将袋子按照个数排序,每次将最小的放入线段树,如果当前连续的个数超过区间,那么说 明最小值即为最后加入的袋子糖果个数. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; #defi