P3759 [TJOI2017]不勤劳的图书管理员 [树套树]

树套树是什么啊我不知道/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;
}

原文地址:https://www.cnblogs.com/Isaunoya/p/12232994.html

时间: 2024-07-31 16:32:08

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

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

Portal Description 给出一个\(1..n(n\leq5\times10^4)\)的排列\(\{a_n\}\)和数列\(\{w_n\}(w_i\leq10^5)\),进行\(m(m\leq5\times10^4)\)次操作: 交换\(a_{p_1},a_{p_2}\),并求\(\sum_{i=1}^n \sum_{j=i+1}^n [a_i>a_j](w_{a_i}+w_{a_j})\). Solution 树套树/CDQ分治,想锻炼一下代码能力所以写了树套树. 首先这是一个求逆

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

题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生 这两本书页数的和的厌烦度.现在有n本被打乱顺序的书,在接下来m天中每天都会因为读者的阅览导致书籍顺序改变位置.因为小豆被要求在接下来的m天中至少 要整理一次图书.小豆想知道,如果他前i天不去整理,第i天他的厌烦度是多少,这样他好选择厌烦度最小的那天去整理. 输入输出格式 输入格式: 第一行会有两个数,n,m分别表示有n本书,m天 接下来n行,每行两

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

[bzoj4889]: [Tjoi2017]不勤劳的图书管理员 题目大意:给定一个序列(n<=50000),每个数有一个编码ai(ai<=50000)和权值vi(vi<=100000),每次交换两个数的位置,交换m次(m<=50000) 求每次交换后的序列的杂乱度对1e9+7取模(对于每一对是逆序对的编码会产生两者权值和的杂乱度). 感觉正解是什么奇怪的树套树?蒟蒻只会分块水题.. 先用BIT求一遍初始状态的杂乱度..(不要问我为什么一开始是BIT..因为打暴力正好用到就懒得改了.

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

国际惯例的题面(Bzoj没有,洛谷找的):动态加权逆序对,一眼树套树.256MB内存,5e4范围,不虚不虚.首先把交换改成两个插入和两个删除.考虑插入和删除的贡献,就是统计前面比这个值大的数的数值和,数量和,后面比这个值小的数的数值和,数量和.然后特判一下当前两个值构成逆序对的情况即可(因为这种情况会被计算两遍).考虑树状数组套动态开点线段树维护这个东西,线段树只需要单点修改区间求和即可,十分简单.然而数组开不下啊......理论上我们数组范围要开到2e7左右,然而并跑不满,开到1.4e7就足以

题解【[TJOI2017]不勤劳的图书管理员】

\[ \texttt{Description} \] 给两个长度为 \(n\) 的数列 \(A\) 和 \(B\) . 记 \(A\) 的逆序对(满足 \(x<y\),\(A_x>A_y\) 的数对 \((x,y)\) )对答案的贡献为 \(B_x+B_y\) . 通俗地说,答案就是 \(\sum\limits_{x < y \ \& \ A_x > A_y}\) \(B_x+B_y\). 一共 \(m\) 次交换,每次交换 \(A_x\) 和 \(A_y\) ,\(B_x

[bzoj4889] [Tjoi2017]不勤劳的图书管理员

Description 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被打乱顺序的书,在接下来m天中每天都会因为读者的阅览导致书籍顺序改变位置.因为小豆被要求在接下来的m天中至少要整理一次图书.小豆想知道,如果他前i天不去整理,第i天他的厌烦度是多少,这样他好选择厌烦度最小的那天去整理. Input 第一行会有两个数,n,m分别表示有n本书,m天 接下来n行,每行两个数

BZOJ_3196_二逼平衡树(树套树:线段树+Treap)

描述 可以处理区间问题的平衡树. 分析 树套树.可以用线段树套Treap.人生第一道树套树的题... op1:如果在整区间,直接在该区间的treap上求解.否则分两个区间求解,然后相加.最后+1. op2:这个不太好直接做,可以二分,每次假定一个值,用这个值去做op1,以此求得一个rank=k+1的数,求rank=k的数等价与求这个数的前驱pre. op3:先删后加. op4&op5:如果在整区间,直接在该区间的treap上求解,否则分量个区间求解,pre取最大值,suc取最小值.注意有些数在有

BZOJ 3110: [Zjoi2013]K大数查询 [树套树]

3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6050  Solved: 2007[Submit][Status][Discuss] Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M接下来M行,每行形如1 a

[BZOJ3110] [Zjoi2013] K大数查询 (树套树)

Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M 接下来M行,每行形如1 a b c或2 a b c Output 输出每个询问的结果 Sample Input 2 5 1 1 2 1 1 1 2 2 2 1 1 2 2 1 1 1 2 1 2 3 Sample Output 1 2 1 HINT [