求调线段树 2,悬赏 3 RMB
查看原帖
求调线段树 2,悬赏 3 RMB
781046
tai_chi楼主2023/8/1 15:34

调一天了,能帮忙调出来的私信本人。

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
#define inf 0x3f3f3f3f

using namespace std;

#define pii pair<int, int>
#define pll pair<ll, ll>

#define endl '\n'
#define IOS                     \
	ios::sync_with_stdio(NULL); \
	cin.tie(NULL);              \
	cout.tie(NULL)
#define qwq cout << "qwq" << endl
#define line cout << "------------------" << endl
#define int long long
const int maxn = 1e5 + 5;
int a[maxn];
int n, q, mod;

struct node
{
	int tagt, taga, w, l, r;
	// tagt 是乘法标记,taga 是加法标记
};
node t[maxn * 4];

void pushup(int u)
{
	t[u].w = (t[u * 2].w + t[u * 2 + 1].w) % mod;
}

void build(int u, int l, int r)
{
	t[u].l = l;
	t[u].r = r;
	t[u].tagt = 1;
	if (l == r)
	{
		t[u].w = a[l];
		return;
	}
	int mid = (l + r) / 2;
	build(u * 2, l, mid);
	build(u * 2 + 1, mid + 1, r);
	pushup(u);
}

bool inr(int l, int r, int L, int R) // in range
{
	return (L <= l) && (r <= R);
}
bool otr(int l, int r, int L, int R) // out of range
{
	return (r < L) || (R < l);
}

void maketag(int u, int tagt, int taga)
{
	t[u].w = (t[u].w * tagt) % mod;
	t[u].w = (t[u].w + taga * (t[u].l - t[u].r + 1)) % mod;
	t[u].taga = (t[u].taga * tagt) % mod;
	t[u].taga = (t[u].taga + taga) % mod;
	t[u].tagt = (t[u].tagt * tagt) % mod;
}

void pushdown(int u)
{
	maketag(u * 2, t[u].tagt, t[u].taga);
	maketag(u * 2 + 1, t[u].tagt, t[u].taga);
	t[u].tagt = 1;
	t[u].taga = 0;
}

void add(int u, int L, int R, int k)
{
	if (inr(t[u].l, t[u].r, L, R))
	{
		maketag(u, 1, k);
	}
	else if (!otr(t[u].l, t[u].r, L, R))
	{
		pushdown(u);
		add(u * 2, L, R, k);
		add(u * 2 + 1, L, R, k);
		pushup(u);
	}
}

void tim(int u, int L, int R, int k) // times
{
	if (inr(t[u].l, t[u].r, L, R))
	{
		maketag(u, k, 0);
	}
	else if (!otr(t[u].l, t[u].r, L, R))
	{
		pushdown(u);
		tim(u * 2, L, R, k);
		tim(u * 2 + 1, L, R, k);
		pushup(u);
	}
}

int ask(int u, int L, int R)
{
	if (inr(t[u].l, t[u].r, L, R))
	{
		return t[u].w % mod;
	}
	else if (!otr(t[u].l, t[u].r, L, R))
	{
		pushdown(u);
		return (ask(u * 2, L, R) + ask(u * 2 + 1, L, R)) % mod;
	}
	return 0;
}

void print() // debug
{
	for (int i = 1; i <= n * 4; i++)
	{
		cout << t[i].l << " " << t[i].r << " " << t[i].w << " " << t[i].taga << " " << t[i].tagt << endl;
	}
}

signed main()
{
	cin >> n >> q >> mod;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	build(1, 1, n);
	while (q--)
	{
		int opt, x, y, k;
		cin >> opt >> x >> y;
		if (opt == 1)
		{
			cin >> k;
			tim(1, x, y, k);
		}
		else if (opt == 2)
		{
			cin >> k;
			add(1, x, y, k);
		}
		else
		{
			cout << ask(1, x, y) << endl;
		}
	}
	return 0;
}

2023/8/1 15:34
加载中...