luogu3759 [TJOI2017]不勤劳的图书管理员
分块+权值逆序对
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
int n, m, uu, vv, blc, bel[50005], ans, ccnt[50005], cval[500005], num[255];
int sum[255][255];
const int mod=1e9+7;
struct Node{
int idx, val;
}nd[50005], qwq[255][255];
bool cmp1(Node x, Node y){
return x.idx<y.idx;
}
int lb(int x){
return x&-x;
}
void bitAdd(int x, int y){
for(int i=x; i<=n; i+=lb(i)){
ccnt[i]++;
cval[i] = (cval[i] + y) % mod;
}
}
int bitQuery(int x, int &qaq){
int re=0;
for(int i=x; i; i-=lb(i)){
qaq += ccnt[i];
re = (re + cval[i]) % mod;
}
return re;
}
int query1(int l, int r, Node x){
if(l>r) return 0;
int re=0;
if(bel[l]==bel[r]){
for(int i=l; i<=r; i++)
if(nd[i].idx>x.idx)
re = (re + (nd[i].val+x.val)%mod) % mod;
}
else{
for(int i=bel[l]+1; i<=bel[r]-1; i++){
int pos=lower_bound(qwq[i]+1, qwq[i]+1+num[i], x, cmp1)-qwq[i];
int cnt=num[i]-pos+1;
re = (re + (ll)cnt * x.val) % mod;
re = (re + ((sum[i][num[i]] - sum[i][pos-1])%mod + mod)%mod) % mod;
}
for(int i=l; i<=bel[l]*blc; i++)
if(nd[i].idx>x.idx)
re = (re + (nd[i].val+x.val)%mod) % mod;
for(int i=(bel[r]-1)*blc+1; i<=r; i++)
if(nd[i].idx>x.idx)
re = (re + (nd[i].val+x.val)%mod) % mod;
}
return re;
}
int query2(int l, int r, Node x){
if(l>r) return 0;
int re=0;
if(bel[l]==bel[r]){
for(int i=l; i<=r; i++)
if(nd[i].idx<x.idx)
re = (re + (nd[i].val+x.val)%mod) % mod;
}
else{
for(int i=bel[l]+1; i<=bel[r]-1; i++){
int pos=lower_bound(qwq[i]+1, qwq[i]+1+num[i], x, cmp1)-qwq[i];
int cnt=pos-1;
re = (re + (ll)cnt * x.val) % mod;
if(cnt) re = (re + sum[i][cnt]) % mod;
}
for(int i=l; i<=bel[l]*blc; i++)
if(nd[i].idx<x.idx)
re = (re + (nd[i].val+x.val)%mod) % mod;
for(int i=(bel[r]-1)*blc+1; i<=r; i++)
if(nd[i].idx<x.idx)
re = (re + (nd[i].val+x.val)%mod) % mod;
}
return re;
}
void gouzao(int x){
num[x] = 0;
for(int i=(x-1)*blc+1; i<=min(x*blc, n); i++)
qwq[x][++num[x]] = nd[i];
sort(qwq[x]+1, qwq[x]+1+num[x], cmp1);
for(int i=1; i<=num[x]; i++){
sum[x][i] = qwq[x][i].val;
sum[x][i] = (sum[x][i] + sum[x][i-1]) % mod;
}
}
int main(){
cin>>n>>m;
blc = sqrt(n);
for(int i=1; i<=n; i++){
scanf("%d %d", &nd[i].idx, &nd[i].val);
bel[i] = (i - 1) / blc + 1;
}
for(int i=1; i<=n; i++)
qwq[bel[i]][++num[bel[i]]] = nd[i];
for(int i=1; i<=bel[n]; i++)
gouzao(i);
for(int i=1; i<=n; i++){
int tmp1=0, tmp2=0;
ans = (ans + bitQuery(n, tmp1) - bitQuery(nd[i].idx, tmp2)) % mod;
ans = (ans + (ll)(tmp1-tmp2)*nd[i].val) % mod;
bitAdd(nd[i].idx, nd[i].val);
}
while(m--){
scanf("%d %d", &uu, &vv);
if(uu>vv) swap(uu, vv);
ans = (ans - query1(uu+1, vv-1, nd[vv]) + mod) % mod;
ans = (ans - query2(uu+1, vv-1, nd[uu]) + mod) % mod;
if(nd[uu].idx>nd[vv].idx) ans = (ans - (nd[uu].val+nd[vv].val)%mod + mod) % mod;
swap(nd[uu], nd[vv]);
gouzao(bel[uu]); gouzao(bel[vv]);
ans = (ans + query1(uu+1, vv-1, nd[vv])) % mod;
ans = (ans + query2(uu+1, vv-1, nd[uu])) % mod;
if(nd[uu].idx>nd[vv].idx) ans = (ans + (nd[uu].val+nd[vv].val)%mod) % mod;
printf("%d\n", ans);
}
return 0;
}
luogu3759 [TJOI2017]不勤劳的图书管理员的更多相关文章
- 【bzoj4889】: [Tjoi2017]不勤劳的图书管理员 分块-BIT
[bzoj4889]: [Tjoi2017]不勤劳的图书管理员 题目大意:给定一个序列(n<=50000),每个数有一个编码ai(ai<=50000)和权值vi(vi<=100000 ...
- 【BZOJ4889】[Tjoi2017]不勤劳的图书管理员 分块+树状数组
[BZOJ4889][Tjoi2017]不勤劳的图书管理员 题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让 ...
- 洛谷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」不勤劳的图书管理员 题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产 ...
- [P3759][TJOI2017]不勤劳的图书管理员(分块+树状数组)
题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生 这两本书页数的和的厌烦度.现在有n本被打乱顺序的书 ...
- BZOJ4889 & 洛谷3759:[TJOI2017]不勤劳的图书管理员——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4889 https://www.luogu.org/problemnew/show/P3759 加里 ...
- [bzoj4889] [Tjoi2017]不勤劳的图书管理员
Description 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被 ...
- 【bzoj4889】[Tjoi2017]不勤劳的图书管理员 树状数组+分块+二分
题目描述(转自洛谷) 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被打 ...
- [TJOI2017] 不勤劳的图书管理员
题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被打乱顺序的书, ...
随机推荐
- pc端_移动端_H5_ QQ在线客服链接代码
PC端:QQ在线客服链接代码 <a href="tencent://message/?uin=1239300678&Site=sc.chinaz.com&Menu=ye ...
- leetcode166 Fraction to Recurring Decimal
思路: 模拟. 实现: class Solution { public: string fractionToDecimal(int numerator, int denominator) { long ...
- Sass基本特性
Sass扩展/继承@extend 代码的继承,声明方式:.class;调用方式:@extend 如: .btn { border: 1px solid #ccc; padding: 6px 10px; ...
- codevs 3054 高精度练习-文件操作
时间限制: 1 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入一组数据,将每个数据加1后输出 输入描述 Input Descripti ...
- PostgressSQL-Installation
安装 sudo apt install -y postgresql 自动生成一个名为 postgres 的 Linux 系统用户 $ finger postgres Login: postgres N ...
- 数据倾斜是多么痛?spark作业调优秘籍
目录视图 摘要视图 订阅 [观点]物联网与大数据将助推工业应用的崛起,你认同么? CSDN日报20170703——<从高考到程序员——我一直在寻找答案> [直播]探究L ...
- 树形dp——覆盖所有边的最少费用(Protecting Zonk)
一.问题描述 有一个n(n<=10000)个节点的无根树.有两种装置A,B,每种都有无限多个. 1.在某个节点X使用A装置需要C1(C1<=1000)的花费,并且此时与节点X相连的边都被覆 ...
- perl 引用(数组和hash引用) --- perlreftut - Mark 的一个简单的'引用'教程 ---Understand References Today. --Mark Jason Dominus, Plover Systems (mjd-perl-ref+@plover.com)
https://blog.csdn.net/fangwei1235/article/details/8570886 首页 博客 学院 下载 论坛 APP 问答 商城 活动 VIP会员 招聘 ITeye ...
- C# 使用Epplus导出Excel [1]:导出固定列数据
C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...
- mysql delete 表无法用别名
delete from exam_paper_question_opt a WHERE a.OPTION_ID = 65630 and a.QUESTION_ID = 28656 AND a.EXAM ...