题目链接

洛谷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. Git笔记——01

    Git - 幕布 Git   教程:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b00 ...

  2. python爬虫基础之一(爬淘宝)

    没想到python如此强大, 今天看一会视频学会了一段python爬虫 这就是我今天学到的内容爬去淘宝网关于书包的一些信息,包括价格, #coding=utf-8 import requests#导入 ...

  3. C++复合类型(结构,共用体,枚举)

    •结构是用户定义的类型,而结构的声明定义了这种类型的数据属性. 一.关键字struct声明:   定义了一种新类型 struct inflatable{ char name[20];//结构成员 fl ...

  4. c# html 导出excel

    [CustomAuthorize]        public FileResult ExportCustomerManagerVisitExcel(string dateType, string r ...

  5. 二 Capacity Scheduler 计算能力调度器

    官网的写的太难懂,参考:http://www.360doc.com/content/14/0603/14/14935022_383254798.shtml Capacity Scheduler 一种可 ...

  6. nodejs基础学习

    一:复制官网的代码,建立一个简单的服务器 const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; ...

  7. 路由器如何设置上网(TP-LINK)

    最近宿舍公用的网络一直不太稳定,正赶上毕业季,本来就打算自己买一台自用的路由器,于是我从一个毕业的师姐手里15RMB收了一台路由器,师姐还给了我一根5m的网线和两根全新15m的,感觉光网线就赚翻了. ...

  8. MyEclipse2013使用总结

    1.myeclipse10中怎样将建的包设置成树形结构或者并列结构. 右上边三角那里进去设置选第一个是显示完整的包名,第二个显示的是树形结构这种方法没效 2.从高版本到项目的低版本的MyEclipse ...

  9. 2019寒假训练营寒假作业(三) MOOC的网络空间安全概论笔记部分

    目录 第五章 网络攻防技术 5.1:网络信息收集技术--网络踩点 信息收集的必要性及内容 网络信息收集技术 网络踩点(Footprinting) 网络踩点常用手段 5.2:网络信息收集技术 --网络扫 ...

  10. osg::Vec2 Vec3 Vec4

    osg::Vec2可以用于保存2D纹理坐标. osg::Vec3是一个三维浮点数数组. osg::Vec4用于保存颜色数据.