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本被打 ...
随机推荐
- mysql日期格式转化
select DATE_FORMAT( '20170701', '%Y-%m-%d'); 先挖坑
- 【Unity3D】【NGUI】Atlas的动态创建
NGUI版本号:3.6.5 1.參见SZUIAtlasMakerRuntimeTest设置对应的值以上值须要提前设置好 2.没有检查是否atlas可以正确创建,自己可以改,增加返回值 3.代码都是在N ...
- IDEA下使用Jetty进行Debug模式调试
过程例如以下: (1)找到选项卡中的 –Run– 然后找到 –Edit Configurations (2)点击下图中绿色的plus–找到Maven点进去 (3)依照下边的方式在Command lin ...
- 对EasyDarwin开源项目后续发展的思考:站在巨人的肩膀上再跳上另一个更高的肩膀
2017 EasyDarwin现状 自从2012年EasyDarwin项目创立开始,经过了快5年了,时光飞逝,如今EasyDarwin已经发展成为了不仅仅是一个单纯的开源流媒体服务器项目了,已经是各种 ...
- django框架小技巧
带命名空间的URL名字 多应用中路由定义,采用命名空间,防止冲突 url(r'^polls/', include('polls.urls', namespace="polls")) ...
- HttpPost (URLConnection)传参数中文乱码
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000000); client.getParams( ...
- Facebook Gradient boosting 梯度提升 separate the positive and negative labeled points using a single line 梯度提升决策树 Gradient Boosted Decision Trees (GBDT)
https://www.quora.com/Why-do-people-use-gradient-boosted-decision-trees-to-do-feature-transform Why ...
- Django框架打印orm转换过程中的sql_模型层
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console':{ 'level':'DEBU ...
- LeetCode——Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- hadoop —— MapReduce例子 (数据排序)
参考:http://eric-gcm.iteye.com/blog/1807468 file1.txt: 2 32 654 32 15 756 65223 file2.txt: 5956 22 650 ...