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

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

  

以上是关于Gym 101201J Shopping (线段树+取模)的主要内容,如果未能解决你的问题,请参考以下文章

Random Numbers Gym - 101466K dfs序+线段树

Gym 240084E - Correct Bracket Sequence Editor - [线段树]

GYM 101350 F. Monkeying Around(线段树 or 思维)

Gym - 101982F Rectangles (扫描线+线段树)

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

Joy of Handcraft Gym - 102822J(线段树或差分)