【線段樹】忠誠2

描述 Description 
 老管家是一个聪明能干的人。他为财主工作了整整10年,财主为了让自已账目更加清楚。要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意。但是由于一些人的挑拨,财主还是对管家产生了怀疑。于是他决定用一种特别的方法来判断管家的忠诚,他把每次的账目按1,2,3…编号,然后不定时的问管家问题,问题是这样的:在a到b号账中最少的一笔是多少?为了让管家没时间作假他总是一次问多个问题。
   在询问过程中账本的内容可能会被修改
   
   
 输入格式 Input Format 
 输入中第一行有两个数m,n表示有m(m<=100000)笔账,n表示有n个问题,n<=100000。
接下来每行为3个数字,第一个p为数字1或数字2,第二个数为x,第三个数为y
当p=1 则查询x,y区间
当p=2 则改变第x个数为y
   
   
  输出格式 Output Format 
 输出文件中为每个问题的答案。具体查看样例。
   
   
  样例输入 Sample Input  
 
   
   
  样例输出 Sample Output  
 
   
   
  时间限制 Time Limitation 
 各个测试点1s

在前面那道忠誠基礎上加了個修改。

ACcode:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <bitset>

using std::min;

const char fi[] = "tyvj1039.in";
const char fo[] = "tyvj1039.out";
const int maxN = 100010;
const int maxM = 100010;
const int MAX = 0x3fffff00;
const int MIN = -MAX;

struct SegTree {int L, R, lc, rc, Min; };
SegTree tree[maxM << 1];
int t[maxN];
int n, m, tot;

  void init_file()
  {
    freopen(fi, "r", stdin);
    freopen(fo, "w", stdout);
  }
  
  void Build(int L, int R)
  {
    int Now = ++tot;
    tree[Now].L = L;
    tree[Now].R = R;
    if (L == R)
      {tree[Now].Min = t[L]; return; }
    int Mid = (L + R) >> 1;
    if (L < R)
    {
      tree[Now].lc = tot + 1;
      Build(L, Mid);
      tree[Now].rc = tot + 1;
      Build(Mid + 1, R);
    }
    tree[Now].Min = min(tree[tree[Now].lc].Min,
      tree[tree[Now].rc].Min);
  }
  
  void Modify(int Now, int p)
  {
    if (tree[Now].L == tree[Now].R
      && tree[Now].L == p)
      {tree[Now].Min = t[p]; return; }
	//找到目標直接修改。
    int Mid = (tree[Now].L + tree[Now].R) >> 1;
    if (p <= Mid) Modify(tree[Now].lc, p);
	//在左子樹則找左子樹。
    if (p > Mid) Modify(tree[Now].rc, p);
	//在右子樹則找右子樹。
    tree[Now].Min = min(tree[tree[Now].lc].Min,
      tree[tree[Now].rc].Min);
  }
  
  int Query(int Now, int L, int R)
  {
    if (L <= tree[Now].L && R >= tree[Now].R)
      return tree[Now].Min;
    int Mid = (tree[Now].L + tree[Now].R) >> 1;
    if (R <= Mid)
      return Query(tree[Now].lc, L, R);
    if (Mid < L)
      return Query(tree[Now].rc, L, R);
    int Lc = Query(tree[Now].lc, L, R),
      Rc = Query(tree[Now].rc, L, R);
    return min(Lc, Rc);
  }
  
  void work()
  {
    scanf("%d%d", &n, &m);
    for (int i = 1; i < n + 1; ++i)
      scanf("%d", t + i);
    tot = 0;
    Build(1, n);
    for (; m; --m)
    {
      int O, x, y;
      scanf("%d%d%d", &O, &x, &y);
      switch(O)
      {
        case 1 :
          printf("%d\n", Query(1, x, y));
          break;
        case 2 :
          t[x] = y;
          Modify(1, x);
          break;
      }
    }
  }
  
int main()
{
  init_file();
  work();
  exit(0);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值