RE + TLE
感觉是哪里写挂了。
#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace fast_IO {
#define IOSIZE 100000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
const int N=1e5+10;
const int C=290;
struct BLOCK {
vector<int> a;
int sum1[C],sum2[N];
int siz;
};
int n,m,b[N];
int bel[N],S=350,block;
int have[N];
vector<BLOCK> e;
inline void init() {
block=(9999)/S+1;
for (int i=1;i<=1e5;i++) bel[i]=(i-1)/S+1;
return ;
}
inline int query_over(int l,int r,int val) {
int res=0,bll=0,blr=0;
for (int i=0;i<e.size();i++) {
if (l>e[i].a.size()) l-=e[i].a.size();
else {
bll=i;
break;
}
}l--;
for (int i=0;i<e.size();i++) {
if (r>e[i].a.size()) r-=e[i].a.size();
else {
blr=i;
break;
}
}r--;
if (bll==blr) {
for (int i=l;i<=r;i++) {
if (e[bll].a[i]>val and have[e[bll].a[i]]) {
res++;
}
}
return res;
}
for (int i=l;i<e[i].a.size();i++) {
if (e[bll].a[i]>val and have[e[bll].a[i]]) {
res++;
}
}
for (int i=0;i<=r;i++) {
if (e[blr].a[i]>val and have[e[blr].a[i]]) {
res++;
}
}
for (int i=1;i<=block;i++) {
if (i*S<val) continue;
if ((i-1)*S+1>val) {
res+=(e[blr-1].sum1[i]-e[bll].sum1[i]);
continue;
}
for (int j=(i-1)*S+1;j<=i*S;j++) {
if (j>val and have[j]) {
res+=e[blr-1].sum2[j]-e[bll].sum2[j];
}
}
}
return res;
}
inline int query_less(int l,int r,int val) {
int res=0,bll=0,blr=0;
for (int i=0;i<e.size();i++) {
if (l>e[i].a.size()) l-=e[i].a.size();
else {
bll=i;
break;
}
}l--;
for (int i=0;i<e.size();i++) {
if (r>e[i].a.size()) r-=e[i].a.size();
else {
blr=i;
break;
}
}r--;
if (bll==blr) {
for (int i=l;i<=r;i++) {
if (e[bll].a[i]<val and have[e[bll].a[i]]) {
res++;
}
}
return res;
}
for (int i=l;i<e[i].a.size();i++) {
if (e[bll].a[i]<val and have[e[bll].a[i]]) {
res++;
}
}
for (int i=0;i<=r;i++) {
if (e[blr].a[i]<val and have[e[blr].a[i]]) {
res++;
}
}
for (int i=1;i<=block;i++) {
if (i*S>val) continue;
if ((i-1)*S+1<val) {
res+=(e[blr-1].sum1[i]-e[bll].sum1[i]);
continue;
}
for (int j=(i-1)*S+1;j<=i*S;j++) {
if (j<val and have[j]) {
res+=e[blr-1].sum2[j]-e[bll].sum2[j];
}
}
}
return res;
}
inline int find(int x) {
int res=0;
for (int i=0;i<e.size();i++) {
bool fl=0;
if (e[i].sum2[x]==1) {
fl=1;
for (int j=0;j<e[i].a.size();j++) {
res++;
if (e[i].a[j]==x) break;
}
}
if (fl) break;
res+=e[i].a.size();
}
return res;
}
inline void Delete(int x) {
for (int i=0;i<e.size();i++) {
BLOCK tmp=e[i];
if (tmp.sum2[x]==1) {
for (int j=i;j<e.size();j++) {
e[j].sum1[bel[x]]--;
e[j].sum2[x]--;
}
have[x]=0;
e[i].siz--;
break;
}
}
return ;
}
namespace BIT {
int t[N];
inline int lowbit(int x){return x&-x;}
inline void update(int x,int val) {
while (x<=n) {
t[x]+=val;
x+=lowbit(x);
}
return ;
}
inline int query(int x) {
int res=0;
while (x) {
res+=t[x];
x-=lowbit(x);
}
return res;
}
}using namespace BIT;
signed main() {
cin>>n>>m;e.emplace_back();
for (int i=1;i<=n;i++) {
int x;cin>>x;b[i]=x;have[x]=1;
e.back().a.emplace_back(x);
e.back().sum1[bel[x]]++;
e.back().sum2[x]++;
e.back().siz++;
if (e.back().a.size()>S) {
BLOCK tmp=e.back();
e.emplace_back(tmp);
e.back().a.clear();
}
}int res=0;
for (int i=1;i<=n;i++) {
update(b[i],1);
res+=i-1-query(b[i]-1);
}
while (m--) {
cout<<res<<endl;
int val,pos;cin>>val;pos=find(val);
res-=query_over(1,pos-1,val);
res-=query_less(pos+1,n,val);
Delete(val);
}
return (0-0);
}