夏夜、

心若平似镜、何题不AC。

UVA 12299 RMQ with Shifts 线段树

简单的单点更新

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb(a) push_back(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("d:\\in.txt","r",stdin);
    freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

const int maxn=100100;
int minv[maxn<<2];
int da[maxn];
int build(int idx,int l,int r)
{
    if(l==r)
    {
        minv[idx]=da[l];
        return 0;
    }
    int mid=(r+l)>>1;
    build(lson);
    build(rson);
    minv[idx]=min(minv[idx<<1],minv[idx<<1|1]);
    return 0;
}
int query(int idx,int l,int r,int tl,int tr)
{
    if(tl<=l&&tr>=r)
    {
        return minv[idx];
    }
    int mid=(r+l)>>1;
    int x=INT_MAX;
    if(tl<=mid)x=min(x,query(lson,tl,tr));
    if(tr>mid) x=min(x,query(rson,tl,tr));
    return x;
}
int update(int idx,int l,int r,int x,int v)
{
    if(l==r)
    {
        minv[idx]=v;
        return 0;
    }
    int mid=(r+l)>>1;
    if(x<=mid)update(lson,x,v);
    else update(rson,x,v);
    minv[idx]=min(minv[idx<<1],minv[idx<<1|1]);
    return 0;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&da[i]);
        build(1,1,n);
        for(int q=1;q<=m;q++)
        {
            char s[50];
            scanf("%s",s);
            if(s[0]=='q')
            {
                int a,b;
                sscanf(s+6,"%d,%d",&a,&b);
                printf("%d\n",query(1,1,n,a,b));
            }else
            {
                int ans[50],k=0;
                for(int i=6;s[i]!='\n';)
                {
                    int x=0;
                    int j=i;
                    while(s[j]!=','&&s[j]!=')')
                    {
                        x=x*10+s[j]-'0';
                        j++;
                    }
                    ans[++k]=x;
                    if(s[j]==')')break;
                    else i=j+1;
                }
                int v=query(1,1,n,ans[1],ans[1]);
                for(int i=1;i<k;i++)
                {
                    int temp=query(1,1,n,ans[i+1],ans[i+1]);
                    update(1,1,n,ans[i],temp);
                }
                update(1,1,n,ans[k],v);
            }
        }
    }
    return 0;
}
View Code

posted on 2013-09-09 15:42  BMan、  阅读(189)  评论(0编辑  收藏  举报

导航