zoukankan      html  css  js  c++  java
  • 线段树模板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;
    }
  • 相关阅读:
    Django框架----路由系统(详细)
    Django框架----路由系统、视图和模板(简单介绍)
    Django框架----模板继承和静态文件配置
    Django框架----模板语法
    毕昇杯之无线通信模块
    新版本ubuntu13.10软件安装
    毕昇杯总结1
    调试程序的方法总结
    新年,新气象
    基于51,人体红外感应和RC522的门禁系统
  • 原文地址:https://www.cnblogs.com/2014nhc/p/6364718.html
Copyright © 2011-2022 走看看