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本被打 ...
随机推荐
- 启动Eclipse时,启不起来JVM terminated. Exit code=-1
启动Eclipse时,启不起来JVM terminated. Exit code=-1 出现错误了,不知道什么原因原本好好的Eclipse,今天早上出问题了,启动不起来还抛出JVM terminate ...
- com.opensymphony.xwork2.ognl.OgnlValueStack - Error setting expression 'customer.applyRate' with value '[Ljava.lang.String;@7d3fae2c'
出错的3个最可能的原因:我是第二种错误 1.action配置错误 <action name="doCreateMeetingInfo" class="meeting ...
- C#之stream
在C#中经常要用stream stream下面主要有 FileStream:使用文件作为后备设备. BufferedStream:使用缓冲区作为后备设备,用来增强性能的中间存储. MemoryStre ...
- Hadoop常见异常及其解决方式
1.Shell$ExitCodeException 现象:执行hadoop job时出现例如以下异常: 14/07/09 14:42:50 INFO mapreduce.Job: Task Id : ...
- MySQL CREATE TRIGGER (1)
CREATE TRIGGER语法 CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW ...
- python 基础 9.3 mysql 数据操作
#/usr/bin/python #coding=utf-8 #@Time :2017/11/21 0:20 #@Auther :liuzhenchuan #@File :mysql 数据操作 ...
- python 基础 8.4 re的 spilt() findall() finditer() 方法
#/usr/bin/python #coding=utf-8 #@Time :2017/11/18 18:24 #@Auther :liuzhenchuan #@File :re的spli ...
- EasyPlayer Android安卓RTSP服务器低延时再优化策略
EasyPlayer低延迟再优化策略 EasyPlayer是一款专门针对RTSP协议进行过优化的播放器.其中两个我们引以为傲的的优点就是起播快和低延迟.最近我们遇到一些需求,其对延迟要求非常苛刻,于是 ...
- There are two different types of export, named and default
export - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statemen ...
- 中文WebFont探索
本文主要讲中文webFont的相关知识,包含了业界现状.WebFont优势.实现方案等. 一 业界使用WebFont现状 1.1 英文WebFont使用现状 英文版已使用非常广泛.比较有名的字体库:G ...