Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

有木有大佬帮忙看看树状数组做的,为啥wa咧

Posted by 171530118 at 2018-11-17 15:39:54 on Problem 3264
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#define INF 0x3f3f3f
#define MAX 50005
using namespace std;
int lowbit[50005],build_min[50005],build_max[50005],n,a[50005];
void GetLow()
{
    for(int i = 1; i <= n; i++)
        lowbit[i] = i&(-i);
}
void TreeBuild()
{
    for(int i = 1; i <= n; i++)
    {
        build_max[i] = 0;
        build_min[i] = 9999999;
        for(int j = i; j > i-lowbit[i]; j--)
        {
            build_max[i] = max(build_max[i],a[j]);
            build_min[i] = min(build_min[i],a[j]);
        }
    }
}
int query_max(int x,int y)
{
    int ans = a[y];
    while(y != x)
    {
        for(y -= 1; y - lowbit[y] > x; y -= lowbit[y])
            ans = max(ans,build_max[y]);
        ans = max(ans,a[y]);
    }
    return ans;
}
int query_min(int x,int y)
{
    int ans = a[y];
    while(y != x)
    {
        for(y -= 1; y - lowbit[y] > x; y -= lowbit[y])
            ans = min(ans,build_max[y]);
        ans = min(ans,a[y]);
    }
    return ans;
}
int main()
{
    int m;
    while(~scanf("%d%d",&n,&m))
    {
        GetLow();
        for(int i = 1; i <= n; i++)
            scanf("%d",&a[i]);
        TreeBuild();
        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(a == b) cout << 0 << endl;
            else
                cout <<  query_max(a,b) - query_min(a,b) << endl;
        }
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator