树套树是什么啊我不知道/dk

我只知道卡常数w

// by Isaunoya
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize( \
"inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2,-ffast-math,-fsched-spec,unroll-loops,-falign-jumps,-falign-loops,-falign-labels,-fdevirtualize,-fcaller-saves,-fcrossjumping,-fthread-jumps,-funroll-loops,-freorder-blocks,-fschedule-insns,inline-functions,-ftree-tail-merge,-fschedule-insns2,-fstrict-aliasing,-fstrict-overflow,-falign-functions,-fcse-follow-jumps,-fsched-interblock,-fpartial-inlining,no-stack-protector,-freorder-functions,-findirect-inlining,-fhoist-adjacent-loads,-frerun-cse-after-loop,inline-small-functions,-finline-small-functions,-ftree-switch-conversion,-foptimize-sibling-calls,-fexpensive-optimizations,inline-functions-called-once,-fdelete-null-pointer-checks")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using ll = long long;
const int _ = 1 << 21;
struct I {
char fin[_], *p1 = fin, *p2 = fin;
inline char gc() {
return (p1 == p2) && (p2 = (p1 = fin) + fread(fin, 1, _, stdin), p1 == p2) ? EOF : *p1++;
}
inline I& operator>>(int& x) {
bool sign = 1;
char c = 0;
while (c < 48) ((c = gc()) == 45) && (sign = 0);
x = (c & 15);
while ((c = gc()) > 47) x = (x << 1) + (x << 3) + (c & 15);
x = sign ? x : -x;
return *this;
}
inline I& operator>>(double& x) {
bool sign = 1;
char c = 0;
while (c < 48) ((c = gc()) == 45) && (sign = 0);
x = (c - 48);
while ((c = gc()) > 47) x = x * 10 + (c - 48);
if (c == '.') {
double d = 1.0;
while ((c = gc()) > 47) d = d * 0.1, x = x + (d * (c - 48));
}
x = sign ? x : -x;
return *this;
}
inline I& operator>>(char& x) {
do
x = gc();
while (isspace(x));
return *this;
}
inline I& operator>>(string& s) {
s = "";
char c = gc();
while (isspace(c)) c = gc();
while (!isspace(c) && c != EOF) s += c, c = gc();
return *this;
}
} in;
struct O {
char st[100], fout[_];
signed stk = 0, top = 0;
inline void flush() { fwrite(fout, 1, top, stdout), fflush(stdout), top = 0; }
inline O& operator<<(int x) {
if (top > (1 << 20))
flush();
if (x < 0)
fout[top++] = 45, x = -x;
do
st[++stk] = x % 10 ^ 48, x /= 10;
while (x);
while (stk) fout[top++] = st[stk--];
return *this;
}
inline O& operator<<(ll x) {
if (top > (1 << 20))
flush();
if (x < 0)
fout[top++] = 45, x = -x;
do
st[++stk] = x % 10 ^ 48, x /= 10;
while (x);
while (stk) fout[top++] = st[stk--];
return *this;
}
inline O& operator<<(char x) {
fout[top++] = x;
return *this;
}
inline O& operator<<(string s) {
if (top > (1 << 20))
flush();
for (char x : s) fout[top++] = x;
return *this;
}
} out;
#define pb emplace_back
#define fir first
#define sec second
template <class T>
inline void cmax(T& x, const T& y) {
(x < y) && (x = y);
}
template <class T>
inline void cmin(T& x, const T& y) {
(x > y) && (x = y);
}
const int N = 5e4 + 10;
const int mod = 1e9 + 7;
int n, m, a[N], v[N], rt[N], ls[N << 8], rs[N << 8], Cnt = 0;
struct SegMent {
int val[N << 8];
void upd(int& p, int l, int r, int pos, int v) {
if (!p)
p = ++Cnt;
val[p] += v;
if (l == r)
return;
int mid = l + r >> 1;
(pos <= mid) ? upd(ls[p], l, mid, pos, v) : upd(rs[p], mid + 1, r, pos, v);
}
int qry(int p, int a, int b, int l, int r) {
if (!p)
return 0;
if (a <= l && r <= b)
return val[p];
int mid = l + r >> 1, ans = 0;
if (a <= mid)
ans = qry(ls[p], a, b, l, mid);
if (b > mid)
ans += qry(rs[p], a, b, mid + 1, r);
return ans % mod;
}
inline int low(const int& x) { return x & -x; }
inline void upd(int x, int y, int k) {
for (; x <= n; x += low(x)) upd(rt[x], 1, n, y, k);
}
inline ll qry(int l, int r, int a, int b) {
ll ans = 0;
for (; r; r ^= low(r)) {
ans = (ans + qry(rt[r], a, b, 1, n)) % mod;
}
for (--l; l; l ^= low(l)) {
ans = (ans - qry(rt[l], a, b, 1, n) + mod) % mod;
}
return ans;
}
} cnt, qwq;
ll ans = 0;
signed main() {
#ifdef _WIN64
freopen("testdata.in", "r", stdin);
#endif
in >> n >> m;
rep(i, 1, n) in >> a[i] >> v[i];
rep(i, 1, n) cnt.upd(i, a[i], 1), qwq.upd(i, a[i], v[i]);
rep(i, 1, n) {
ans = (ans + cnt.qry(i + 1, n, 1, a[i]) * v[i]) % mod;
ans = (ans + qwq.qry(i + 1, n, 1, a[i])) % mod;
}
while (m--) {
int x, y;
in >> x >> y;
if (x > y)
swap(x, y);
if (x == y) {
out << ans << '\n';
continue;
}
ans = (ans +
(a[x] < a[y] ? 1 : -1) *
(2 * qwq.qry(x, y, min(a[x], a[y]) + 1, max(a[x], a[y]) - 1) +
(cnt.qry(x, y, min(a[x], a[y]) + 1, max(a[x], a[y]) - 1) + 1) * (v[x] + v[y])) %
mod +
mod) %
mod;
ans = (ans + cnt.qry(x, y, 1, min(a[x], a[y]) - 1) * (v[y] - v[x]) % mod + mod) % mod;
ans = (ans + cnt.qry(x, y, max(a[x], a[y]) + 1, n) * (v[x] - v[y]) % mod + mod) % mod;
cnt.upd(x, a[x], -1), qwq.upd(x, a[x], -v[x]);
cnt.upd(y, a[y], -1), qwq.upd(y, a[y], -v[y]);
cnt.upd(x, a[y], 1), qwq.upd(x, a[y], v[y]);
cnt.upd(y, a[x], 1), qwq.upd(y, a[x], v[x]);
swap(a[x], a[y]), swap(v[x], v[y]);
out << ans << '\n';
}
return out.flush(), 0;
}

P3759 [TJOI2017]不勤劳的图书管理员 [树套树]的更多相关文章

  1. 洛谷P3759 - [TJOI2017]不勤劳的图书管理员

    Portal Description 给出一个\(1..n(n\leq5\times10^4)\)的排列\(\{a_n\}\)和数列\(\{w_n\}(w_i\leq10^5)\),进行\(m(m\l ...

  2. [TJOI2017]不勤劳的图书管理员(分块+树状数组)

    有一个数组开大会MLE开小会RE的做法:就是树套树,即树状数组套主席树,这种方法比较暴力,然而很遗憾它不能通过,因为其时空复杂度均为O(nlog2n). 想到一种不怎么耗内存,以时间换空间,分块!单次 ...

  3. [P3759][TJOI2017]不勤劳的图书管理员(分块+树状数组)

    题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生 这两本书页数的和的厌烦度.现在有n本被打乱顺序的书 ...

  4. 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】

    题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...

  5. LUOGU P3759 [TJOI2017]不勤劳的图书管理员(树套树)

    传送门 解题思路 和以前做过的一道题有点像,就是区间逆序对之类的问题,用的是\(BIT\)套权值线段树,交换时讨论一下计算答案..跑的不如暴力快.. 代码 #include<iostream&g ...

  6. 【BZOJ4889】[Tjoi2017]不勤劳的图书管理员 分块+树状数组

    [BZOJ4889][Tjoi2017]不勤劳的图书管理员 题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让 ...

  7. 【bzoj4889】: [Tjoi2017]不勤劳的图书管理员 分块-BIT

    [bzoj4889]: [Tjoi2017]不勤劳的图书管理员 题目大意:给定一个序列(n<=50000),每个数有一个编码ai(ai<=50000)和权值vi(vi<=100000 ...

  8. 4889: [Tjoi2017]不勤劳的图书管理员 树套树

    国际惯例的题面(Bzoj没有,洛谷找的):动态加权逆序对,一眼树套树.256MB内存,5e4范围,不虚不虚.首先把交换改成两个插入和两个删除.考虑插入和删除的贡献,就是统计前面比这个值大的数的数值和, ...

  9. 【洛谷3759】[TJOI2017] 不勤劳的图书管理员(树套树)

    点此看题面 大致题意: 给定一个序列,每个元素有两个属性\(a_i\)和\(v_i\),每次操作改变两个元素的位置,求每次操作后\(\sum{v_i+v_j}[i<j,a_i>a_j]\) ...

随机推荐

  1. 不用'+'完成a + b

    这也是'+'底层运算原理: #include<bits/stdc++.h> using namespace std; int a,b; int main() { scanf("% ...

  2. NR / 5G - W-OFDM

  3. pytorch之 RNN classifier

    import torch from torch import nn import torchvision.datasets as dsets import torchvision.transforms ...

  4. logstash 安装 配置

    1.Logstash 安装:在产生日志的服务器上安装 Logstash1.安装java环境 # yum install java-1.8.0-openjdk.x86_642.安装logstash(使用 ...

  5. Apache 容器 Directory Location Files 及htaccess文件

    配置段容器的类型 相关模块 core mod_proxy 相关指令 <Directory> <DirectoryMatch> <Files> <FilesMa ...

  6. 【算法总结】图论/dp-动态规划 大总结

    写于一只蹲在角落的蒟蒻-Z__X... 2020.2.7,图论和 \(dp\) 终于告一段落.蓦然回首,好似已走过许多...不曾细细品味,太多太多又绵延不断地向我涌来... 谨以此纪念 逝去 的图论和 ...

  7. Java面试—消息队列

    消息队列面试题 题目来自于中华石杉,解决方案根据自己的思路来总结而得. 题目主要如下: 1. 为什么要引入消息队列? 消息队列的引入可以解决3个核心问题: 解耦 异步 削峰 解耦 在一个项目中,如果一 ...

  8. 2,Hadoop部署

    前期准备 (1)JAVA_HOME:因为Hadoop的配置文件中依赖 $JAVA_HOME.修改/etc/profile文件. (2)hostname:修改主机名,方便管理./etc/sysconfi ...

  9. C#设计模式学习笔记:(10)外观模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7772184.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲结构型设计模式的第五个模式--外 ...

  10. SAP VL10B 报错 - 4500000317 000010 交付 $ 1 的交付项目 000010 与 POD 无关-

    SAP VL10B 报错 - 4500000317 000010 交付 $ 1 的交付项目 000010 与 POD 无关- 如下公司间STO单据, 业务背景是货物从公司代码LYSP转入公司代码BTS ...