妹子,刚学线段树,0pts全RE,求调,悬赏一关注
查看原帖
妹子,刚学线段树,0pts全RE,求调,悬赏一关注
661641
Cx114514楼主2023/6/7 21:28
#include <bits/stdc++.h>
#define int long long
using namespace std;

int read()
{
	int f = 1;
	char c = getchar();
	while (!isdigit(c))
	{
	    if (c == '-') f = -1;
	    c = getchar();
    }
	int x = 0;
	while (isdigit(c))
	{
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * f;
}

int buf[25];

void write(int x)
{
	int p = 0;
	if (x < 0)
	{
	    putchar('-');
	    x = -x;
	}
	if (x == 0) putchar('0');
	else
	{
		while (x)
		{
			buf[++p] = x % 10;
			x /= 10;
		}
		for (int i = p; i >= 1; i--)
			putchar('0' + buf[i]);
	}
}

int n, m, dep, lastans, vis[2000005], a[2000005], tree[25][2000005], size[25][2000005], tag[25][2000005];

void build(int rt, int d)
{
	if (d == dep) 
	{
		tree[d][rt] = a[rt];
		size[d][rt] = vis[rt];
	}
	else
	{
		build(rt, d + 1);
		build(rt + (1 << d), d + 1);
		tree[d][rt] = tree[d + 1][rt] + tree[d + 1][rt + (1 << d)];
		size[d][rt] = size[d + 1][rt] + size[d + 1][rt + (1 << d)];
	} 
}

void update(int rt, int d, int x, int y, int v)
{
	if (d == x)
	{
		tree[d][rt] += size[d][rt] * v;
		tag[d][rt] += v;
	}
	else
	{
		if (tag[d][rt])
		{
			tag[d + 1][rt] += tag[d][rt];
			tree[d + 1][rt] += tag[d][rt] * size[d + 1][rt];
			tag[d + 1][(rt + (1 << d))] += tag[d][rt];
			tree[d + 1][rt + (1 << d)] += tag[d][rt] * size[d + 1][rt + (1 << d)];
			tag[d][rt] = 0;
		}
		update(y % (1 << (d + 1)), d + 1, x, y, v);
		tree[d][rt] = tree[d + 1][rt] + tree[d + 1][rt + (1 << d)];
	}
}

int ask(int rt, int d, int x, int y)
{
	if (d == x) return tree[d][rt];
	if (tag[d][rt])
	{
		tag[d + 1][rt] += tag[d][rt];
		tree[d + 1][rt] += tag[d][rt] * size[d + 1][rt];
		tag[d + 1][rt + (1 << d)] += tag[d][rt];
		tree[d + 1][rt + (1 << d)] += tag[d][rt] * size[d + 1][rt + (1 << d)];
		tag[d][rt] = 0;
	}
	return ask(y % (1 << (d + 1)), d + 1, x, y);
}

signed main()
{
	n = read(), m = read();
	for (int i = 1; i <= n; i++)
	{
		a[i] = read();
		vis[i] = 1;
	}
	dep = log2(n);
	if ((1 << dep) < n) dep++;
	else 
	{
		a[0] = a[n];
		vis[n] = 0;
		vis[0] = 1;
	}
	build(0, 0);
	for (int i = 1; i <= m; i++)
	{
		int op, x, y, z;
		op = read();
		op = (lastans + op) % 2 + 1;
		if (op == 1) 
		{
			x = read(), y = read(), z = read();
			y %= (1 << x);
			update(0, 0, x, y, z);
		}
		else
		{
			x = read(), y = read();
			y %= (1 << x);
			lastans = ask(0, 0, x, y);
			write(lastans);
			putchar('\n');
		}
	}
    return 0;
}


2023/6/7 21:28
加载中...