QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#757732 | #7743. Grand Finale | jr_zlw | TL | 0ms | 0kb | C++17 | 2.5kb | 2024-11-17 12:57:28 | 2024-11-17 12:57:29 |
answer
#include<bits/stdc++.h>
#define rep(a,b,c) for(int c(a);c<=(b);++c)
#define drep(a,b,c) for(int c(a);c>=(b);--c)
using namespace std;
inline int read()
{
int res=0;char ch=getchar();bool f=0;while(ch<'0'||ch>'9')f^=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0')res=res*10+(ch^48),ch=getchar();return f?-res:res;
}
typedef long long ll;
int n;
const int N=4e5+10;int a[N];ll K;int S;
unsigned seed=998244353;
unsigned rnd()
{
seed^=seed<<19;
seed^=seed>>13;
seed^=seed<<7;
return seed;
}
struct Node
{
int l,r,siz,val;ll sm;unsigned key;
}t[N];int Rt,T1,T2,T3,T4,T5,cnt;
inline void update(int x)
{
t[x].sm=t[t[x].l].sm+t[t[x].r].sm+t[x].val;
t[x].siz=t[t[x].l].siz+t[t[x].r].siz+1;
}
inline void split(int cur,int k,int &x,int &y)
{
if(!cur){x=y=0;return;}
if(t[cur].val<=k){x=cur;split(t[cur].r,k,t[x].r,y);update(x);}
else{y=cur;split(t[cur].l,k,x,t[cur].l);update(y);}
}
inline int merge(int x,int y)
{
if(!x||!y)return x|y;
if(t[x].key<t[y].key){t[x].r=merge(t[x].r,y);update(x);return x;}
t[y].l=merge(x,t[y].l);update(y);return y;
}
inline void Split(int cur,ll k,int &x,int &y)
{
if(!cur){x=y=0;return;}
if(t[t[cur].l].sm+t[cur].val<=k)
{
x=cur;Split(t[cur].r,k-t[t[cur].l].sm-t[cur].val,t[cur].r,y);
update(x);
}
else
{
y=cur;Split(t[cur].l,k,x,t[cur].l);
update(y);
}
}
inline int Node(int v)
{
t[++cnt].val=v;t[cnt].siz=1;
t[cnt].sm=v;t[cnt].key=rnd();
return cnt;
}
inline void ins(int k)
{
split(Rt,k,T1,T2);
Rt=merge(merge(T1,Node(k)),T2);
}
inline void del(int k)
{
split(Rt,k,T1,T3);
split(T1,k-1,T1,T2);
T2=merge(t[T2].l,t[T2].r);
Rt=merge(merge(T1,T2),T3);
}
inline void dfs(int x)
{
if(t[x].l)dfs(t[x].l);
printf("%d ",t[x].val);
if(t[x].r)dfs(t[x].r);
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("tj.out","w",stdout);
n=read();int Q=read();
rep(1,n,i)
{
a[i]=read();
if(a[i]<0)
{
K-=a[i];
}
else if(a[i]>0)
{
ins(a[i]);
++S;
}
}
while(Q--)
{
int i=read(),x=read();
if(a[i]<0)
{
K+=a[i];
}
else if(a[i]>0)
{
del(a[i]);
--S;
}
a[i]=x;
if(a[i]<0)
{
K-=a[i];
}
else if(a[i]>0)
{
ins(a[i]);
++S;
}
// rep(1,cnt,i)
// {
// printf("%d :: %d %d %d\n",i,t[i].l,t[i].r,t[i].val);
// }
Split(Rt,K,T1,T2);
// printf("a :: ");rep(1,n,i)printf("%d ",a[i]);puts("");
// printf("t :: ");dfs(T1);puts("");
int T=t[T2].siz;
Rt=merge(T1,T2);
printf("%d\n",S-T+1);
}
}
/*
3 5
1 2 3
3 4
2 -2
1 -3
3 1
2 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
2 2 6 BG BQWBWW 4 6 GQBW WWWWQB