Analysis
万恶的数据结构题目
又见线段树…
总结性质地说
这颗线段树需要
动态开点 + Hash
幸好不用在线
所以随便维护几个值
比如说
1. 左边连续空格数
2. 右边连续空格数
3. 中间(最多)连续空格数
4. 中间位置
5. 左右界
剩下的随意好了。
Tips 哈希不是想删就删的!
Code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define oo 2139062143
#define sqr(x) ((x)*(x))
#define abs(x) (((x)>=0)?(x):(-(x)))
#define max(x,y) (((x)>(y))?(x):(y))
#define min(x,y) (((x)<(y))?(x):(y))
#define fo(i,x,y) for (int i = (x);i <= (y);++ i)
#define fd(i,x,y) for (int i = (x);i >= (y);-- i)
#define fm(i,x) for (int i = las[x];i;i = e[i].nex)
using namespace std;
typedef long long ll;
const int N = 200200,Mo = 3628801;
int m,n,t0;
struct Poi{
int tot,vl,vr,val,mxs,l,r;
//tot 指l~r间排插使用数量 vl 左最大 vr 右最大 val中间最大 mxs中间最大左边界
}t[N * 10];
int hh[Mo],del[Mo];
int read(int &n)
{
char ch=' ';int q=0,w=1;
for(;(ch!='-')&&((ch<'0')||(ch>'9'));ch=getchar());
if(ch=='-')w=-1,ch=getchar();
for(;ch>='0' && ch<='9';ch=getchar())q=q*10+ch-48;n=q*w;return n;
}
int hash(int x)
{
int rt = x % Mo;
while (hh[rt] && hh[rt] != x) rt = (rt + 1) % Mo;
return rt;
}
void merge(Poi &s,Poi q,Poi w,int l,int r,int t)
{
if (q.tot == 0) q.vl = q.vr = t - l + 1;
if (w.tot == 0) w.vl = w.vr = r - t;
if (q.val > w.val) s.val = q.val,s.mxs = q.mxs;
else s.val = w.val,s.mxs = w.mxs;
if (s.val < q.vr + w.vl || (s.val == q.vr + w.vl && t - q.vr + 1 > s.mxs)) s.val = q.vr + w.vl,s.mxs = t - q.vr + 1;
s.vl = q.vl,s.vr = w.vr;
if (q.tot == 0) s.vl += w.vl;
if (w.tot == 0) s.vr += q.vr;
s.tot = q.tot + w.tot;
}
void change(int l,int r,int &e,int tl,int tr)
//tl询问L tr 询问R
{
if (!e) e = ++ t0;
if (l == r)
{
t[e].vl = t[e].vr = t[e].val = !(t[e].tot = tr);
if (!tr) t[e].mxs = l;
return;
}
int mid = (l + r) >> 1;
if (tl <= mid) change(l,mid,t[e].l,tl,tr);
else change(mid + 1,r,t[e].r,tl,tr);
merge(t[e],t[t[e].l],t[t[e].r],l,r,mid);
}
int find(int l,int r,int e,int tl,int tr)
{
if (!e) return 0;
if (l == tl && r == tr) return t[e].tot;
int mid = (l + r) >> 1;
if (tr <= mid) return find(l,mid,t[e].l,tl,tr);
else if (mid < tl) return find(mid + 1,r,t[e].r,tl,tr);
else return find(l,mid,t[e].l,tl,mid) + find(mid + 1,r,t[e].r,mid + 1,tr);
}
int main()
{
int v = 1;
read(n),read(m);
t[t0 = 1].val = n,t[1].mxs = 1;
fo(i,1,m)
{
int o;
scanf("%d", &o);
if(!o)
{
int x,y;
read(x),read(y);
printf("%d\n",find(1,n,1,x,y));
continue;
}
int code = hash(o);
if (del[code])
{
change(1,n,v,del[code],0);
del[code] = 0;
}
else
{
hh[code] = o;
o = (ll)((t[1].mxs << 1) + t[1].val) >> 1;
del[code] = o;
change(1,n,v,del[code],1);
}
}
}