题目链接

洛谷P3759

题解

树状数组套主席树板题

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
#define lbt(x) (x & -x)
using namespace std;
const int maxn = 100005,maxm = 10000005,INF = 1000000000,P = 1000000007;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int sum[maxm],siz[maxm],ls[maxm],rs[maxm],cnt;
void upd(int u){
sum[u] = (sum[ls[u]] + sum[rs[u]]) % P;
siz[u] = siz[ls[u]] + siz[rs[u]];
}
void Modify(int& u,int l,int r,int pos,int v,int vv){
if (!u) u = ++cnt;
if (l == r){sum[u] += v; siz[u] += vv; return;}
int mid = l + r >> 1;
if (mid >= pos) Modify(ls[u],l,mid,pos,v,vv);
else Modify(rs[u],mid + 1,r,pos,v,vv);
upd(u);
}
cp Query(int u,int l,int r,int L,int R){
if (!u) return mp(0,0);
if (l >= L && r <= R) return mp(sum[u],siz[u]);
int mid = l + r >> 1;
if (mid >= R) return Query(ls[u],l,mid,L,R);
if (mid < L) return Query(rs[u],mid + 1,r,L,R);
cp t = Query(ls[u],l,mid,L,R),tt = Query(rs[u],mid + 1,r,L,R);
return mp((t.first + tt.first) % P,t.second + tt.second);
}
int rt[maxn << 2],n,m,N = 100005,A[maxn],V[maxn]; LL ans;
void modify(int u,int pos,int v,int vv){
for (int i = u; i <= N; i += lbt(i))
Modify(rt[i],1,N,pos,v,vv);
}
cp query(int l,int r,int L,int R){
if (L > R) return mp(0,0);
cp re = mp(0,0),t;
for (int i = r; i; i -= lbt(i)){
t = Query(rt[i],1,N,L,R);
re.first = (re.first + t.first) % P;
re.second += t.second;
}
for (int i = l - 1; i; i -= lbt(i)){
t = Query(rt[i],1,N,L,R);
re.first = (re.first - t.first + P) % P;
re.second -= t.second;
}
return re;
}
int main(){
n = read(); m = read();
int x,y; cp t,tt;
REP(i,n){
A[i] = read(); V[i] = read();
if (i - 1){
t = query(1,i - 1,A[i] + 1,N);
ans = ((ans + t.first) % P + 1ll * t.second * V[i] % P) % P;
}
modify(i,A[i],V[i],1);
}
while (m--){
x = read(); y = read();
if (x == y){printf("%lld\n",(ans % P + P) % P); continue;}
if (x > y) swap(x,y);
if (A[x] < A[y]) ans = ((ans + V[x]) % P + V[y]) % P;
else ans = ((ans - (V[x] + V[y]) % P) % P + P) % P;
if (x < y){
t = query(x + 1,y - 1,A[x] + 1,N);
tt = query(x + 1,y - 1,1,A[x] - 1);
t.first = (t.first - tt.first) % P; t.second -= tt.second;
ans = ((ans + t.first) % P + 1ll * V[x] * t.second % P) % P;
t = query(x + 1,y - 1,1,A[y] - 1);
tt = query(x + 1,y - 1,A[y] + 1,N);
t.first = ((t.first - tt.first) % P + P) % P; t.second -= tt.second;
ans = ((ans + t.first) % P + 1ll * V[y] * t.second % P) % P;
}
modify(x,A[x],-V[x],-1);
modify(y,A[y],-V[y],-1);
modify(x,A[y],V[y],1);
modify(y,A[x],V[x],1);
swap(A[x],A[y]); swap(V[x],V[y]);
printf("%lld\n",(ans % P + P) % P);
}
return 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. [P3759][TJOI2017]不勤劳的图书管理员(分块+树状数组)

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

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

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

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

    树套树是什么啊我不知道/dk 我只知道卡常数w // by Isaunoya #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC o ...

  5. luogu3759 不勤劳的图书管理员 (树状数组套线段树)

    交换的话,只有它们中间的书会对答案产生影响 树状数组记位置,套线段树记书的编号 它对应的页数和书的个数 然后就是减掉中间那些原来是逆序对的,再把交换以后是逆序对的加上 别忘了考虑这两个自己交换以后是不 ...

  6. 【loj2639】[Tjoi2017]不勤劳的图书管理员

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

  7. [TJOI2017] 不勤劳的图书管理员

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

  8. BZOJ4889 & 洛谷3759:[TJOI2017]不勤劳的图书管理员——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4889 https://www.luogu.org/problemnew/show/P3759 加里 ...

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

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

随机推荐

  1. Ubuntu安装netdata监控平台

    介绍 Netdata通过可扩展的Web仪表板提供准确的性能监控,可以显示Linux系统上的流程和服务.它监控有关CPU,内存,磁盘,网络,进程等指标. Netdata官网地址:https://my-n ...

  2. Solr与Elasticsearch区别

    Elasticsearch Elasticsearch是一个实时的分布式搜索和分析引擎.它可以帮助你用前所未有的速度去处理大规模数据. 它可以用于全文搜索,结构化搜索以及分析. 优点 Elastics ...

  3. Visual Stdio Code编辑Mark Down

    Visual Studio Code可以一边写Markdown一边预览了,而且不需要任何插件. 方法如下: 新建一个文件,以 .md 为后缀: Visual Studio Code 原生就支持高亮Ma ...

  4. HPUX 11.31 MC/SG恢复丢失的锁盘

    有时候由于一些特殊的原因,用户的cluster中的锁盘信息丢失,或者需要更换锁盘,只要执行一个命令就可以了. #cmdisklock reset /dev/vglock:/dev/disk/diskX ...

  5. 使用eclipse创建maven项目出现的一个问题

    错误信息 This error occurs when you employ a plugin that Maven could not download. Possible causes for t ...

  6. Docker学习笔记总结

    Docker学习笔记 https://yeasy.gitbooks.io/docker_practice/content/   一 环境搭建 Ubuntu安装 .添加软件源的GPG密钥 curl -f ...

  7. Python3 数据类型-列表

    序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. 索引如下图: 列表命名(list): 组成:使用[]括起来,并且 ...

  8. Automatic Judge

    Description Welcome to HDU to take part in the second CCPC girls’ competition! A new automatic judge ...

  9. Greedy Gift Givers 贪婪的送礼者

    Description 对于一群要互送礼物的朋友,TRW要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群 ...

  10. Uncaught Error: Syntax error, unrecognized expression: |117的js错误

    117指的是js代码在浏览器运行时的出错的行号 var  a="117|117" 前面的错误是由于有特殊符号“|”,用$("txtId"+a).val();去取 ...