\(\text{Problem}\)

维护一个序列

支持插入一个数,区间加,询问区间平方和

\(\text{Solution}\)

平衡树很模板的题了

考场打 \(fhq-treap\) 毫无悬念过了

读入有负数,快读注意!

打完之后发现有模数?

狂改代码,无脑乱加模,代码直接丑了

\(\text{Code}\)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std; const int N = 1e5, P = 7459;
int n, m, rt; struct node{
int ls, rs, rnd, sz, val, s1, s2, tg;
}tr[N * 2 + 5]; void read(int &x)
{
x = 0; int f = 1; char ch = getchar();
while (!isdigit(ch)) f = (ch == '-' ? -1 : f), ch = getchar();
while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
x *= f;
} void update(int p)
{
tr[p].sz = tr[tr[p].ls].sz + tr[tr[p].rs].sz + 1;
tr[p].s1 = (tr[tr[p].ls].s1 + tr[tr[p].rs].s1 + tr[p].val) % P;
tr[p].s2 = (tr[tr[p].ls].s2 + tr[tr[p].rs].s2 + tr[p].val * tr[p].val % P) % P;
} int new_node(int v)
{
static int tot = 0;
tr[++tot] = node{0, 0, rand(), 1, v, v, v * v % P, 0};
return tot;
} void pushdown(int p)
{
if (!p || !tr[p].tg) return;
long long v = tr[p].tg;
if (tr[p].ls)
{
tr[tr[p].ls].s2 += v * v % P * tr[tr[p].ls].sz % P + v * 2 * tr[tr[p].ls].s1 % P;
tr[tr[p].ls].s1 += v * tr[tr[p].ls].sz % P, tr[tr[p].ls].tg += v, tr[tr[p].ls].val += v;
tr[tr[p].ls].s2 %= P, tr[tr[p].ls].s1 %= P, tr[tr[p].ls].tg %= P, tr[tr[p].ls].val %= P;
}
if (tr[p].rs)
{
tr[tr[p].rs].s2 += v * v % P * tr[tr[p].rs].sz % P + v * 2 * tr[tr[p].rs].s1 % P;
tr[tr[p].rs].s1 += v * tr[tr[p].rs].sz % P, tr[tr[p].rs].tg += v, tr[tr[p].rs].val += v;
tr[tr[p].rs].s2 %= P, tr[tr[p].rs].s1 %= P, tr[tr[p].rs].tg %= P, tr[tr[p].rs].val %= P;
}
tr[p].tg = 0;
} void split(int p, int k, int &x, int &y)
{
if (!p) x = y = 0;
else{
pushdown(p);
if (k <= tr[tr[p].ls].sz) y = p, split(tr[p].ls, k, x, tr[p].ls);
else x = p, split(tr[p].rs, k - tr[tr[p].ls].sz - 1, tr[p].rs, y);
update(p);
}
} int merge(int x, int y)
{
if (!x || !y) return x | y;
pushdown(x), pushdown(y);
if (tr[x].rnd < tr[y].rnd)
{
tr[x].rs = merge(tr[x].rs, y);
update(x); return x;
}
else{
tr[y].ls = merge(x, tr[y].ls);
update(y); return y;
}
} inline void insert(int x, int y)
{
int a, b;
x = (x + P) % P, split(rt, y - 1, a, b);
rt = merge(merge(a, new_node(x)), b);
} void add(int l, int r, long long x)
{
int a, b, c, d;
x = (x + P) % P, split(rt, r, a, b), split(a, l - 1, c, d);
tr[d].s2 += x * x % P * tr[d].sz % P + x * 2 * tr[d].s1 % P;
tr[d].s1 += x * tr[d].sz % P, tr[d].tg += x, tr[d].val += x;
tr[d].s2 %= P, tr[d].s1 %= P, tr[d].tg %= P, tr[d].val %= P;
rt = merge(merge(c, d), b);
} int query(int l, int r)
{
int a, b, c, d;
split(rt, r, a, b), split(a, l - 1, c, d);
int res = tr[d].s2;
rt = merge(merge(c, d), b);
return res;
} int main()
{
srand(time(0)), read(n);
char op[10]; int l, r, x;
for(int i = 1; i <= n; i++) read(x), insert(x, i);
read(m);
for(int i = 1; i <= m; i++)
{
scanf("%s", op), read(l), read(r);
if (op[0] == 'I') insert(r, l);
else if (op[0] == 'A') read(x), add(l, r, x);
else printf("%d\n", query(l, r));
}
}

最新文章

  1. HDU 5451 广义斐波那契数列
  2. The new Portable Class Library for SQLite z
  3. 安装Ubuntu服务器
  4. debug模式启动provider
  5. SET ANSI_NULLS (Transact-SQL)
  6. 学习PHP时的一些总结(四)
  7. angular $cookies、$cookieStore
  8. 【重要】使用Git命令行上传到GitHub上
  9. T5大牛带你解析:如何实现分布式技术
  10. python复习2
  11. React Native组件、生命周期及属性传值props详解
  12. 一、ESP8266入门(基于LUA开发)
  13. 通过 txt 文件批量导入需要批量处理的数据的标识字段
  14. OutOfMemoryError 到底能不能被捕获?
  15. 深入理解Java虚拟机--阅读笔记三
  16. elasticsearch client 为空 错误信息:java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecut‌​or()Ljava/util/concu‌​rrent/Executor
  17. 【centos】centos安装配置samba
  18. Beta 冲刺 四
  19. 在Oracle电子商务套件版本12.2中创建自定义应用程序(文档ID 1577707.1)
  20. 在vultr中安装coreos

热门文章

  1. whylogs工具库的工业实践!机器学习模型流程与效果监控 ⛵
  2. Spring Security(7)
  3. 过压保护芯片,高输入电压(OVP)
  4. CSS中和颜色及渐变
  5. Qt VideoMeeting_Intercom师生对讲开发中实际上遇到的一些问题,终于结项了,也照例写一下总结吧。
  6. week_5
  7. JavaScript:七大基础数据类型:大整数bigint
  8. [seaborn] seaborn学习笔记5-小提琴图VIOLINPLOT
  9. C#开发的插件程序 - 开源研究系列文章
  10. python之路41 前端页面尝试 丑出新高度