• 线段树模板1(例题BZOJ1012)单点修改+区间查询


    ---恢复内容开始---

    之所以这么迟才发是因为,这道题调了好久好久,结果发现是输入错误了。。。。。

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012

    好啦,模板的话还是直接看代码吧。区间修改模板还没有写。。。

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<string>
    using namespace std;
    int minx=-2147283646;
    int a,f[1000010],tt,n1,m,d;
    char c[10];
    
    //建立线段树(这道题写的是后几位的最大值,不是区间和)
    void build(int root,int l,int r)
    {
        int mid;
        f[root]=minx;
        if (l==r) return ;
        mid=(l+r)/2;
        build(root*2,l,mid);
        build(root*2+1,mid+1,r);
        f[root]=max(f[root*2],f[root*2+1]);
    }
    
    //区间最值查询
    int query(int root,int x,int y,int nowl,int nowr)
    {
        int mid,sum;
        if (nowl>=x&&nowr<=y) return f[root];
        sum=minx;
        mid=(nowl+nowr)/2;
        if (x<=mid) sum=max(query(2*root,x,y,nowl,mid),sum);
        if (y>mid) sum=max(query(2*root+1,x,y,mid+1,nowr),sum);
        return sum;
    }
    
    //单点修改
    void update(int root,int nowl,int nowr,int x,int val)
    {
        int mid;
        if (nowl==nowr)
       {
            f[root]=val;
            return;
       }    
       mid=(nowl+nowr)/2;
       if (x<=mid) update(root*2,nowl,mid,x,val);
       else  update(root*2+1,mid+1,nowr,x,val);
       f[root]=max(f[root*2],f[root*2+1]);
    }
    int main()
    {
        //freopen("lydsy1012in.txt","r",stdin);
        //freopen("lydsy1012out.txt","w",stdout);
        scanf("%d%d
    ",&m,&d);
        build(1,1,m);
        n1=0;
        tt=0; 
        for (int i=1;i<=m;i++)
        {
            scanf("%s%d",&c,&a);
            if (c[0]=='A') 
            {
                n1++;
                update(1,1,m,n1,(a+tt)%d);
            }
            else
            {
                tt=query(1,n1-a+1,n1,1,m);
                printf("%d
    ",tt);
            }
        }
        return 0;
    }
  • 相关阅读:
    Oracle的列操作(增加列,修改列,删除列),包括操作多列
    txt文本怎么去除重复项
    Javascript去掉字符串前后空格
    Js作用域链及变量作用域
    js变量以及其作用域详解
    如何在sqlserver 的函数或存储过程中抛出异常。
    存储过程如何处理异常
    设置VS2010和IE8 调试ATL控件<转>
    YUV图像合成原理<转>
    VS2013 查看程序各个函数的CPU利用率<转>
  • 原文地址:https://www.cnblogs.com/2014nhc/p/6364718.html
Copyright © 2020-2023  润新知