代码为:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N = 80005, Mod = 1000000;
struct Node
{
int l,r;
LL val,key;
LL size;
}fhq[N];
int root,cnt;
int kind; // 树里装的是人还是狗
int n;
int x,y,z;
void pushup(int u)
{
fhq[u].size = fhq[fhq[u].l].size + fhq[fhq[u].r].size + 1;
}
void split(int u,LL val,int &x,int &y)
{
if(!u)
{
x = y = 0;
return ;
}
if(fhq[u].val <= val)
{
x = u;
split(fhq[u].r,val,fhq[u].r,y);
}
else
{
y = u;
split(fhq[u].l,val,x,fhq[u].l);
}
pushup(u);
}
int merge(int x,int y)
{
if(!x || !y) return x | y;
if(fhq[x].key >= fhq[y].key)
{
fhq[x].r = merge(fhq[x].r,y);
pushup(x);
return x;
}
else
{
fhq[y].l = merge(x,fhq[y].l);
pushup(y);
return y;
}
}
mt19937 rnd(233);
int intree = 0;
int create(LL val)
{
fhq[++cnt].val = val,fhq[cnt].key = rnd(),fhq[cnt].size = 1;
return cnt;
}
void insert(LL val)
{
split(root,val,x,y);
root = merge(merge(x,create(val)),y);
}
void del(LL val)
{
// intree --;
split(root,val,x,z);
split(x,val-1,x,y);
y = merge(fhq[y].l,fhq[y].r);
root = merge(x,merge(y,z));
// cnt --;
}
LL pre(LL val)
{
split(root,val-1,x,y);
int u = x;
while(fhq[u].r) u = fhq[u].r;
LL res = fhq[u].val;
root = merge(x,y);
return res;
}
LL nxt(LL val)
{
split(root,val,x,y);
int u = y;
while(fhq[u].l) u = fhq[u].l;
LL res = fhq[u].val;
root = merge(x,y);
return res;
}
int main()
{
// freopen("input.txt","r",stdin);
scanf("%d",&n);
insert(-1e10),insert(1e10);
long long ans = 0;
while(n --)
{
LL opt, val;
scanf("%lld%lld",&opt,&val);
if(!intree || opt == kind)
{
intree ++;
insert(val);
kind = opt;
}
else
{
intree --;
LL val1 = pre(val);
LL val2 = nxt(val);
if(abs(val-val1) < abs(val-val2)) del(val1),ans = (ans + abs(val-val1)) % Mod;
else del(val2),ans = (ans + abs(val-val2)) % Mod;
}
}
cout<<ans;
return 0;
}
开始的时候因为没加哨兵WA了几个点,加了哨兵之后无论是本地还是Luogu IDE测试样例都是错的
抱着随便试试的想法交了,却AC了。
求原因。。。