ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU7059 Counting Stars

2021-08-12 21:31:31  阅读:213  来源: 互联网

标签:oth now const ll HDU7059 ret Stars return Counting


传送门


这题贼简单,结果线段树竟然写错了,奇耻大辱。


由题意得,每一个数的'1'的个数只减不增,那么最多只会改31次,因此对于删除lowbit的操作,可以暴力修改,时间复杂度\(O(nlog^2n)\)。

而对于第二种操作,只是相当于把最高位的'1'往高挪了一位,那么用线段树维护区间最高位的和,以及向左移动多少位的标记。区间修改的时候就将标记+1,并且区间和加上最高位的和,最高位的和再乘以2即可。

#include<bits/stdc++.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
const int maxn = 1e5 + 5;
const ll mod = 998244353;
const int N = 30;
In ll read() {ll x; scanf("%lld", &x); return x;}
In void write(ll x) {printf("%lld", x);}

ll q2[maxn];
In ll ADD(const ll& a, const ll& b) {return a + b < mod ? a + b : a + b - mod;}

int n, Q;
ll a[maxn];

struct Tree
{
	int l, r;
	ll sum, rsum, lzy;
	bool lft;
	In Tree operator + (const Tree& oth)const
	{
		Tree ret; ret.l = l, ret.r = oth.r;
		ret.sum = ADD(sum, oth.sum);
		ret.rsum = ADD(rsum, oth.rsum);
		ret.lzy = 0;
		ret.lft = lft | oth.lft;
		return ret;
	}
}t[maxn << 2];
In void build(int L, int R, int now)
{
	t[now].l = L, t[now].r = R;
	if(L == R)
	{
		a[L] = read();
		t[now].sum = a[L] % mod, t[now].lzy = 0;
		t[now].rsum = 0;
		if(a[L]) t[now].lft = 1;
		for(int i = N; i >= 0; --i)
			if((a[L] >> i) & 1) 
			{
				t[now].rsum = (1 << i) % mod;
				break;
			}
		return;
	}
	int mid = (L + R) >> 1;
	build(L, mid, now << 1), build(mid + 1, R, now << 1 | 1);
	t[now] = t[now << 1] + t[now << 1 | 1];
}
In void Change(int now, int d)
{
	ll tp = q2[d];
	t[now].sum = ADD(t[now].sum, t[now].rsum * (tp - 1 + mod) % mod);
	t[now].rsum = t[now].rsum * tp % mod;
	t[now].lzy += d;
}
In void pushdown(int now)
{
	if(t[now].lzy)
	{
		Change(now << 1, t[now].lzy), Change(now << 1 | 1, t[now].lzy);
		t[now].lzy = 0;
	}
}
In void update_low(int L, int R, int now)
{
	if(!t[now].lft) return;		//整个区间都是0了 
	if(t[now].l == t[now].r)	//我竟然写成L == R,而且整场比赛没看出来 
	{
		ll tp = a[L] & (-a[L]);
		if(tp == a[L])
		{
			t[now].rsum = t[now].sum = 0;
			t[now].lzy = t[now].lft = 0;
		}
		else
		{
			t[now].sum = ADD(t[now].sum, mod- tp % mod);
			a[L] -= tp;
		}
		return;
	}
	pushdown(now);
	int mid = (t[now].l + t[now].r) >> 1;
	if(R <= mid) update_low(L, R, now << 1);
	else if(L > mid) update_low(L, R, now << 1 | 1);
	else update_low(L, mid, now << 1), update_low(mid + 1, R, now << 1 | 1);
	t[now] = t[now << 1] + t[now << 1 | 1];
}
In void update_hig(int L, int R, int now)
{
	if(t[now].l == L && t[now].r == R)
	{
		Change(now, 1);
		return;
	}
	pushdown(now);
	int mid = (t[now].l + t[now].r) >> 1;
	if(R <= mid) update_hig(L, R, now << 1);
	else if(L > mid) update_hig(L, R, now << 1 | 1);
	else update_hig(L, mid, now << 1), update_hig(mid + 1, R, now << 1 | 1);
	t[now] = t[now << 1] + t[now << 1 | 1];
}
In ll query(int L, int R, int now)
{
	if(t[now].l == L && t[now].r == R) return t[now].sum;
	pushdown(now);
	int mid = (t[now].l + t[now].r) >> 1;
	if(R <= mid) return query(L, R, now << 1);
	else if(L > mid) return query(L, R, now << 1 |1);
	else return ADD(query(L, mid, now << 1), query(mid + 1, R, now << 1 | 1));
}

int main()
{
	int T = read();
	q2[0] = 1;
	for(int i = 1; i < maxn; ++i) q2[i] = q2[i - 1] * 2 % mod;
	while(T--)
	{
		n = read();
		build(1, n, 1);
		Q = read();
		for(int i = 1; i <= Q; ++i)
		{
			int op = read(), L = read(), R = read();
			if(L > R) swap(L, R);
			if(op == 1) write(query(L, R, 1)), enter;
			else if(op == 2) update_low(L, R, 1);
			else update_hig(L, R, 1);
		}
	}
	return 0;
}

标签:oth,now,const,ll,HDU7059,ret,Stars,return,Counting
来源: https://www.cnblogs.com/mrclr/p/15134965.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有