每个数对答案的贡献=min(a[i],s)
所以用树状数组维护小于等于每个数的权值和以及个数
每次对于一个s,把小于s的所有数加起来,再把大于s的当成s,统计一下贡献,看能否实现
#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
#define N 2000010
using namespace std;
struct ppp {long long siz,sum;};
ppp c[N];
ppp operator+(ppp x,ppp y){return (ppp){x.siz+y.siz,x.sum+y.sum};}
long long n,m;
long long lowbit(long long x) {return x&(-x);}
void change(long long x,ppp v)
{
for(;x<=m+1;x+=lowbit(x))
c[x]=c[x]+v;
}
ppp check(long long x)
{
ppp ret={0,0};
for(;x;x-=lowbit(x))
ret=ret+c[x];
return ret;
}
long long o[N],x[N],y[N];
long long aa[N];
map<long long,long long>p;
long long a[N];
int main()
{
scanf("%lld%lld",&n,&m);
long long i,j;
char s[10];
for(i=1;i<=m;i++)
{
scanf("%s%lld%lld",s,&x[i],&y[i]);
if(s[0]=='Z') o[i]=1;
aa[i]=y[i];
}
sort(aa+1,aa+m+1);
p[0]=1;
for(i=1;i<=m;i++)
p[aa[i]]=i+1;
change(p[0],(ppp){n,0});
for(i=1;i<=m;i++)
{
if(o[i]==0)
{
change(p[a[x[i]]],(ppp){-1,-a[x[i]]});
a[x[i]]=y[i];
change(p[a[x[i]]],(ppp){1,a[x[i]]});
}
else
{
ppp t=check(p[y[i]]);
// cout<<t.siz<<' '<<t.sum<<endl;
if((n-t.siz)*y[i]+t.sum>=y[i]*x[i]) puts("TAK");
else puts("NIE");
}
}
}