Counting Stars
官方题解是这样的
题目类似于求根号的和,就是对于第一种操作每一个数最多减少lon(a[i])次,那么总体用线段树单点修改的话就只要
修改n*lon(a[i])^2次,对于第二种操作,只对最高位有用,所以可以考虑开两个线段树来维护最高和除了最高位的其他数,最高位可以
区间修改,最低为位单点修改。
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define ll long long int
#define ms(a, b) memset(a, b, sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define se second
#define ull unsigned long long
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define endl "\n"
#define bug cout << "----acac----" << endl;
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
ll ksm(ll a, ll b, ll mod)
{
ll ans = 1;
while (b)
{
if (b & 1)
{
ans = (a % mod * ans % mod) % mod;
}
b >>= 1;
a = (a % mod * a % mod) % mod;
}
return ans;
}
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a % b);
}
const ll mod = 998244353;
const int maxn = 2e5 + 10;
const int maxm = 2e3 + 50;
const double eps = 1e-8;
const ll inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);
ll b[maxn], c[maxn];
int n;
struct node
{
int l, r;
ll lazy;
ll sum;
} a[maxn << 2][3];
void push_up(int rt)
{
a[rt][1].sum = (a[lson][1].sum + a[rson][1].sum) % mod;
a[rt][2].sum = (a[lson][2].sum + a[rson][2].sum) % mod;
}
void push_down(int rt)
{
if (a[rt][2].lazy == 1)
return;
a[lson][2].lazy = a[lson][2].lazy * a[rt][2].lazy % mod;
a[rson][2].lazy = a[rson][2].lazy * a[rt][2].lazy % mod;
a[lson][2].sum = a[lson][2].sum * a[rt][2].lazy % mod;
a[rson][2].sum = a[rson][2].sum * a[rt][2].lazy % mod;
a[rt][2].lazy = 1;
}
void build(int l, int r, int rt)
{
a[rt][1].l = a[rt][2].l = l;
a[rt][1].r = a[rt][2].r = r;
a[rt][1].lazy = a[rt][2].lazy = 1;
if (l == r)
{
a[rt][1].sum = b[l]; //1表示低位
a[rt][2].sum = c[l]; //2表示最高位
return;
}
int mid = (l + r) >> 1;
build(l, mid, lson);
build(mid + 1, r, rson);
push_up(rt);
}
void update(int l, int r, int op, int rt)
{
if (a[rt][2].sum == 0)
return;
if (op == 2 && a[rt][1].l == a[rt][1].r)
{
if (a[rt][1].sum == 0)
{
a[rt][2].sum = 0;
}
else
{
a[rt][1].sum -= lowbit(a[rt][1].sum);
}
return;
}
if(op==3)
{
if(l<=a[rt][1].l&&a[rt][1].r<=r)
{
a[rt][2].sum = a[rt][2].sum * 2 % mod;
a[rt][2].lazy = a[rt][2].lazy * 2 % mod;
return;
}
}
push_down(rt);
int mid = (a[rt][1].l + a[rt][1].r) >> 1;
if (l <= mid)
update(l, r, op, lson);
if (r > mid)
update(l, r, op, rson);
push_up(rt);
}
ll query(int l,int r,int rt)
{
if(a[rt][1].l>=l&&r>=a[rt][1].r)
{
return (a[rt][1].sum + a[rt][2].sum)%mod;
}
ll ans = 0;
push_down(rt);
int mid = (a[rt][1].l + a[rt][1].r) >> 1;
if(l<=mid)
{
ans = (ans + query(l, r, lson)) % mod;
}
if(r>mid)
{
ans = (ans + query(l, r, rson)) % mod;
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &b[i]);
for (int j = 30; j >= 0; j--)
{
if (((b[i] >> j) & 1))
{
c[i] = (1 << j);
break;
}
}
b[i] -= c[i];
}
build(1, n, 1);
int m;
scanf("%d", &m);
while(m--)
{
int op, l, r;
scanf("%d%d%d", &op, &l, &r);
if(op==1)
{
printf("%lld\n", query(l, r, 1));
}
else
{
update(l, r, op, 1);
}
}
}
return 0;
}