标签:class ble get 一个 std tle 暴力 div 函数
这题面。。
我也是一中的。。但这个一中是高中。。人家初中就开始学线段树了我是不是有点弱。。
#include<stdio.h>
#include<iostream>
using namespace std;
const int MaxN(20004);
struct Node
{
int L,R,Mid;
int Lf,Rf,len;
}Tree[MaxN*4];
inline void pushup(int rt)
{
Tree[rt].L=Tree[rt<<1].L;
Tree[rt].R=Tree[rt<<1|1].R;
Tree[rt].Lf=Tree[rt<<1].Lf;
Tree[rt].Rf=Tree[rt<<1|1].Rf;
Tree[rt].Mid=max(Tree[rt<<1].R,Tree[rt<<1].Mid);
Tree[rt].Mid=max(Tree[rt].Mid,Tree[rt<<1|1].L);
Tree[rt].Mid=max(Tree[rt].Mid,Tree[rt<<1|1].Mid);
if(Tree[rt<<1].Rf!=Tree[rt<<1|1].Lf)
{
Tree[rt].Mid=max(Tree[rt<<1].R+Tree[rt<<1|1].L,Tree[rt].Mid);
if(Tree[rt<<1].L==Tree[rt<<1].len)
Tree[rt].L+=Tree[rt<<1|1].L;
if(Tree[rt<<1|1].R==Tree[rt<<1|1].len)
Tree[rt].R+=Tree[rt<<1].R;
}
return;
}
void Build(int rt,int l,int r)
{
Tree[rt].len=r-l+1;
if(l==r)
{
Tree[rt].L=Tree[rt].R=Tree[rt].Mid=1;
Tree[rt].Lf=Tree[rt].Rf=0;
return;
}
int m=(l+r)>>1;
Build(rt<<1,l,m);
Build(rt<<1|1,m+1,r);
pushup(rt);
return;
}
void Update(int rt,int l,int r,int k)
{
if(l==r)
{
Tree[rt].Lf=Tree[rt].Rf=(Tree[rt].Rf+1)%2;
return;
}
int m=(l+r)>>1;
if(k<=m)
Update(rt<<1,l,m,k);
else
Update(rt<<1|1,m+1,r,k);
pushup(rt);
return;
}
int main()
{
int N,M,i,x;
scanf("%d%d",&N,&M);
Build(1,1,N);
for(i=1;i<=M;i++)
{
scanf("%d",&x);
Update(1,1,N,x);
printf("%d\n",Tree[1].Mid);
}
return 0;
}
哦对了再附上暴力代码。。因为数据太水了。。O(n*n)完全没压力。。
#include <cstdio>
bool a[20005];
int n,m,x;
int check() {//计算函数,虽然存在大量重复,但是本人懒得优化(毕竟数据那么水)
int cans=1,ans=1;
for(int i=2;i<=n;++i) { // 最大子段和
if(a[i]!=a[i-1]) cans++;
if(ans<cans) ans=cans;
if(a[i]==a[i-1]) cans=1;
}
return ans;
}
int main() {
scanf("%d %d",&n,&m);
for(int i=1;i<=m;++i) {//暴力模拟不解释
scanf("%d",&x);
a[x]=!a[x]; //改变状态
printf("%d\n",check());//输出
}
return 0;
}
这tm暴力。。又短又好理解。。
标签:class ble get 一个 std tle 暴力 div 函数
原文地址:https://www.cnblogs.com/hahaha2124652975/p/11206588.html