P5066 人人本着正义之名
解法在Ynoi末日三问精选集里面,这里用来存放代码。
#pragma pack(1)
#define inf 100000000
using namespace std;
inline int read()
{
register int x = 0, f = 1;
register char c = getchar();
while(c < '0' || c > '9')
{
if(c == '-')f = -1;
c = getchar();
}
while(c <= '9' && c >= '0')
{
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
inline void _write(int x)
{
if(!x)return;
_write(x / 10);
putchar(x % 10 + '0');
return;
}
inline void write(int x)
{
if(!x)putchar('0');
else if(x < 0)putchar('-'), _write(-x);
else _write(x);
return;
}
inline void writeln(int x)
{
if(!x)putchar('0');
else if(x < 0)putchar('-'), _write(-x);
else _write(x);
putchar('\n');
return;
}
mt19937 rnd(19260817);
struct my_rand {
typedef unsigned int result_type;
unsigned int _M_seed;
inline unsigned int u32_hasher(unsigned int x) {
float tmp = x;
return *(unsigned int*)&tmp;
}
inline unsigned int operator()() { return _M_seed += u32_hasher(_M_seed); }
inline my_rand(unsigned int sd = 5489u) : _M_seed(sd) {}
}Rnd;
struct val
{
int len = 0, val = -1;
int minlen[2] = {inf, inf}, sum[2] = {0, 0}, tot[2] = {0, 0};//子树内0/1最短区间长度,0/1的个数,0/1的区间数
//我们在修改的时候,如果有连续段被删空,则直接递归进入删除
//所以不会有长度<=0的情况
int l = -1, r = -1;//最左侧和最右侧是0/1
}nullval;
void merge2v(val &ans, val x, val y)
{
ans.minlen[0] = min(x.minlen[0], y.minlen[0]);
ans.minlen[1] = min(x.minlen[1], y.minlen[1]);
ans.sum[0] = x.sum[0] + y.sum[0];
ans.sum[1] = x.sum[1] + y.sum[1];
ans.tot[0] = x.tot[0] + y.tot[0];
ans.tot[1] = x.tot[1] + y.tot[1];
if(x.l != -1)ans.l = x.l;
else ans.l = ans.val;
if(y.r != -1)ans.r = y.r;
else ans.r = ans.val;
ans.minlen[ans.val] = min(ans.minlen[ans.val], ans.len);
ans.sum[ans.val] += ans.len;
ans.tot[ans.val]++;
return;
}
struct tag
{
int right[2], left[2];//0/1,+是增,-是少
// int set = -1;//区间推平
//我们可以通过一种神秘的操作:推平时取出包含的所有的连续段,删掉,换成一个新节点
//避免了区间推平tag
}newtg;
val newval(int len, int v)
{
val ans;
ans.len = len, ans.val = ans.l = ans.r = v;
if(v == 0)ans.minlen[0] = ans.sum[0] = len, ans.tot[0] = 1, ans.minlen[1] = inf, ans.sum[1] = ans.tot[1] = 0;
else ans.minlen[1] = ans.sum[1] = len, ans.tot[1] = 1, ans.minlen[0] = inf, ans.sum[0] = ans.tot[0] = 0;
return ans;
}
val adtg2v(val v, tag tg)
{
val x = v;
x.len += tg.right[v.val] + tg.left[v.val];
x.minlen[0] += tg.right[0] + tg.left[0];
x.minlen[1] += tg.right[1] + tg.left[1];
x.sum[0] += x.tot[0] * (tg.right[0] + tg.left[0]);
x.sum[1] += x.tot[1] * (tg.right[1] + tg.left[1]);///每个连续段变化这么多
return x;
}
tag adtg2tg(tag x, tag y)//把y加到x上
{
tag ans = x;
ans.right[0] += y.right[0], ans.right[1] += y.right[1];
ans.left[0] += y.left[0], ans.left[1] += y.left[1];//好像直接加就完事了??
return ans;
}
//注意!!!我们必须保证01连续段间隔排列,我们才是对的!!!
struct node
{
val v;
tag tg;//存的v是加上tag之后的
int ch[2];
unsigned int rnd;
}t[6000010];
int n, m, a[3000100], cnt, rt;
stack <int> st;
int newnode(val v)
{
int x;
if(!st.empty())x = st.top(), st.pop();
else x = ++cnt;
t[x] = {v, newtg, 0, 0, Rnd()};
return x;
}
void pushdown(int q)
{
// t[q].v = adtg2v(t[q].v, t[q].tg);
if(t[q].ch[0])
{
t[t[q].ch[0]].tg = adtg2tg(t[t[q].ch[0]].tg, t[q].tg);
t[t[q].ch[0]].v = adtg2v(t[t[q].ch[0]].v, t[q].tg);
}
if(t[q].ch[1])
{
t[t[q].ch[1]].tg = adtg2tg(t[t[q].ch[1]].tg, t[q].tg);
t[t[q].ch[1]].v = adtg2v(t[t[q].ch[1]].v, t[q].tg);
}
t[q].tg = newtg;
return;
}
void update(int q)
{
pushdown(q);
merge2v(t[q].v, t[t[q].ch[0]].v, t[t[q].ch[1]].v);
//意味着我们合并两个val的时候不需要考虑tag的影响了!
t[q].v = adtg2v(t[q].v, t[q].tg);
return;
}
void splitx(int q, int &x, int &y, int pos)//将包含pos位置的节点作为x的最右点,右边的给y
{
if(!q)return (void)(x = y = 0);
pushdown(q);
int llen = t[t[q].ch[0]].v.sum[0] + t[t[q].ch[0]].v.sum[1];
if(llen < pos && llen + t[q].v.len >= pos)
{
x = q, y = t[q].ch[1];
t[x].ch[1] = 0;
update(x);
return;
}
if(llen >= pos)
{
y = q;
splitx(t[q].ch[0], x, t[q].ch[0], pos);
}
else
{
x = q;
splitx(t[q].ch[1], t[q].ch[1], y, pos - llen - t[q].v.len);
}
update(q);
return;
}
void splity(int q, int &x, int &y, int pos)//将包含pos位置的节点作为y的最左点,左边的给x
{
if(!q)return (void)(x = y = 0);
pushdown(q);
int llen = t[t[q].ch[0]].v.sum[0] + t[t[q].ch[0]].v.sum[1];
// cout << "splity111: " << q << ' ' << llen << ' ' << pos << '\n';
if(llen < pos && llen + t[q].v.len >= pos)
{
y = q, x = t[q].ch[0];
// cout << "splity: " << x << ' ' << y << "\n";
t[y].ch[0] = 0;
update(y);
return;
}
if(llen >= pos)
{
y = q;
splity(t[q].ch[0], x, t[q].ch[0], pos);
}
else
{
x = q;
splity(t[q].ch[1], t[q].ch[1], y, pos - llen - t[q].v.len);
}
update(q);
return;
}
void splitk(int q, int &x, int &y, int k)//先分到左边
{
if(!q)return (void)(x = y = 0);
pushdown(q);
int lsize = t[t[q].ch[0]].v.tot[0] + t[t[q].ch[0]].v.tot[1];
if(lsize + 1 == k)
{
x = q, y = t[q].ch[1];
t[x].ch[1] = 0;
update(x);
return;
}
if(lsize >= k)
{
y = q;
splitk(t[q].ch[0], x, t[q].ch[0], k);
}
else
{
x = q;
splitk(t[q].ch[1], t[q].ch[1], y, k - lsize - 1);
}
update(q);
return;
}
int merge(int x, int y)
{
if(!x || !y)return x + y;
pushdown(x);
pushdown(y);
if(t[x].rnd > t[y].rnd)
{
t[x].ch[1] = merge(t[x].ch[1], y);
update(x);
return x;
}
else
{
t[y].ch[0] = merge(x, t[y].ch[0]);
update(y);
return y;
}
}
void deltree(int q)
{
if(!q)return;
deltree(t[q].ch[0]);
st.push(q);
deltree(t[q].ch[1]);
t[q] = {nullval, newtg, 0, 0, Rnd()};
return;
}
//del要改!!!建议直接使用splitk与merge进行,类似assign,需要编写函数查找splitk的k
int get1rnk(int q, int v)
{
if(!q)return inf;
pushdown(q);
if(t[t[q].ch[0]].v.minlen[v] <= 1)return get1rnk(t[q].ch[0], v);
if(t[q].v.len == 1 && t[q].v.val == v)return t[t[q].ch[0]].v.tot[0] + t[t[q].ch[0]].v.tot[1] + 1;
if(t[q].v.minlen[v] > 1)return inf;
return get1rnk(t[q].ch[1], v) + t[t[q].ch[0]].v.tot[0] + t[t[q].ch[0]].v.tot[1] + 1;
}
void del(int &q, int v)
{
// cout << "del: ";
// cout << q << ' ' << v << "\n";
int p = get1rnk(q, v);
int x, y, z;
splitk(q, x, y, p);
splitk(x, x, z, p - 1);
// cout << t[z].v.val << '\n';
q = merge(x, merge(z, y));
while(p < inf / 2)
{
// cout << p << ' ' << t[q].v.tot[0] + t[q].v.tot[1] << "\n";
splitk(q, x, z, min(p + 1, t[q].v.tot[0] + t[q].v.tot[1]));
splitk(x, x, y, max(0, p - 2));
// cout << x << ' ' << y << ' ' << z << ' ' << t[x].v.r << ' ' << t[y].v.l << ' ' << t[y].v.r << ' ' << t[z].v.l << "\n";
q = merge(x, merge(newnode(newval(t[y].v.sum[0] + t[y].v.sum[1], v ^ 1)), z));
deltree(y);
p = get1rnk(q, v);
}
return;
}
void delnode(int q)
{
st.push(q);
t[q] = {nullval, newtg, 0, 0, Rnd()};
return;
}
//void del(int q, int v)//删除q子树中len==1,val==v的节点并合并左右子的区间为一个连续段,merge起来
//{
// pushdown(q);
// if(t[q].v.len == 1 && t[q].v.val == v)
// {
// pushdown(t[q].ch[0]);
// pushdown(t[q].ch[1]);
// int x = merge(t[t[q].ch[0]].ch[0], t[t[q].ch[0]].ch[1]);
// t[t[q].ch[1]].v = newval(t[t[q].ch[0]].v.len + t[t[q].ch[1]].v.len, t[t[q].ch[0]].v.val);
// t[q] = t[t[q].ch[1]];
// update(q);
// q = merge(x, q);
// update(q);
// }
// if(t[t[q].ch[0]].v.minlen[v] == 1)del(t[q].ch[0], v);
// if(t[t[q].ch[1]].v.minlen[v] == 0)del(t[q].ch[1], v);
// update(q);
// return;
//}
void assign(int l, int r, int v)//进行一个区间的推平
{
int x, y, z;
splitx(rt, x, z, r);
splity(x, x, y, l);
//现在的y包含了区间[l,r]
//我们需要把两头留下,中间推平
// cout << x << ' ' << y << ' ' << z << "\n";
int yl = t[x].v.sum[0] + t[x].v.sum[1] + 1, yr = t[x].v.sum[0] + t[x].v.sum[1] + t[y].v.sum[0] + t[y].v.sum[1];
int lv = t[y].v.l, rv = t[y].v.r;
int ans = newnode(newval(r - l + 1, v));
if(l != yl && v == lv)t[ans].v.len += l - yl, t[ans].v.sum[v] += l - yl, t[ans].v.minlen[v] += l - yl;
if(r != yr && v == rv)t[ans].v.len += yr - r, t[ans].v.sum[v] += yr - r, t[ans].v.minlen[v] += yr - r;
if(l != yl && v != lv)if(v != lv)t[ans].ch[0] = newnode(newval(l - yl, lv));
if(r != yr && v != rv)if(v != rv)t[ans].ch[1] = newnode(newval(yr - r, rv));
update(ans);
if(t[ans].v.l == t[x].v.r)
{
int o, p;
splitk(x, x, o, t[x].v.tot[0] + t[x].v.tot[1] - 1);
splitk(ans, p, ans, 1);
ans = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), ans);
delnode(o), delnode(p);
}
if(t[ans].v.r == t[z].v.l)
{
int o, p;
splitk(ans, ans, o, t[ans].v.tot[0] + t[ans].v.tot[1] - 1);
splitk(z, p, z, 1);
z = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), z);
delnode(o), delnode(p);
}
rt = merge(x, merge(ans, z));
deltree(y);
return;
}
void rightand(int l, int r)//[l,r-1]a_i与a_i+1按位与
{
//等同于区间中的0连续段左侧增加一个,1连续段右侧减少一个
//需要在这里判出来最小1连续段长度是不是1,是1就删掉他并合并左右连续段
int x, y, z, w = 0, u = 0, a = 0, b = 0;
splitx(rt, x, z, r);
splity(x, x, y, l);
//删完了,还要考虑位置0和位置n的特殊情况
if(t[y].v.l == 0)splitk(y, w, y, 1);
if(t[y].v.r == 1)splitk(y, y, u, t[y].v.tot[0] + t[y].v.tot[1] - 1);
if(t[y].v.minlen[1] == 1)del(y, 1);//删除y子树内所有长度为1的0连续段并合并左右子节点为一个节点,时间是O(logn)
if(x && !w && y && t[y].v.l == t[x].v.r)
{
int o, p;
splitk(y, o, y, 1);
splitk(x, x, p, t[x].v.tot[0] + t[x].v.tot[1] - 1);
x = merge(x, newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)));
delnode(o), delnode(p);
}
if(w && y && t[y].v.l == t[w].v.r)
{
int o;
splitk(y, o, y, 1);
t[w].v.len += t[o].v.len, t[w].v.minlen[t[w].v.val] += t[o].v.len, t[w].v.sum[t[w].v.val] += t[o].v.len;
}
if(z && !u && y && t[y].v.r == t[z].v.l)
{
int o, p;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
splitk(z, p, z, 1);
t[o].v.len += t[p].v.len;
z = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), z);
delnode(o), delnode(p);
}
if(u && y && t[y].v.r == t[u].v.l)
{
int o;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[u].v.len += t[o].v.len, t[u].v.minlen[t[u].v.val] += t[o].v.len, t[u].v.sum[t[u].v.val] += t[o].v.len;
}
if(t[y].v.l == 0)splitk(y, a, y, 1);
if(t[y].v.r == 1)splitk(y, y, b, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[y].tg.right[1]--;
t[y].tg.left[0]++;
t[y].v = adtg2v(t[y].v, {0, -1, 1, 0});
rt = merge(merge(x, merge(w, a)), merge(y, merge(merge(b, u), z)));
return;
}
void rightor(int l, int r)
{
//等同于区间中的0连续段右侧减少一个,1连续段左侧增加一个
//需要在这里判出来最小0连续段长度是不是1,是1就删掉他并合并左右连续段
int x, y, z, w = 0, u = 0, a = 0, b = 0;
splitx(rt, x, z, r);
// cout << x << ' ' << z << ' ' << t[x].v.tot[0] + t[x].v.tot[1] << "\n";
splity(x, x, y, l);
// cout << x << ' ' << y << ' ' << z << ' ' << t[y].v.tot[0] + t[y].v.tot[1] << "\n";
//删完了,还要考虑位置0和位置n的特殊情况
// cout << y << ' ' << t[y].v.tot[0] + t[y].v.tot[1] << ' ' << t[y].v.l << ' ' << t[y].v.r << "\n";
if(t[y].v.l == 1)splitk(y, w, y, 1);
// cout << y << '\n';
if(t[y].v.r == 0)splitk(y, y, u, t[y].v.tot[0] + t[y].v.tot[1] - 1);
if(t[y].v.minlen[0] == 1)del(y, 0);//删除y子树内所有长度为1的0连续段并合并左右子节点为一个节点,时间是O(logn)
if(x && !w && y && t[y].v.l == t[x].v.r)
{
int o, p;
splitk(y, o, y, 1);
splitk(x, x, p, t[x].v.tot[0] + t[x].v.tot[1] - 1);
x = merge(x, newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)));
delnode(o), delnode(p);
}
if(w && y && t[y].v.l == t[w].v.r)
{
int o;
splitk(y, o, y, 1);
t[w].v.len += t[o].v.len, t[w].v.minlen[t[w].v.val] += t[o].v.len, t[w].v.sum[t[w].v.val] += t[o].v.len;
}
if(z && !u && y && t[y].v.r == t[z].v.l)
{
int o, p;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
splitk(z, p, z, 1);
t[o].v.len += t[p].v.len;
z = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), z);
delnode(o), delnode(p);
}
if(u && y && t[y].v.r == t[u].v.l)
{
int o;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[u].v.len += t[o].v.len, t[u].v.minlen[t[u].v.val] += t[o].v.len, t[u].v.sum[t[u].v.val] += t[o].v.len;
}
// cout << y << ' ' << t[y].v.tot[0] + t[y].v.tot[1] << ' ' << t[y].v.l << ' ' << t[y].v.r << "\n";
if(t[y].v.l == 1)splitk(y, a, y, 1);
// cout << y << '\n';
if(t[y].v.r == 0)splitk(y, y, b, t[y].v.tot[0] + t[y].v.tot[1] - 1);
// cout << x << ' ' << w << ' ' << y << ' ' << u << ' ' << z << "\n";
t[y].tg.right[0]--;
t[y].tg.left[1]++;
t[y].v = adtg2v(t[y].v, {-1, 0, 0, 1});
rt = merge(merge(x, merge(w, a)), merge(y, merge(merge(b, u), z)));
return;
}
void leftand(int l, int r)
{
//等同于区间中的0连续段右侧增加一个,1连续段左侧减少一个
//需要在这里判出来最小1连续段长度是不是1,是1就删掉他并合并左右连续段
int x, y, z, w = 0, u = 0, a = 0, b = 0;
splitx(rt, x, z, r);
// cout << x << ' ' << z << ' ' << t[x].v.tot[0] + t[x].v.tot[1] << "\n";
splity(x, x, y, l);
// cout << x << ' ' << y << ' ' << z << ' ' << t[y].v.tot[0] + t[y].v.tot[1] << "\n";
//删完了,还要考虑位置0和位置n的特殊情况
// cout << y << ' ' << t[y].v.tot[0] + t[y].v.tot[1] << ' ' << t[y].v.l << ' ' << t[y].v.r << "\n";
if(t[y].v.l == 1)splitk(y, w, y, 1);
// cout << y << '\n';
if(t[y].v.r == 0)splitk(y, y, u, t[y].v.tot[0] + t[y].v.tot[1] - 1);
// cout << x << ' ' << w << ' ' << y << ' ' << u << ' ' << z << "\n";
if(t[y].v.minlen[1] == 1)del(y, 1);//删除y子树内所有长度为1的0连续段并合并左右子节点为一个节点,时间是O(logn)
if(x && !w && y && t[y].v.l == t[x].v.r)
{
int o, p;
splitk(y, o, y, 1);
splitk(x, x, p, t[x].v.tot[0] + t[x].v.tot[1] - 1);
x = merge(x, newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)));
delnode(o), delnode(p);
}
if(w && y && t[y].v.l == t[w].v.r)
{
int o;
splitk(y, o, y, 1);
t[w].v.len += t[o].v.len, t[w].v.minlen[t[w].v.val] += t[o].v.len, t[w].v.sum[t[w].v.val] += t[o].v.len;
}
if(z && !u && y && t[y].v.r == t[z].v.l)
{
int o, p;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
splitk(z, p, z, 1);
t[o].v.len += t[p].v.len;
z = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), z);
delnode(o), delnode(p);
}
if(u && y && t[y].v.r == t[u].v.l)
{
int o;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[u].v.len += t[o].v.len, t[u].v.minlen[t[u].v.val] += t[o].v.len, t[u].v.sum[t[u].v.val] += t[o].v.len;
}
if(t[y].v.l == 1)splitk(y, a, y, 1);
// cout << y << '\n';
if(t[y].v.r == 0)splitk(y, y, b, t[y].v.tot[0] + t[y].v.tot[1] - 1);
// cout << x << ' ' << w << ' ' << y << ' ' << u << ' ' << z << "\n";
t[y].tg.right[0]++;
t[y].tg.left[1]--;
t[y].v = adtg2v(t[y].v, {1, 0, 0, -1});
rt = merge(merge(x, merge(w, a)), merge(y, merge(merge(b, u), z)));
return;
}
void leftor(int l, int r)
{
//等同于区间中的0连续段左侧减少一个,1连续段右侧增加一个
//需要在这里判出来最小0连续段长度是不是1,是1就删掉他并合并左右连续段
int x, y, z, w = 0, u = 0, a = 0, b = 0;
splitx(rt, x, z, r);
splity(x, x, y, l);
//删完了,还要考虑位置0和位置n的特殊情况
if(t[y].v.l == 0)splitk(y, w, y, 1);
if(t[y].v.r == 1)splitk(y, y, u, t[y].v.tot[0] + t[y].v.tot[1] - 1);
if(t[y].v.minlen[0] == 1)del(y, 0);//删除y子树内所有长度为1的0连续段并合并左右子节点为一个节点,时间是O(logn)
if(x && !w && y && t[y].v.l == t[x].v.r)
{
int o, p;
splitk(y, o, y, 1);
splitk(x, x, p, t[x].v.tot[0] + t[x].v.tot[1] - 1);
x = merge(x, newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)));
delnode(o), delnode(p);
}
if(w && y && t[y].v.l == t[w].v.r)
{
int o;
splitk(y, o, y, 1);
t[w].v.len += t[o].v.len, t[w].v.minlen[t[w].v.val] += t[o].v.len, t[w].v.sum[t[w].v.val] += t[o].v.len;
}
if(z && !u && y && t[y].v.r == t[z].v.l)
{
int o, p;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
splitk(z, p, z, 1);
t[o].v.len += t[p].v.len;
z = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), z);
delnode(o), delnode(p);
}
if(u && y && t[y].v.r == t[u].v.l)
{
int o;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[u].v.len += t[o].v.len, t[u].v.minlen[t[u].v.val] += t[o].v.len, t[u].v.sum[t[u].v.val] += t[o].v.len;
}
if(t[y].v.l == 0)splitk(y, a, y, 1);
if(t[y].v.r == 1)splitk(y, y, b, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[y].tg.right[1]++;
t[y].tg.left[0]--;
t[y].v = adtg2v(t[y].v, {0, 1, -1, 0});
rt = merge(merge(x, merge(w, a)), merge(y, merge(merge(b, u), z)));
return;
}
int query(int l, int r)
{
int x, y, z;
splitx(rt, x, z, r);
// cout << x << ' ' << z << "\n";
splity(x, x, y, l);
// cout << x << ' ' << y << "\n";
int yl = t[x].v.sum[0] + t[x].v.sum[1] + 1, yr = t[x].v.sum[0] + t[x].v.sum[1] + t[y].v.sum[0] + t[y].v.sum[1];
int ans = t[y].v.sum[1];
// cout << x << ' ' << y << ' ' << z << ' ' << yl << ' ' << yr << ' ' << t[x].v.sum[0] << ' ' << t[x].v.sum[1] << ' '<< t[y].v.sum[0] << ' ' << t[y].v.sum[1] << "\n";
ans -= t[y].v.l * (l - yl) + t[y].v.r * (yr - r);
rt = merge(x, merge(y, z));
return ans;
}
void print(int q)
{
if(!q)return;
pushdown(q);
print(t[q].ch[0]);
for(int i = 1;i <= t[q].v.len;i++)cout << t[q].v.val << ' ';
// for(int q = 1;q <= cnt;q++)
// cout << q << ' ' << t[q].ch[0] << ' ' << t[q].ch[1] << ' ' << t[q].v.len << ' ' << t[q].v.val << ' ' << t[q].v.l << ' ' << t[q].v.r << ' ' << t[q].v.sum[0] << ' ' << t[q].v.sum[1] << ' ' << t[q].v.tot[0] << ' ' << t[q].v.tot[1] << ' ' << t[q].v.minlen[0] << ' ' << t[q].v.minlen[1] << ' ' << t[q].tg.right[0] + t[q].tg.left[0] << ' ' << t[q].tg.right[1] + t[q].tg.left[1] << "\n";
print(t[q].ch[1]);
update(q);
return;
}
signed main()
{
n = read(), m = read();
for(int i = 1;i <= n;i++)a[i] = read();
int len = 1, val = a[1];
for(int i = 2;i <= n;i++)
{
if(val != a[i])
{
rt = merge(rt, newnode(newval(len, val)));
val = a[i], len = 0;
}
len++;
}
rt = merge(rt, newnode(newval(len, val)));
// cout << rt << '\n';
print(rt);
cout << '\n';
int lastans = 0;
while(m--)
{
int op, l, r;
op = read(), l = read(), r = read();
// l ^= lastans, r ^= lastans;
if(op == 1)assign(l, r, 0);
if(op == 2)assign(l, r, 1);
if(op == 3)rightor(l, r);
if(op == 4)leftor(l, r);
if(op == 5)rightand(l, r);
if(op == 6)leftand(l, r);
if(op == 7)writeln(lastans = query(l, r));
// cout << rt << '\n';
print(rt);
cout << "\n";
}
return 0;
}
由于在调试的时候我把注释删了,上面那版代码不正确,但是有注释。下面这版正确但是没注释。
#include <bits/stdc++.h>
#pragma pack(1)
#define inf 100000000
using namespace std;
const int BUFSIZE=1<<23;
char ibuf[BUFSIZE],*is=ibuf,*it=ibuf;
__inline char getch(){if(is==it)it=(is=ibuf)+fread(ibuf,1,BUFSIZE,stdin);return is==it?EOF:*is++;}
char tmp[BUFSIZE];int CNT=0;
void flush(){fwrite(tmp,1,CNT,stdout);CNT=0;}
__inline void putch(char c){tmp[CNT++]=c;if(CNT==BUFSIZE)flush();}
__inline int read()
{
register int x = 0, f = 1;
register char c = getch();
while(c < '0' || c > '9')
{
if(c == '-')f = -1;
c = getch();
}
while(c <= '9' && c >= '0')
{
x = x * 10 + c - '0';
c = getch();
}
return x * f;
}
__inline void _write(int x)
{
if(!x)return;
_write(x / 10);
putch(x % 10 + '0');
return;
}
__inline void write(int x)
{
if(!x)putch('0');
else if(x < 0)putch('-'), _write(-x);
else _write(x);
return;
}
__inline void writeln(int x)
{
if(!x)putch('0');
else if(x < 0)putch('-'), _write(-x);
else _write(x);
putch('\n');
return;
}
struct my_rand {
typedef unsigned int result_type;
unsigned int _M_seed;
__inline unsigned int u32_hasher(unsigned int x) {
float tmp = x;
return *(unsigned int*)&tmp;
}
__inline unsigned int operator()() { return _M_seed += u32_hasher(_M_seed); }
__inline my_rand(unsigned int sd = 9751u) : _M_seed(sd) {}
}Rnd;
struct val
{
int len = 0, val = -1;
int minlen[2] = {inf, inf}, sum[2] = {0, 0}, tot[2] = {0, 0};
int l = -1, r = -1;
}nullval;
__inline void merge2v(val &ans, val x, val y)
{
ans.minlen[0] = min(x.minlen[0], y.minlen[0]);
ans.minlen[1] = min(x.minlen[1], y.minlen[1]);
ans.sum[0] = x.sum[0] + y.sum[0];
ans.sum[1] = x.sum[1] + y.sum[1];
ans.tot[0] = x.tot[0] + y.tot[0];
ans.tot[1] = x.tot[1] + y.tot[1];
ans.l = (x.l == -1 ? ans.val : x.l);
ans.r = (y.r == -1 ? ans.val : y.r);
ans.minlen[ans.val] = min(ans.minlen[ans.val], ans.len);
ans.sum[ans.val] += ans.len;
ans.tot[ans.val]++;
return;
}
__inline val newval(int len, int v)
{
register val ans;
ans.len = len, ans.val = ans.l = ans.r = v;
if(v == 0)ans.minlen[0] = ans.sum[0] = len, ans.tot[0] = 1;
else ans.minlen[1] = ans.sum[1] = len, ans.tot[1] = 1;
return ans;
}
__inline void adtg2v(val &v, int tg)
{
v.len += (v.val ? -tg : tg);
v.minlen[0] += tg;
v.minlen[1] -= tg;
v.sum[0] += v.tot[0] * tg;
v.sum[1] -= v.tot[1] * tg;
return;
}
struct node
{
val v;
int tg;
int ch[2];
unsigned int rnd;
}t[3000010];
int n, m, a[3000100], cnt, rt;
stack <int> st;
__inline int newnode(val v)
{
int x;
if(!st.empty())x = st.top(), st.pop();
else x = ++cnt;
t[x] = {v, 0, 0, 0, Rnd()};
return x;
}
__inline void pushdown(int q)
{
if(!t[q].tg)return;
if(t[q].ch[0])
{
t[t[q].ch[0]].tg += t[q].tg;
adtg2v(t[t[q].ch[0]].v, t[q].tg);
}
if(t[q].ch[1])
{
t[t[q].ch[1]].tg += t[q].tg;
adtg2v(t[t[q].ch[1]].v, t[q].tg);
}
t[q].tg = 0;
return;
}
__inline void update(int q)
{
merge2v(t[q].v, t[t[q].ch[0]].v, t[t[q].ch[1]].v);
adtg2v(t[q].v, t[q].tg);
return;
}
__inline void splitx(int q, int &x, int &y, int pos)
{
if(!q)return (void)(x = y = 0);
pushdown(q);
int llen = t[t[q].ch[0]].v.sum[0] + t[t[q].ch[0]].v.sum[1];
if(llen < pos && llen + t[q].v.len >= pos)
{
x = q, y = t[q].ch[1];
t[x].ch[1] = 0;
update(x);
return;
}
if(llen >= pos)
{
y = q;
splitx(t[q].ch[0], x, t[q].ch[0], pos);
}
else
{
x = q;
splitx(t[q].ch[1], t[q].ch[1], y, pos - llen - t[q].v.len);
}
update(q);
return;
}
__inline void splity(int q, int &x, int &y, int pos)
{
if(!q)return (void)(x = y = 0);
pushdown(q);
int llen = t[t[q].ch[0]].v.sum[0] + t[t[q].ch[0]].v.sum[1];
if(llen < pos && llen + t[q].v.len >= pos)
{
y = q, x = t[q].ch[0];
t[y].ch[0] = 0;
update(y);
return;
}
if(llen >= pos)
{
y = q;
splity(t[q].ch[0], x, t[q].ch[0], pos);
}
else
{
x = q;
splity(t[q].ch[1], t[q].ch[1], y, pos - llen - t[q].v.len);
}
update(q);
return;
}
__inline void splitk(int q, int &x, int &y, int k)
{
if(!q)return (void)(x = y = 0);
pushdown(q);
int lsize = t[t[q].ch[0]].v.tot[0] + t[t[q].ch[0]].v.tot[1];
if(lsize + 1 == k)
{
x = q, y = t[q].ch[1];
t[x].ch[1] = 0;
update(x);
return;
}
if(lsize >= k)
{
y = q;
splitk(t[q].ch[0], x, t[q].ch[0], k);
}
else
{
x = q;
splitk(t[q].ch[1], t[q].ch[1], y, k - lsize - 1);
}
update(q);
return;
}
__inline int merge(int x, int y)
{
if(!x || !y)return x + y;
if(t[x].rnd > t[y].rnd)
{
pushdown(x);
t[x].ch[1] = merge(t[x].ch[1], y);
update(x);
return x;
}
else
{
pushdown(y);
t[y].ch[0] = merge(x, t[y].ch[0]);
update(y);
return y;
}
}
__inline void deltree(int q)
{
if(!q)return;
deltree(t[q].ch[0]);
st.push(q);
deltree(t[q].ch[1]);
t[q] = {nullval, 0, 0, 0, Rnd()};
return;
}
__inline int get1rnk(int q, int v)
{
if(!q)return inf;
pushdown(q);
if(t[t[q].ch[0]].v.minlen[v] <= 1)return get1rnk(t[q].ch[0], v);
if(t[q].v.len == 1 && t[q].v.val == v)return t[t[q].ch[0]].v.tot[0] + t[t[q].ch[0]].v.tot[1] + 1;
if(t[q].v.minlen[v] > 1)return inf;
return get1rnk(t[q].ch[1], v) + t[t[q].ch[0]].v.tot[0] + t[t[q].ch[0]].v.tot[1] + 1;
}
__inline void del(int &q, int v)
{
int p = get1rnk(q, v);
int x, y, z;
splitk(q, x, y, p);
splitk(x, x, z, p - 1);
q = merge(x, merge(z, y));
while(p < inf / 2)
{
splitk(q, x, z, min(p + 1, t[q].v.tot[0] + t[q].v.tot[1]));
splitk(x, x, y, max(0, p - 2));
q = merge(x, merge(newnode(newval(t[y].v.sum[0] + t[y].v.sum[1], v ^ 1)), z));
deltree(y);
p = get1rnk(q, v);
}
return;
}
__inline void delnode(int q)
{
st.push(q);
t[q] = {nullval, 0, 0, 0, Rnd()};
return;
}
__inline void assign(int l, int r, int v)
{
int x, y, z;
splitx(rt, x, z, r);
splity(x, x, y, l);
int yl = t[x].v.sum[0] + t[x].v.sum[1] + 1, yr = t[x].v.sum[0] + t[x].v.sum[1] + t[y].v.sum[0] + t[y].v.sum[1];
int lv = t[y].v.l, rv = t[y].v.r;
int ans = newnode(newval(r - l + 1, v));
if(l != yl && v == lv)t[ans].v.len += l - yl, t[ans].v.sum[v] += l - yl, t[ans].v.minlen[v] += l - yl;
if(r != yr && v == rv)t[ans].v.len += yr - r, t[ans].v.sum[v] += yr - r, t[ans].v.minlen[v] += yr - r;
if(l != yl && v != lv)if(v != lv)t[ans].ch[0] = newnode(newval(l - yl, lv));
if(r != yr && v != rv)if(v != rv)t[ans].ch[1] = newnode(newval(yr - r, rv));
update(ans);
if(t[ans].v.l == t[x].v.r)
{
int o, p;
splitk(x, x, o, t[x].v.tot[0] + t[x].v.tot[1] - 1);
splitk(ans, p, ans, 1);
ans = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), ans);
delnode(o), delnode(p);
}
if(t[ans].v.r == t[z].v.l)
{
int o, p;
splitk(ans, ans, o, t[ans].v.tot[0] + t[ans].v.tot[1] - 1);
splitk(z, p, z, 1);
z = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), z);
delnode(o), delnode(p);
}
rt = merge(x, merge(ans, z));
deltree(y);
return;
}
__inline void rightand(int l, int r)
{
int x, y, z, w = 0, u = 0, a = 0, b = 0;
splitx(rt, x, z, r);
splity(x, x, y, l);
if(t[y].v.l == 0)splitk(y, w, y, 1);
if(t[y].v.r == 1)splitk(y, y, u, t[y].v.tot[0] + t[y].v.tot[1] - 1);
if(t[y].v.minlen[1] == 1)del(y, 1);
if(x && !w && y && t[y].v.l == t[x].v.r)
{
int o, p;
splitk(y, o, y, 1);
splitk(x, x, p, t[x].v.tot[0] + t[x].v.tot[1] - 1);
x = merge(x, newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)));
delnode(o), delnode(p);
}
if(w && y && t[y].v.l == t[w].v.r)
{
int o;
splitk(y, o, y, 1);
t[w].v.len += t[o].v.len, t[w].v.minlen[t[w].v.val] += t[o].v.len, t[w].v.sum[t[w].v.val] += t[o].v.len;
delnode(o);
}
if(z && !u && y && t[y].v.r == t[z].v.l)
{
int o, p;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
splitk(z, p, z, 1);
z = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), z);
delnode(o), delnode(p);
}
if(u && y && t[y].v.r == t[u].v.l)
{
int o;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[u].v.len += t[o].v.len, t[u].v.minlen[t[u].v.val] += t[o].v.len, t[u].v.sum[t[u].v.val] += t[o].v.len;
delnode(o);
}
if(t[y].v.l == 0)splitk(y, a, y, 1);
if(t[y].v.r == 1)splitk(y, y, b, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[y].tg++;
adtg2v(t[y].v, 1);
rt = merge(merge(x, merge(w, a)), merge(y, merge(merge(b, u), z)));
return;
}
__inline void rightor(int l, int r)
{
int x, y, z, w = 0, u = 0, a = 0, b = 0;
splitx(rt, x, z, r);splity(x, x, y, l);if(t[y].v.l == 1)splitk(y, w, y, 1);
if(t[y].v.r == 0)splitk(y, y, u, t[y].v.tot[0] + t[y].v.tot[1] - 1);
if(t[y].v.minlen[0] == 1)del(y, 0);
if(x && !w && y && t[y].v.l == t[x].v.r)
{
int o, p;
splitk(y, o, y, 1);
splitk(x, x, p, t[x].v.tot[0] + t[x].v.tot[1] - 1);
x = merge(x, newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)));
delnode(o), delnode(p);
}
if(w && y && t[y].v.l == t[w].v.r)
{
int o;
splitk(y, o, y, 1);
t[w].v.len += t[o].v.len, t[w].v.minlen[t[w].v.val] += t[o].v.len, t[w].v.sum[t[w].v.val] += t[o].v.len;
delnode(o);
}
if(z && !u && y && t[y].v.r == t[z].v.l)
{
int o, p;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
splitk(z, p, z, 1);
z = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), z);
delnode(o), delnode(p);
}
if(u && y && t[y].v.r == t[u].v.l)
{
int o;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[u].v.len += t[o].v.len, t[u].v.minlen[t[u].v.val] += t[o].v.len, t[u].v.sum[t[u].v.val] += t[o].v.len;
delnode(o);
}
if(t[y].v.l == 1)splitk(y, a, y, 1);
if(t[y].v.r == 0)splitk(y, y, b, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[y].tg--;
adtg2v(t[y].v, -1);
rt = merge(merge(x, merge(w, a)), merge(y, merge(merge(b, u), z)));
return;
}
__inline void leftand(int l, int r)
{
int x, y, z, w = 0, u = 0, a = 0, b = 0;
splitx(rt, x, z, r);
splity(x, x, y, l);
if(t[y].v.l == 1)splitk(y, w, y, 1);
if(t[y].v.r == 0)splitk(y, y, u, t[y].v.tot[0] + t[y].v.tot[1] - 1);
if(t[y].v.minlen[1] == 1)del(y, 1);
if(x && !w && y && t[y].v.l == t[x].v.r)
{
int o, p;
splitk(y, o, y, 1);
splitk(x, x, p, t[x].v.tot[0] + t[x].v.tot[1] - 1);
x = merge(x, newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)));
delnode(o), delnode(p);
}
if(w && y && t[y].v.l == t[w].v.r)
{
int o;
splitk(y, o, y, 1);
t[w].v.len += t[o].v.len, t[w].v.minlen[t[w].v.val] += t[o].v.len, t[w].v.sum[t[w].v.val] += t[o].v.len;
delnode(o);
}
if(z && !u && y && t[y].v.r == t[z].v.l)
{
int o, p;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
splitk(z, p, z, 1);
z = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), z);
delnode(o), delnode(p);
}
if(u && y && t[y].v.r == t[u].v.l)
{
int o;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[u].v.len += t[o].v.len, t[u].v.minlen[t[u].v.val] += t[o].v.len, t[u].v.sum[t[u].v.val] += t[o].v.len;
delnode(o);
}
if(t[y].v.l == 1)splitk(y, a, y, 1);
if(t[y].v.r == 0)splitk(y, y, b, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[y].tg++;
adtg2v(t[y].v, 1);
rt = merge(merge(x, merge(w, a)), merge(y, merge(merge(b, u), z)));
return;
}
__inline void leftor(int l, int r)
{
int x, y, z, w = 0, u = 0, a = 0, b = 0;
splitx(rt, x, z, r);
splity(x, x, y, l);
if(t[y].v.l == 0)splitk(y, w, y, 1);
if(t[y].v.r == 1)splitk(y, y, u, t[y].v.tot[0] + t[y].v.tot[1] - 1);
if(t[y].v.minlen[0] == 1)del(y, 0);
if(x && !w && y && t[y].v.l == t[x].v.r)
{
int o, p;
splitk(y, o, y, 1);
splitk(x, x, p, t[x].v.tot[0] + t[x].v.tot[1] - 1);
x = merge(x, newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)));
delnode(o), delnode(p);
}
if(w && y && t[y].v.l == t[w].v.r)
{
int o;
splitk(y, o, y, 1);
t[w].v.len += t[o].v.len, t[w].v.minlen[t[w].v.val] += t[o].v.len, t[w].v.sum[t[w].v.val] += t[o].v.len;
delnode(o);
}
if(z && !u && y && t[y].v.r == t[z].v.l)
{
int o, p;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
splitk(z, p, z, 1);
z = merge(newnode(newval(t[o].v.len + t[p].v.len, t[o].v.val)), z);
delnode(o), delnode(p);
}
if(u && y && t[y].v.r == t[u].v.l)
{
int o;
splitk(y, y, o, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[u].v.len += t[o].v.len, t[u].v.minlen[t[u].v.val] += t[o].v.len, t[u].v.sum[t[u].v.val] += t[o].v.len;
delnode(o);
}
if(t[y].v.l == 0)splitk(y, a, y, 1);
if(t[y].v.r == 1)splitk(y, y, b, t[y].v.tot[0] + t[y].v.tot[1] - 1);
t[y].tg--;
adtg2v(t[y].v, -1);
rt = merge(merge(x, merge(w, a)), merge(y, merge(merge(b, u), z)));
return;
}
int presum(int q, int x)//查询序列上1-x中1的个数
{
if(!q)return 0;
pushdown(q);
register int llen = t[t[q].ch[0]].v.sum[0] + t[t[q].ch[0]].v.sum[1];
if(llen < x && llen + t[q].v.len >= x)return t[t[q].ch[0]].v.sum[1] + t[q].v.val * (x - llen);
if(llen < x)return presum(t[q].ch[1], x - llen - t[q].v.len) + t[t[q].ch[0]].v.sum[1] + t[q].v.val * t[q].v.len;
else return presum(t[q].ch[0], x);
}
__inline int query(int l, int r)
{
return presum(rt, r) - presum(rt, l - 1);
}
int blen[3001000], bval[3000100];
int top;
int build(int l, int r)
{
if(l > r)return 0;
if(l == r)return newnode(newval(blen[l], bval[l]));
int mid = (l + r) / 2;
int res = newnode(newval(blen[mid], bval[mid]));
t[res].ch[0] = build(l, mid - 1);
t[res].ch[1] = build(mid + 1, r);
update(res);
return res;
}
signed main()
{
n = read(), m = read();
for(int i = 1;i <= n;i++)a[i] = read();
int len = 1, val = a[1];
for(int i = 2;i <= n;i++)
{
if(val != a[i])
{
top++;
blen[top] = len, bval[top] = val;
val = a[i], len = 0;
}
len++;
}
top++;
blen[top] = len, bval[top] = val;
rt = build(1, top);
int lastans = 0;
while(m--)
{
int op, l, r;
op = read(), l = read(), r = read();
l ^= lastans, r ^= lastans;
if(op == 1)assign(l, r, 0);
if(op == 2)assign(l, r, 1);
if(op == 3)rightor(l, r);
if(op == 4)leftor(l, r);
if(op == 5)rightand(l, r);
if(op == 6)leftand(l, r);
if(op == 7)writeln(lastans = query(l, r));
}
flush();
return 0;
}
2025/4/30 爆肝15k代码,然后成功获得0分好成绩,明天再调。
一些hack:
10 10
1 1 1 1 1 0 0 1 1 0
3 6 7
4 1 4
3 6 8
3 4 6
1 2 5
7 1 6
1 5 5
4 1 7
3 8 9
1 3 5
10 10
1 0 0 1 1 0 1 1 0 1
3 1 10
6 1 4
3 3 4
7 8 8
7 8 10
7 5 10
3 8 9
2 2 3
2 8 9
3 6 7
10 10
0 1 0 1 1 1 0 1 0 1
7 8 10
4 5 7
6 3 10
5 3 10
7 3 10
7 1 10
7 2 7
3 5 7
7 4 4
4 9 10
2025/5/1 23:26 发现问题:当相邻两个连续段颜色相同时,应该分给 y 还是另一个,需要考虑好逻辑。同时记得 delnode(o)。
2025/5/2 14:05 过了。
https://www.luogu.com.cn/record/215660499