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

来一发ST表

Posted by ibilllee at 2018-07-19 17:15:18 on Problem 3250
我的线段树太丑就T了...
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define RG register ll
#define rep(i,a,b)    for(RG i=a;i<=b;++i)
#define per(i,a,b)    for(RG i=a;i>=b;--i)
#define ll unsigned long long
#define inf (1<<29)
#define maxn  80005
#define ls (pos<<1)
#define rs (pos<<1|1)
#define mid ((t[pos].l+t[pos].r)>>1)
using namespace std;
ll n,ANS;
ll num[maxn],mx[maxn][18];
inline ll read()
{
	ll x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
	return x*f;
}
/*
void build(ll pos,ll l,ll r)
{
	t[pos].l=l,t[pos].r=r;
	if(l==r)	{t[pos].mx=num[l];return;}
	build(ls,l,mid);build(rs,mid+1,r);
	t[pos].mx=max(t[ls].mx,t[rs].mx);
}

ll query(ll pos,ll l,ll r)
{
	if(l<=t[pos].l&&t[pos].r<=r)	return t[pos].mx;
	ll ans=0;
	if(l<=mid)	ans=query(ls,l,r);
	if(r>mid)	ans=max(ans,query(rs,l,r));
	return ans;
}
*/

void ST()
{
	rep(i,1,n) mx[i][0]=num[i];
	rep(j,1,17)
		rep(i,1,n)
			if(i+(1<<j)-1<=n)
				mx[i][j]=max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
}

int query(int l,int r)
{
	int len=log(r-l+1)/log(2);
	return max(mx[l][len],mx[r-(1<<len)+1][len]);
}

int main()
{
	n=read();
	rep(i,1,n)	num[i]=read();
	//build(1,1,n);
	ST();
	rep(i,1,n)
	{
		RG l=i+1,r=n,ans=i,md;
		while(l<=r)
		{
			md=(l+r)>>1;
			if(query(i+1,md)<num[i])	ans=md,l=md+1;
			else						r=md-1;
		}
		ANS+=ans-i;
	}
	cout<<ANS;
	return 0;
}

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