这道题就是不要求强制在线的 BZOJ 3744 Gty的妹子序列

所以说离线做法有莫队,在线做法见上面连接.

这里贴出常数巨大O(nnlogn)O(n\sqrt nlogn)O(nn​logn)分块+树状数组+主席树做法.

CODE

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
template<typename T>inline void read(T &num) {
char ch; int flg = 1;
while((ch=getchar())<'0'||ch>'9')if(ch=='-')flg=-flg;
for(num=0;ch>='0'&&ch<='9';num=num*10+ch-'0',ch=getchar());
num*=flg;
}
const int MAXN = 50005;
int n, m, N, B, f[225][MAXN], a[MAXN], b[MAXN], bel[MAXN]; int ch[MAXN*20][2], sz[MAXN*20], rt[MAXN], tot;
void insert(int &i, int p, int l, int r, int x) {
sz[i=++tot] = sz[p] + 1;
if(l == r) return;
int mid = (l + r) >> 1;
if(x <= mid) ch[i][1] = ch[p][1], insert(ch[i][0], ch[p][0], l, mid, x);
else ch[i][0] = ch[p][0], insert(ch[i][1], ch[p][1], mid+1, r, x);
} int query(int x, int y, int l, int r, int L, int R) {
if(sz[x] == sz[y]) return 0;
if(L <= l && r <= R) return sz[y]-sz[x];
int mid = (l + r) >> 1;
if(R <= mid) return query(ch[x][0], ch[y][0], l, mid, L, R);
else if(L > mid) return query(ch[x][1], ch[y][1], mid+1, r, L, R);
return query(ch[x][0], ch[y][0], l, mid, L, R) + query(ch[x][1], ch[y][1], mid+1, r, L, R);
} int T[MAXN]; inline void upd(int x, int val) {
while(x) T[x] += val, x -= x&-x;
}
inline int qsum(int x) { int res = 0;
while(x <= N) res += T[x], x += x&-x;
return res;
}
inline int solve(int L, int R) {
if(L > R) swap(L, R);
int res = 0;
if(bel[L] == bel[R]) {
for(int i = L; i <= R; ++i)
res += qsum(a[i]+1), upd(a[i], 1);
for(int i = L; i <= R; ++i) upd(a[i], -1);
return res;
}
res = f[bel[L]+1][R];
for(int i = L; i <= bel[L]*B && i < R; ++i)
res += query(rt[i], rt[R], 1, N, 1, a[i]-1);
return res;
} int main () {
read(n); B = sqrt(n);
for(int i = 1; i <= n; ++i) read(a[i]), b[++N] = a[i];
sort(b + 1, b + N + 1);
N = unique(b + 1, b + N + 1) - b - 1;
for(int i = 1; i <= n; ++i) a[i] = lower_bound(b + 1, b + N + 1, a[i]) - b;
for(int i = 1; i <= n; ++i) {
bel[i] = (i-1)/B + 1;
insert(rt[i], rt[i-1], 1, N, a[i]);
}
for(int i = 1; i <= bel[n]; ++i) {
for(int j = (i-1)*B+1; j <= n; ++j)
f[i][j] = f[i][j-1] + qsum(a[j]+1), upd(a[j], 1);
memset(T, 0, (N+1)<<2);
}
int x, y;
read(m);
while(m--) {
read(x), read(y);
printf("%d\n", solve(x, y));
}
}

最新文章

  1. 初识ASP.NET MVC
  2. android:layout_gravity 和 android:gravity 的区别
  3. logback文章推荐
  4. 三种语言(c++、as、lua)中函数的差异性
  5. Flex 日期和字符串之间转换
  6. uva 10369
  7. 关于Jquery.Data()和HTML标签的data-*属性
  8. 学生管理系统(list)
  9. BAPI_GOODSMVT_CREATE 移动类型311 CODE = &#39;04&#39; 代码
  10. JS中三目运算符和if else的区别分析与示例
  11. GreenDao
  12. springMVC(5)---导入excel文件数据到数据库
  13. Nodejs的安装配置及如何在sublimetext2中运行js
  14. 【转】BFG Repo-Cleaner: Removes large or troublesome blobs like git-filter-branch does, but faster.
  15. Windows下Oracle 11g安装以及创建数据库
  16. leetcode23
  17. Win10系列:JavaScript页内导航
  18. Java交替打印两个字符串
  19. 设置Shader关键字高亮(网上转)
  20. [csp-201809-3]元素选择器-编译原理

热门文章

  1. Mac安装postgresql和卸载PostgreSQL
  2. ubuntu 忘记密码如何 修改密码
  3. Pebbles HDU 2167
  4. OPENCV运行的问题,自带的程序可以运行,但是自己制作的QT报错
  5. 这里除了安全,什么都不会发生!Docker镜像P2P加速之路
  6. spring-cloud 学习三 服务提供者
  7. VBA学习资料分享-1
  8. [NOIP10.6模拟赛]2.equation题解--DFS序+线段树
  9. iOS 更改状态栏文字颜色
  10. BigDecimal与Long、int之间的相互转换