#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#define int long long
using namespace std;
#define ll (o*2,L,m)
#define rr (o*2+1,m+1,R)
const int maxn = 1e5 + 5;
struct segment_tree
{
int addv[4*maxn];
int sumv[4 * maxn];
int y1, y2; int v;
int n;
int* arr; int _sum;
void dfs(int o, int L, int R)
{
if (L == R)addv[o] = sumv[o] = arr[L]; //addv
else
{
int m = L + (R - L) / 2;
dfs ll; dfs rr;
sumv[o] = sumv[o * 2] + sumv[o * 2 + 1];
}
}
void build(int n, int* a)
{
arr = a; this->n = n;
dfs(1, 1, n);
}
void maintain(int o, int L, int R)
{
int m = L + (R - L) / 2;
sumv[o] = 0;
if (R > L)sumv[o] = sumv[o * 2] + sumv[o * 2 + 1];
sumv[o] += addv[o] * (R - L + 1);
}
void pushdown(int o, int L, int R)
{
int lc = o * 2; int rc = lc + 1;
addv[lc] += addv[o];
addv[rc] += addv[o];
addv[o] = 0;
}
void update(int o, int L, int R)
{
if (y1 <= L && R <= y2)
{
addv[o] += v;
}
else
{
int m = L + (R - L) / 2;
pushdown(o, L, R);
if (y1 <= m)update ll; else maintain ll; //else
if (y2 > m)update rr; else maintain rr;
}
maintain(o, L, R);
}
void query(int o, int L, int R)
{
if (y1 <= L && R <= y2)
{
_sum += sumv[o]; return;
}
else
{
int m = L + (R - L) / 2;
pushdown(o, L, R);
maintain ll; maintain rr; //先maintain
if (y1 <= m)query ll;
if (y2 > m)query rr;
}
}
void interval_add(int a, int b, int v)
{
y1 = a; y2 = min(b,n); this->v = v;//这边y1不能像y2一样处理,
//否则可能本来不该加的最后却给第n个元素加了
if (y1 > n)return;//小心超出范围,那样线段树会出问题的
update(1, 1, n);
}
int interval_sum(int a, int b)
{
_sum = 0; y1 = a; y2 = b;
query(1, 1, n);
return _sum;
}
}t;
int arr[maxn];
signed main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)cin >> arr[i];
for (int i = n; i >= 1; i--)arr[i] = arr[i] - arr[i - 1];
t.build(n, arr);
for (int i = 0; i < m; i++)
{
int op; cin >> op;
if (op == 1)
{
int l, r, k, d; cin >> l >> r >> k >> d;
t.interval_add(l, l, k - d);
t.interval_add(l, r, d);
t.interval_add(r + 1, r + 1, -(k + (r-l) * d));
}
else
{
int p; cin >> p;
int ret=t.interval_sum(1, p);
cout << ret << endl;
}
}
return 0;
}
P1438 无聊的数列
最新推荐文章于 2024-08-25 18:17:58 发布