题意:
首先给出n, m, q, 一个长度为n的数组,m次修改操作,每次修改操作,l, r, x, 对l, r区间里面的数加上k, q次询问操作,每次询问操作,问
\(\sum\limits_{i = lk}^{rk}\sum\limits_{j = xk}^{yk}a_{i, j}^2\)
思路:
等过段时间在写,先贴代码,提醒下线段树维护矩阵
总结:
线段树维护矩阵元素,重载运算符,细节写法
点击查看代码
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define endl '\n'
#define ls rt << 1
#define rs rt << 1 | 1
#define lson ls, l, mid
#define rson rs, mid + 1, r
using namespace std;
typedef long long ll;
const ll MAXN = 1E5 + 10, MOD = 1e9 + 7;
ll n, m, q;
ll a[MAXN], ans[MAXN];
struct Mat {
ll v[4][4];
Mat() { memset(v, 0, sizeof(v)); }
Mat(ll k) {
v[0][0] = v[1][1] = v[2][2] = v[3][3] = v[2][3] = 1;
v[0][1] = k;
v[0][2] = v[0][3] = k * k % MOD;
v[1][2] = v[1][3] = 2 * k % MOD;
v[1][0] = v[2][0] = v[2][1] = v[3][0] = v[3][1] = v[3][2] = 0;
}
void init()
{
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
v[i][j] = (i == j ? 1 : 0);
}
ll* operator [] (int x) { return v[x]; }
const ll* operator [] (int x) const { return v[x]; }
Mat operator * (const Mat& b)
{
const Mat& a = *this;
Mat res;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
for (int k = 0; k < 4; ++k)
res.v[i][j] = (res.v[i][j] + a.v[i][k] * b.v[k][j]) % MOD;
return res;
}
};
struct Node {
int val[4];
Node() { memset(val, 0, sizeof val); }
Node(ll x) { val[0] = 1, val[1] = x, val[2] = val[3] = x * x % MOD; }
Node operator + (Node b)
{
Node res;
for (int i = 0; i < 4; ++i)
res.val[i] = (val[i] + b.val[i]) % MOD;
return res;
}
Node operator * (Mat b)
{
ll res[4] = { 0 };
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
res[j] = (res[j] + val[i] * b.v[i][j]) % MOD;
for (int i = 0; i < 4; ++i)
val[i] = res[i];
return *this;
}
} tree[MAXN << 2];
Mat lazy[MAXN << 2];
inline void push_up(int rt)
{
tree[rt] = tree[ls] + tree[rs];
}
inline void push(int rt, Mat k)
{
tree[rt] = tree[rt] * k, lazy[rt] = lazy[rt] * k;
}
inline void push_down(int rt) {
push(ls, lazy[rt]), push(rs, lazy[rt]);
lazy[rt].init();
return;
}
void build(int rt, int l, int r)
{
lazy[rt].init();
if (l == r)
{
tree[rt] = Node(a[l]);
return;
}
int mid = l + r >> 1;
build(lson), build(rson);
push_up(rt);
}
void update(int rt, int l, int r, int L, int R, ll val)
{
if (r < L || l > R || (l >= L && r <= R)) //这里直接少调用了两个函数
return push(rt, Mat((l >= L && r <= R) * val));
push_down(rt);
int mid = l + r >> 1;
update(lson, L, R, val), update(rson, L, R, val);
push_up(rt);
}
Node query(int rt, int l, int r, int x, int y)
{
if (x <= l && r <= y)
return tree[rt];
push_down(rt);
int mid = l + r >> 1;
Node ans;
if (x <= mid)
ans = (ans + query(ls, l, mid, x, y));
if (y >= mid + 1)
ans = (ans + query(rs, mid + 1, r, x, y));
return ans;
}
struct modify
{
int l, r, val;
} op[MAXN];
struct query
{
int op, id, x, l, r;
const bool operator< (const query& s) const { return x < s.x; }
} que[MAXN];
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m >> q;
for (int i = 1; i <= n; i++)
cin >> a[i], a[i] = (a[i] + MOD) % MOD;
build(1, 1, n);
for (int i = 1, l, r, k; i <= m; ++i)
cin >> op[i].l >> op[i].r >> op[i].val, op[i].val = (op[i].val + MOD) % MOD;
for (int i = 1, l, r, x, y; i <= q; i++)
{
cin >> l >> r >> x >> y;
que[i * 2 - 1] = { -1, i, x - 1, l, r };
que[i * 2] = { 1, i, y, l, r };
}
sort(que + 1, que + 1 + 2 * q);
int pos = 1;
while (pos <= 2 * q && que[pos].x < 0) //如果当前的x小于0,那只可能是减的操作,但其实,根本不用考虑
++pos;
for (int i = 0; i <= m; i++)
{
if (i)
{
update(1, 1, n, op[i].l, op[i].r, op[i].val);
//update(1, 1, n, 1, op[i].l - 1, 0); //边上的两个版本也要更新,就因为调用了这两个函数,直接超时了
//update(1, 1, n, op[i].r + 1, n, 0);
}
while (pos <= 2 * q && que[pos].x == i)
ans[que[pos].id] += que[pos].op * query(1, 1, n, que[pos].l, que[pos].r).val[3] % MOD, ++pos;
}
for (int i = 1; i <= q; ++i)
cout << (ans[i] + MOD) % MOD << endl;
return 0;
}