POJ 3468 A Simple Problem with Integers range of problem-solving report segment tree modification

POJ 3468 A Simple Problem with Integers range of problem-solving report segment tree modification

Problem-solving ideas: mark with a lazy update delay, in order to improve efficiency, or will tle.
Specifically, if you want to change interval is less than the current interval range, then mark this interval, go directly to the value plus. If used after its sub-interval, and then update down.
Here Insert Picture Description

#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<string.h>
#include<map>
#include<stack>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<fstream>
#include<sstream>
#include<set>
#pragma warning(disable:4996)
#define INF 0x3f3f3f3f
#define ll long long
#define PI acos(-1.0)
const int N = 100005;
const int maxn = 1e9;
using namespace std;
int n, t, m;
ll a[100005];
ll tree[400020];//tree里放区间和
ll lazy[400020];
void pushdown(int id, int l, int r)
{
	if (lazy[id])
	{
		lazy[id << 1] += lazy[id];
		lazy[id << 1 | 1] += lazy[id];
		tree[id << 1] += lazy[id] * l;
		tree[id << 1 | 1] += lazy[id] * r;
		lazy[id] = 0;
	}
}
void build(int id, int l, int r)
{
	lazy[id] = 0;
	if (l == r)
	{
		tree[id] = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(id << 1, l, mid);
	build(id << 1 | 1, mid + 1, r);
	tree[id] = tree[id << 1] + tree[id << 1 | 1];
}
ll int query(int id, int l, int r, int x, int y)
{
	if (l > r || x > r || y < l)
		return 0;
	if (x <= l && y >= r)
	{
		return tree[id];
	}
	int mid = (l + r) >> 1;
	pushdown(id, mid - l + 1, r - mid);
	ll sum = 0;
	if (l <= mid)
		sum += query(id << 1, l, mid, x, y);
	if (r > mid)
		sum += query(id << 1 | 1, mid + 1, r, x, y);
	return sum;
}
void Add(int id, int l, int r, int x, int y, int z)
{
	if (l > r || x > r || y < l)
		return;
	if (l >= x && r <= y)
	{
		tree[id] += z * (r - l + 1);
		lazy[id] += z;
		return;
	}
	int mid = (l + r) >> 1;
	pushdown(id, mid - l + 1, r - mid);
	if (l <= mid)
		Add(id << 1, l, mid, x, y, z);
	if (r > mid)
		Add(id << 1 | 1, mid + 1, r, x, y, z);
	tree[id] = tree[id << 1] + tree[id << 1 | 1];
}
int main()
{
	scanf("%d%d", &n, &m);
	memset(a, 0, sizeof(a));
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", &a[i]);
	}
	build(1, 1, n);
	char ch[10] = "";
	while (m--)
	{
		scanf("%s", ch);
		if (strcmp(ch, "Q") == 0)
		{
			int x, y;
			scanf("%d%d", &x, &y);
			printf("%lld\n", query(1, 1, n, x, y));
		}
		else if (strcmp(ch, "C") == 0)
		{
			int x, y, z;
			scanf("%d%d%d", &x, &y, &z);
			Add(1, 1, n, x, y, z);

		}
		memset(ch, 0, sizeof(0));
	}
}


Published 64 original articles · won praise 0 · Views 1459

Guess you like

Origin blog.csdn.net/weixin_45566331/article/details/104544624