思路:

莫队算法+树状数组。

莫队算法的基本思想是对大量要查询的区间进行离线处理,按照一定的顺序计算,来降低复杂度。概括来说,我们在知道了[l, r]的解,并且可以通过一个较低的复杂度推出[l - 1, r], [l, r - 1], [l + 1, r], [l, r + 1]的解的情况下,则可使用该算法。

对该算法比较好的介绍:

1.https://blog.sengxian.com/algorithms/mo-s-algorithm

2.http://blog.csdn.net/bossup/article/details/39236275

在本题中,对于一个区间[l, r],实际上是求a[l] * (r - l + 1) + a[l + 1] * (r - l) +... + a[r] * 1。

那么在知道了[l, r]的解的前提下,如何转移到[l', r']呢?

我们可以观察一个简单的情况:[l, r] -> [l - 1, r]。比如[1, 2, 3, 4] -> [3, 1, 2, 3, 4]。

对于这个例子,假设原来的解为res。那么在左边加上一个‘3’之后,新的解res_new为

1 * 5 + 2 * 4 + 3 * 3 + 3 * 2 + 4 * 1 =

1 * 4 + 2 * 3 + 3 * 2 + 4 * 1 + 1 + 2 + 3 * 3 =

res + 1 + 2 + 3 * 3。

实际上就是原来的解res + 原来的区间内比3小的数的和 + 3 * (原来的区间内大于等于3的数的个数 + 1);

反过来,[3, 1, 2, 3, 4] -> [1, 2, 3, 4]的过程类似。

我们可以使用两个树状数组来维护变化的部分。具体来说其中一个维护大于等于3的数的个数,另一个维护小于3的数的和即可。

实现:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll; const int N = ; int t, n, m, L, R, a[N + ], block;
ll tot = , ans[N + ];
ll bit1[N + ]; //统计个数
ll bit2[N + ]; //统计和 struct query
{
int l, r, id;
};
query Q[N + ]; bool cmp(const query & x, const query & y)
{
if (x.l / block != y.l / block)
return x.l / block < y.l / block;
return x.r < y.r;
} void bit_add(ll * bit, int i, int x)
{
if (i == )
return;
while (i <= N)
{
bit[i] += x;
i += i & -i;
}
} ll bit_sum(ll * bit, int i)
{
ll s = ;
while (i)
{
s += bit[i];
i -= i & -i;
}
return s;
} ll bit_query(ll * bit, int l ,int r)
{
return bit_sum(bit, r) - bit_sum(bit, l - );
} void Add(int pos)
{
tot += (ll)a[pos] * (bit_query(bit1, a[pos], N + ) + );
tot += bit_query(bit2, , a[pos] - );
bit_add(bit1, a[pos], );
bit_add(bit2, a[pos], a[pos]);
} void Del(int pos)
{
tot -= (ll)a[pos] * bit_query(bit1, a[pos], N + );
tot -= bit_query(bit2, , a[pos] - );
bit_add(bit1, a[pos], -);
bit_add(bit2, a[pos], -a[pos]);
} void init()
{
block = sqrt(n);
L = R = ;
tot = a[];
memset(bit1, , sizeof(bit1));
memset(bit2, , sizeof(bit2));
bit_add(bit1, a[], );
bit_add(bit2, a[], a[]);
} int main()
{
cin >> t;
while (t--)
{
cin >> n >> m;
for (int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
}
init();
for (int i = ; i < m; i++)
{
scanf("%d %d", &Q[i].l, &Q[i].r);
Q[i].id = i;
}
sort(Q, Q + m, cmp);
for (int i = ; i < m; i++)
{
while (L < Q[i].l)
Del(L++);
while (L > Q[i].l)
Add(--L);
while (R < Q[i].r)
Add(++R);
while (R > Q[i].r)
Del(R--);
ans[Q[i].id] = tot;
}
for (int i = ; i < m; i++)
{
printf("%lld\n", ans[i]);
}
}
return ;
}

hihocoder offer收割编程练习赛11 D 排队接水的更多相关文章

  1. hihocoder offer收割编程练习赛11 C 岛屿3

    思路: 并查集的应用. 实现: #include <iostream> #include <cstdio> using namespace std; ][]; int n, x ...

  2. hihocoder offer收割编程练习赛11 B 物品价值

    思路: 状态压缩 + dp. 实现: #include <iostream> #include <cstdio> #include <cstring> #inclu ...

  3. hihocoder offer收割编程练习赛11 A hiho字符串

    思路: 我用的尺取. 注意题目描述为恰好2个'h',1个'i',1个'o'. 实现: #include <iostream> #include <cstdio> #includ ...

  4. hihocoder [Offer收割]编程练习赛4

    描述 最近天气炎热,小Ho天天宅在家里叫外卖.他常吃的一家餐馆一共有N道菜品,价格分别是A1, A2, ... AN元.并且如果消费总计满X元,还能享受优惠.小Ho是一个不薅羊毛不舒服斯基的人,他希望 ...

  5. hihocoder [Offer收割]编程练习赛61

    [Offer收割]编程练习赛61 A:最小排列 给定一个长度为m的序列b[1..m],再给定一个n,求一个字典序最小的1~n的排列A,使得b是A的子序列. 贪心即可,b是A的子序列,把不在b中的元素, ...

  6. 【[Offer收割]编程练习赛11 D】排队接水

    [题目链接]:http://hihocoder.com/problemset/problem/1488 [题意] 中文题 [题解] 莫队算法+树状数组; 首先贪心地知道,应该按照时间从小到大的顺序打水 ...

  7. ACM学习历程—Hihocoder [Offer收割]编程练习赛1

    比赛链接:http://hihocoder.com/contest/hihointerview3/problem/1 大概有一个月没怎么打算法了.这一场的前一场BC,也打的不是很好.本来Div1的A和 ...

  8. hihocoder offer收割编程练习赛8 C 数组分拆

    思路:(引自bfsoyc的回答:http://hihocoder.com/discuss/question/4160) 动态规划.状态dp[i]表示 前i个数的合法的方案数,转移是 dp[i] = s ...

  9. 【[Offer收割]编程练习赛11 B】物品价值

    [题目链接]:http://hihocoder.com/problemset/problem/1486 [题意] [题解] 设f[i][j]表示前i个物品,每种属性的状态奇偶状态为j的最大价值; 这里 ...

随机推荐

  1. Android数据与服务器交互的GET,POST,HTTPGET,HTTPPOST的使用

    Android有这几种方式,可以提交数据到服务器,他们是怎么使用的呢,这里我们来探讨一下. 这里的例子用的都是提交客户端的用户名及密码,同时本节用到的StreamTools.readInputStre ...

  2. setTimeout不可靠的修正办法及clearTimeout

    javascript里的这两个定时器函数,大家一定耳熟能详: setTimeout("函数()",毫秒)就是开启一个计时器,指定毫秒后执行该函数一次. 有关定时器,javascri ...

  3. MySQL 5.7 Keywords and Reserved Words

    https://dev.mysql.com/doc/refman/5.7/en/keywords.html#keywords-5-7-detailed-T

  4. WinPE:创建 USB 可引导驱动器

    https://msdn.microsoft.com/zh-cn/library/windows/hardware/dn938386(v=vs.85).aspx

  5. “Invalid configuration file. File "I:/My Virtual Machines/Windows XP english Professional/Windows XP Professional.vmx" was created by a VMware product

    “Invalid configuration file. File "I:/My Virtual Machines/Windows XP english Professional/Windo ...

  6. mysql数据库ip与字符串

    Mysql自带的IP转换语句 inet_aton:将ip地址转换成数字型 inet_ntoa:将数字型转换成ip地址 //使用inet_aton函数,将字符串IP转换为整型: mysql> se ...

  7. java中一个字符串是另外一个字符串的字串

    java中一个字符串是另外一个字符串的字串 String类中有一个方法 public boolean contains(Sting s)就是用来判断当前字符串是否含有参数指定的字符串例s1=“take ...

  8. 【Codevs1346】HelloWorld编译器

    http://codevs.cn/problem/1346/ 可怜我战绩 // <1346.cpp> - 10/30/16 17:12:09 // This file is made by ...

  9. gfnormal 域名 是阿里云的高防IP

    最近DGA检出了一堆阿里高防的域名,例如:u3mbyv2siyaw2tnm.gfnormal09aq.com,然后专门查找了下相关文档. 例如 8264.com 这个网站启用了aliyun的高防DDo ...

  10. 聊聊Shiro

    Shiro是项目中用的比较多Java安全框架,能满足大多数项目的安全认证.授权流程.相比SpringSecurity的复杂重量级,它更简单易用. Shiro中最关键的两个概念是认证和授权,前者解决确认 ...