A.闪烁的繁星

题目:https://vijos.org/p/1881

题解:貌似做过小白逛公园或者序列操作都可以秒出吧,就是pushup函数比较麻烦,不过仔细想一想就知道了。

代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define maxn 250000+1510
13 #define maxm 500+100
14 #define eps 1e-10
15 #define ll long long
16 #define pa pair<int,int>
17 #define for0(i,n) for(int i=0;i<=(n);i++)
18 #define for1(i,n) for(int i=1;i<=(n);i++)
19 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
20 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
21 #define mod 1000000007
22 using namespace std;
23 inline int read()
24 {
25     int x=0,f=1;char ch=getchar();
26     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
27     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
28     return x*f;
29 }
30 int n,m;
31 struct seg{int l,r,ls,rs,lx,rx,mx;}t[4*maxn];
32 void build(int k,int l,int r)
33 {
34     t[k].l=l;t[k].r=r;t[k].lx=t[k].rx=t[k].mx=t[k].ls=t[k].rs=1;
35     if(l==r)return;
36     int mid=(l+r)>>1;
37     build(k<<1,l,mid);build(k<<1|1,mid+1,r);
38 }
39 void pushup(int k)
40 {
41     int l=k<<1,r=k<<1|1,mid=(t[k].l+t[k].r)>>1;
42     t[k].ls=t[l].ls;t[k].rs=t[r].rs;
43     t[k].lx=t[l].lx;
44     t[k].rx=t[r].rx;
45     t[k].mx=max(t[l].mx,t[r].mx);
46     if(t[l].rs!=t[r].ls)
47      {
48       t[k].mx=max(t[k].mx,t[l].rx+t[r].lx);
49       if(t[l].mx==mid-t[k].l+1)t[k].lx=max(t[k].lx,t[l].mx+t[r].lx);
50       if(t[r].mx==t[k].r-mid)t[k].rx=max(t[k].rx,t[r].mx+t[l].rx);
51      }
52     //cout<<t[k].l<<' '<<t[k].r<<' '<<t[k].ls<<' '<<t[k].rs<<' '<<t[k].lx<<' '<<t[k].rx<<' '<<t[k].mx<<endl; 
53 }
54 void change(int k,int x)
55 {
56     int l=t[k].l,r=t[k].r,mid=(l+r)>>1;
57     if(l==r){t[k].rs=1-t[k].rs;t[k].ls=t[k].rs;return;}
58     if(x<=mid)change(k<<1,x);else change(k<<1|1,x);
59     pushup(k);
60 }
61 int main()
62 {
63     freopen("input.txt","r",stdin);
64     freopen("output.txt","w",stdout);
65     n=read();m=read();
66     build(1,1,n);
67     while(m--)change(1,read()),printf("%d\n",t[1].mx);
68     return 0;
69 }
View Code

相关文章:

  • 2022-12-23
  • 2020-10-03
  • 2021-09-23
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
  • 2021-11-09
猜你喜欢
  • 2021-12-13
  • 2021-10-05
  • 2021-10-14
  • 2021-07-05
  • 2021-06-07
  • 2021-10-10
  • 2022-12-23
相关资源
相似解决方案