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

线段树,C++交,G++会T掉

Posted by yu_xuan at 2021-09-05 14:41:36 on Problem 2823
#include<iostream>
using namespace std;
#include<vector>
#include<stdio.h>
#pragma GCC optimize(2)
const int N=1e6+3;
struct tre{int minn,maxn;}t[N<<2];int n;
void pushup(int id)
{
	t[id].minn=min(t[id<<1].minn,t[id<<1|1].minn);
	t[id].maxn=max(t[id<<1].maxn,t[id<<1|1].maxn);
}
void build(int l,int r,int id)
{
	if(l==r)
	{
		scanf("%d",&t[id].minn);t[id].maxn=t[id].minn;return ;
	}
	int mid=(l+r)>>1;
	if(l<=mid)build(l,mid,id<<1);
	if(r>mid)build(mid+1,r,id<<1|1);
	pushup(id);
}
int ans1,ans2;
void query(int x,int y,int l=1,int r=n,int id=1)
{
	if(l>=x&&r<=y)
	{
		ans1=max(ans1,t[id].maxn);ans2=min(ans2,t[id].minn);return ;
	}
	int mid=(l+r)>>1;
	if(x<=mid)query(x,y,l,mid,id<<1);
	if(y>mid)query(x,y,mid+1,r,id<<1|1);
}
int main()
{
	int k;cin>>n>>k;
	build(1,n,1);
	vector<int>a1,a2;
	for(int i=1;i<=n-k+1;i++)
	{
		int j=i+k-1;ans1=-0x3f3f3f3f,ans2=0x3f3f3f3f;
		query(i,j);a1.push_back(ans1);a2.push_back(ans2);
	}
	for(int i=0;i<a2.size();i++)printf("%d%c",a2[i],i==a2.size()-1?'\n':' ');
	for(int i=0;i<a1.size();i++)printf("%d%c",a1[i],i==a1.size()-1?'\n':' ');
}

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