您的位置:首页 > 其它

4785: [Zjoi2017]树状数组

2017-04-04 19:04 176 查看

4785: [Zjoi2017]树状数组

Time Limit: 40 Sec  Memory Limit: 512 MB
Submit: 102  Solved: 73

[Submit][Status][Discuss]

Description

 漆黑的晚上,九条可怜躺在床上辗转反侧。难以入眠的她想起了若干年前她的一次悲惨的OI 比赛经历。那是一道
基础的树状数组题。给出一个长度为 n 的数组 A,初始值都为 0,接下来进行 m 次操作,操作有两种:
1 x,表示将 Ax 变成 (Ax + 1) mod 2。
2 l r,表示询问 sigma(Ai) mod 2,L<=i<=r
尽管那个时候的可怜非常的 simple,但是她还是发现这题可以用树状数组做。当时非常young 的她写了如下的算
法:
1: function Add(x)
2: while x > 0 do
3: A
x ← (Ax + 1) mod 2
4: x ← x ? lowbit(x)
5: end while
6: end function
7:
8: function Find(x)
9: if x == 0 then
10: return 0
11: end if
12: ans ← 0
13: while x ≤ n do
14: ans ← (ans + Ax) mod 2
15: x ← x + lowbit(x)
16: end while
17: return ans
18: end function
19:
20: function Query(l, r)
21: ansl ← Find(l ? 1)
22: ansr ← Find(r)
23: return (ansr ? ansl + 2) mod 2
24: end function

其中 lowbit(x) 表示数字 x 最?的非 0 二进制位,例如 lowbit(5) = 1, lowbit(12) = 4。进行第一类操作的时
候就调用 Add(x),第二类操作的时候答案就是 Query(l, r)。如果你对树状数组比较熟悉,不难发现可怜把树状
数组写错了: Add和Find 中 x 变化的方向反了。因此这个程序在最终测试时华丽的爆 0 了。然而奇怪的是,在
当时,这个程序通过了出题人给出的大样例——这也是可怜没有进行对拍的原因。现在,可怜想要算一下,这个程
序回答对每一个询问的概率是多少,这样她就可以再次的感受到自己是一个多么非的人了。然而时间已经过去了很
多年,即使是可怜也没有办法完全回忆起当时的大样例。幸运的是,她回忆起了大部分内容,唯一遗忘的是每一次
第一类操作的 x的值,因此她假定这次操作的 x 是在 [li, ri] 范围内 等概率随机 的。具体来说,可怜给出了
一个长度为 n 的数组 A,初始为 0,接下来进行了 m 次操作:
1 l r,表示在区间 [l, r] 中等概率选取一个 x 并执行 Add(x)。
2 l r,表示询问执行 Query(l, r) 得到的结果是正确的概率是多少。

Input

第一行输入两个整数 n, m。
接下来 m 行每行描述一个操作,格式如题目中所示。
N<=10^5,m<=10^5,1<=L<=R<=N

Output

对于每组询问,输出一个整数表示答案。如果答案化为最简分数后形如 x/y

,那么你只需要输出 x*y^?1 mod 998244353 后的值。(即输出答案模 998244353)。

Sample Input

5 5

1 3 3

2 3 5

2 4 5

1 1 3

2 2 5

Sample Output

1

0

665496236

//在进行完 Add(3) 之后, A 数组变成了 [0, 1, 1, 0, 0]。所以前两次询问可怜的程序答案都是

1,因此第一次询问可怜一定正确,第二次询问可怜一定错误。

HINT

Source



[Submit][Status][Discuss]

由树状数组的性质,,可以发现
当修改和询问的操作倒过来的时候,每次询问返回的东西其实是个后缀和
也就是调用函数Query(l,r)时,返回的其实是[l-1,r-1]的数值的和
因此,对于每个询问,当曾经某一次修改了位置l-1或者r的时候,才会导致这个询问的结果出现偏差
于是可以枚举每个修改,再枚举后面的询问,判断一下出错的概率,O(m^2)解决这个问题
更进一步,每个修改操作等价于是对符合一定限制的区间有某个概率使其出错
因此就转变为二维数点问题,可以使用CDQ + 树状数组或是二维线段树解决
使用二维线段树的话比较好写,而且可以在线解答
特别地,对于询问[1,k],由于调用函数的位置是0和k,导致了第一次函数调用变成多余的
此时,对于每个修改操作,当且仅当其修改位置k的时候不影响答案,其它都会干扰答案,特判即可
对于概率,维护奇偶性简单讨论就行了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int N = 4;
const int T = 14;
const int maxn = 1E5 + 10;
typedef long long LL;
const LL mo = 998244353;

inline int Mul(const LL &x,const LL &y) {return x * y % mo;}
inline int Add(const int &x,const int &y) {return x + y < mo ? x + y : x + y - mo;}
inline int Dec(const int &x,const int &y) {return x - y >= 0 ? x - y : x - y + mo;}

inline int ksm(int x,int y)
{
int ret = 1;
for (; y; y >>= 1)
{
if (y & 1) ret = Mul(ret,x);
x = Mul(x,x);
}
return ret;
}

struct data{
int e,o; data(){}
data(int e,int o): e(e),o(o){}
data operator ^ (const data &B)
{
data c;
c.e = Add(Mul(e,B.e),Mul(o,B.o));
c.o = Add(Mul(e,B.o),Mul(o,B.e));
return c;
}
}d1,d2,Ans,c1[maxn*T*T],c2[maxn*T*T];

int n,m,cnt,All,rt[maxn*N],lc[maxn*T*T],rc[maxn*T*T];
bool vis[maxn*N];

inline void New(int &o)
{
o = ++cnt;
c1[o] = c2[o] = data(1,0);
}

inline void Insert(int &o,int l,int r,int pos)
{
if (!o) New(o);
c1[o] = c1[o] ^ d1; c2[o] = c2[o] ^ d2;
if (l == r) return; int mid = l + r >> 1;
if (pos <= mid) Insert(lc[o],l,mid,pos);
else Insert(rc[o],mid+1,r,pos);
}

inline void Modify(int o,int l,int r,int ql,int qr)
{
vis[o] = 1; Insert(rt[o],1,n,qr);
if (l == r) return; int mid = l + r >> 1;
if (ql <= mid) Modify(o<<1,l,mid,ql,qr);
else Modify(o<<1|1,mid+1,r,ql,qr);
}

inline void query(int o,int l,int r,int ql,int qr,int typ)
{
if (!o) return;
if (ql <= l && r <= qr)
{
Ans = Ans ^ (typ == 1 ? c1[o] : c2[o]);
return;
}
int mid = l + r >> 1;
if (ql <= mid) query(lc[o],l,mid,ql,qr,typ);
if (qr > mid) query(rc[o],mid+1,r,ql,qr,typ);
}

inline void Query(int o,int l,int r,int l1,int r1,int l2,int r2,int typ)
{
if (!vis[o]) return;
if (l1 <= l && r <= r1)
{
query(rt[o],1,n,l2,r2,typ); return;
}
int mid = l + r >> 1;
if (l1 <= mid) Query(o<<1,l,mid,l1,r1,l2,r2,typ);
if (r1 > mid) Query(o<<1|1,mid+1,r,l1,r1,l2,r2,typ);
}

int getint()
{
char ch = getchar(); int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret * 10 + ch - '0',ch = getchar();
return ret;
}

int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
freopen("test.txt","w",stdout);
#endif

n = getint(); m = getint();
while (m--)
{
int typ = getint(),l,r;
l = getint(); r = getint();
if (typ == 1)
{
int Inv = ksm(r - l + 1,mo - 2); ++All;
d1 = data(Dec(1,Inv),Inv); Inv = Mul(2,Inv);
d2 = data(Dec(1,Inv),Inv); Modify(1,1,n,l,r);
}
else
{
--l; Ans = data(1,0);
if (!l)
{
Query(1,1,n,1,r,r,n,1);
if (All & 1) Ans.e = Ans.o;
}
else
{
Query(1,1,n,1,l,r,n,2);
Query(1,1,n,1,l,l,r - 1,1);
Query(1,1,n,l + 1,r,r,n,1);
}
printf("%d\n",Ans.e);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: