BZOJ4889:[TJOI2017]不勤劳的图书管理员
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4889
树状数组套线段树搞一搞就好了。
时间复杂度:\(O(nlog^2n)\)
空间复杂度:\(O(nlog^2n)\)
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
typedef pair<int,int> pii;
#define low(i) ((i)&(-(i)))
#define fr first
#define sc second
const int maxn=5e4+5,pps=1e9+7;
int n,m,ans;
int pos[maxn],v[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct segment_tree {
int tot;
pii tree[maxn*200];
int ls[maxn*200],rs[maxn*200];
void change(int p,int l,int r,int pos,int v,int cnt) {
while(1) {
tree[p].fr=(tree[p].fr+v)%pps;
tree[p].sc+=cnt;if(l==r)break;
int mid=(l+r)>>1;
if(pos<=mid) {
if(!ls[p])ls[p]=++tot;
r=mid,p=ls[p];
}
else {
if(!rs[p])rs[p]=++tot;
l=mid+1,p=rs[p];
}
}
}
}T;
struct TreeArray {
int cnt1,cnt2;
int rt[maxn],u1[maxn],u2[maxn];
void change(int pos,int key1,int key2,int cnt) {
for(int i=pos;i<=n;i+=low(i)) {
if(!rt[i])rt[i]=++T.tot;
T.change(rt[i],1,n,key1,key2,cnt);
}
}
int query(int L,int R,int pos,int val,int opt) {
if(R<=L)return 0;
cnt1=cnt2=0;pii res;res.fr=res.sc=0;
for(int i=L;i;i-=low(i))u1[++cnt1]=rt[i];
for(int i=R;i;i-=low(i))u2[++cnt2]=rt[i];
int l=1,r=n;
while(l!=r) {
int mid=(l+r)>>1;
if(pos<=mid) {
if(opt) {
for(int i=1;i<=cnt1;i++) {
int u=T.rs[u1[i]];
res.fr=(res.fr-T.tree[u].fr+pps)%pps;
res.sc=res.sc-T.tree[u].sc;
}
for(int i=1;i<=cnt2;i++) {
int u=T.rs[u2[i]];
res.fr=(res.fr+T.tree[u].fr)%pps;
res.sc=res.sc+T.tree[u].sc;
}
}
r=mid;
for(int i=1;i<=cnt1;i++)u1[i]=T.ls[u1[i]];
for(int i=1;i<=cnt2;i++)u2[i]=T.ls[u2[i]];
}
else {
if(!opt) {
for(int i=1;i<=cnt1;i++) {
int u=T.ls[u1[i]];
res.fr=(res.fr-T.tree[u].fr)%pps;
res.sc=res.sc-T.tree[u].sc;
}
for(int i=1;i<=cnt2;i++) {
int u=T.ls[u2[i]];
res.fr=(res.fr+T.tree[u].fr)%pps;
res.sc=res.sc+T.tree[u].sc;
}
}
l=mid+1;
for(int i=1;i<=cnt1;i++)u1[i]=T.rs[u1[i]];
for(int i=1;i<=cnt2;i++)u2[i]=T.rs[u2[i]];
}
}
return (res.fr+1ll*res.sc*val%pps)%pps;
}
}bit;
int main() {
n=read(),m=read();
for(int i=1;i<=n;i++) {
pos[i]=read(),v[i]=read();
ans=(ans+bit.query(0,i-1,pos[i],v[i],1))%pps;
bit.change(i,pos[i],v[i],1);
}
for(int i=1;i<=m;i++) {
int x=read(),y=read();
if(x>y)swap(x,y);
ans=(ans-bit.query(x,y-1,pos[y],v[y],1)+pps)%pps;
ans=(ans-bit.query(x,y-1,pos[x],v[x],0)+pps)%pps;
ans=(ans+bit.query(x,y-1,pos[y],v[y],0))%pps;
ans=(ans+bit.query(x,y-1,pos[x],v[x],1))%pps;
if(pos[x]>pos[y])ans=(ans-v[x]-v[y]+pps)%pps;
else if(pos[x]<pos[y])ans=(ans+v[x]+v[y])%pps;
bit.change(x,pos[x],-v[x],-1),bit.change(y,pos[y],-v[y],-1);
bit.change(x,pos[y],v[y],1),bit.change(y,pos[x],v[x],1);
swap(pos[x],pos[y]),swap(v[x],v[y]);
printf("%d\n",ans);
}
return 0;
}
BZOJ4889:[TJOI2017]不勤劳的图书管理员的更多相关文章
- [bzoj4889] [Tjoi2017]不勤劳的图书管理员
Description 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被 ...
- bzoj4889: [Tjoi2017]不勤劳的图书管理员(树套树)
传送门 据说正解线段树套平衡树 然而网上参考(抄)了一个树状数组套动态开点线段树的 思路比较清楚,看代码应该就明白了 //minamoto #include<iostream> #incl ...
- 【bzoj4889】: [Tjoi2017]不勤劳的图书管理员 分块-BIT
[bzoj4889]: [Tjoi2017]不勤劳的图书管理员 题目大意:给定一个序列(n<=50000),每个数有一个编码ai(ai<=50000)和权值vi(vi<=100000 ...
- 【BZOJ4889】[Tjoi2017]不勤劳的图书管理员 分块+树状数组
[BZOJ4889][Tjoi2017]不勤劳的图书管理员 题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让 ...
- 【BZOJ4889】不勤劳的图书管理员(树套树)
[BZOJ4889]不勤劳的图书管理员(树套树) 题面 又是权限题,烦死了 洛谷真好 题解 分开考虑每一次交换产生的贡献. 假设交换\((x,y)\) 检查\(x\)与\(y\)对于区间\([x+1, ...
- 洛谷P3759 - [TJOI2017]不勤劳的图书管理员
Portal Description 给出一个\(1..n(n\leq5\times10^4)\)的排列\(\{a_n\}\)和数列\(\{w_n\}(w_i\leq10^5)\),进行\(m(m\l ...
- 【loj2639】[Tjoi2017]不勤劳的图书管理员
#2639. 「TJOI2017」不勤劳的图书管理员 题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产 ...
- BZOJ4889 & 洛谷3759:[TJOI2017]不勤劳的图书管理员——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4889 https://www.luogu.org/problemnew/show/P3759 加里 ...
- 【bzoj4889】[Tjoi2017]不勤劳的图书管理员 树状数组+分块+二分
题目描述(转自洛谷) 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被打 ...
随机推荐
- js 检测客户端网速
<!doctype html> <html> <head> <meta http-equiv=Content-Type content="text/ ...
- 使用ZipArchive解压
本文转载至 http://www.apkbus.com/forum.php?mod=viewthread&tid=131390&extra=page%3D1 qqpk360 该用户从未 ...
- 九度OJ 1172:哈夫曼树 (贪心)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6701 解决:2954 题目描述: 哈夫曼树,第一行输入一个数n,表示叶结点的个数.需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结 ...
- squared-error loss is much more repaidly updated than mean-absolute-deviation when searching for splits
平方差损失能较绝对值差损失更快地更新
- Struts2问题总结
1 如何搭建Struts2开发环境? Struts2 获取 http://struts.apache.org/download.cgi Struts-2.3.16.3-all.zip 创建Web项 ...
- Java for LeetCode 111 Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 协程与IO多路复用
IO多路复用 I/O多路复用 : 通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作. Python Python中有一个select模块, ...
- em、pt、px和百分比
浏览器默认的字体大小为100%=16px=12pt=1em px像素(Pixel):是固定大小的单元.相对长度单位.像素px是相对于显示器屏幕分辨率而言的.一个像素等于电脑屏幕上的一个点(是你屏幕分辨 ...
- Python环境问题
http://installion.co.uk/ubuntu/precise/main/p/python3.2/uninstall/index.html
- Spring Boot2.0之多数据源事务管理
结合前面做的小项目,如果我把test01 test02下面的 service 都加了 事务的注解 这样启动时候会报错! 事务管理器里面不能有两个事务!!!! 这时候需要用 @Transactiona ...