【[Offer收割]编程练习赛11 D】排队接水
【题目链接】:http://hihocoder.com/problemset/problem/1488
【题意】
中文题
【题解】
莫队算法+树状数组;
首先贪心地知道,应该按照时间从小到大的顺序打水;
可以发现;
新增加一个人打水的话,对时间小于它的人打水没有影响;
只对时间大于它的人打水的时间有影响;
具体的;
对于时间大于它的打水的人的个数*这个人打水的时间;是新增加的打水时间;
然后同时还要加上这个人单独打水的时间;
即在它前面(时间比它小的打水的人)的所有打水时间总和+这个人的打水时间就是这个人的单独打水时间;
同样的道理
减去一个人也只会对它后面的人造成影响;
前面的人不受影响;
再减去它自身就可以了;
既然能够知道减少一个或者多一个人对答案的影响了;
则直接用莫队算法搞就好了
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e4+100;
struct abc
{
int l,r,id;
};
int n,m,a[N],l,r,num;
LL bit[N],bit2[N],ans,out[N];
abc b[N];
int lowbit(int x)
{
return x&(-x);
}
bool cmp(abc a,abc b)
{
if (a.l/100==b.l/100)
return a.r < b.r;
else
return a.l < b.l;
}
void add(int x,int key)
{
if (key==1)
{
int y = x-1;
LL sum = 0,cnt = 0;
while (y>0)
{
sum+=bit[y];
cnt+=bit2[y];
y-=lowbit(y);
}
ans+=sum+x+(num-cnt)*x;
y = x;
while (y<N-10)
{
bit[y]+=x;
bit2[y]++;
y+=lowbit(y);
}
num++;
}
else
{
int y = x-1;
LL sum = 0,cnt = 0;
while (y>0)
{
sum+=bit[y];
cnt+=bit2[y];
y-=lowbit(y);
}
ans-=sum+x+(num-cnt-1)*x;
y = x;
while (y<N-10)
{
bit[y]-=x;
bit2[y]--;
y+=lowbit(y);
}
num--;
}
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
int T;
cin >> T;
while (T--)
{
rep1(i,0,N-10)
bit[i] = bit2[i] = 0;
cin >> n >> m;
rep1(i,1,n) cin >> a[i];
rep1(i,1,m)
{
cin >> b[i].l >> b[i].r;
b[i].id = i;
}
sort(b+1,b+1+m,cmp);
l = 1,r = 1,ans = 0,num = 0;
add(a[l],1);
rep1(i,1,m)
{
while (r<b[i].r) add(a[++r],1);
while (l>b[i].l) add(a[--l],1);
while (r>b[i].r) add(a[r--],-1);
while (l<b[i].l) add(a[l++],-1);
out[b[i].id] = ans;
}
rep1(i,1,m) cout << out[i] << endl;
}
return 0;
}
【[Offer收割]编程练习赛11 D】排队接水的更多相关文章
- hihocoder offer收割编程练习赛11 D 排队接水
思路: 莫队算法+树状数组. 莫队算法的基本思想是对大量要查询的区间进行离线处理,按照一定的顺序计算,来降低复杂度.概括来说,我们在知道了[l, r]的解,并且可以通过一个较低的复杂度推出[l - 1 ...
- hihocoder offer收割编程练习赛11 C 岛屿3
思路: 并查集的应用. 实现: #include <iostream> #include <cstdio> using namespace std; ][]; int n, x ...
- hihocoder offer收割编程练习赛11 B 物品价值
思路: 状态压缩 + dp. 实现: #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- hihocoder offer收割编程练习赛11 A hiho字符串
思路: 我用的尺取. 注意题目描述为恰好2个'h',1个'i',1个'o'. 实现: #include <iostream> #include <cstdio> #includ ...
- 【[Offer收割]编程练习赛11 B】物品价值
[题目链接]:http://hihocoder.com/problemset/problem/1486 [题意] [题解] 设f[i][j]表示前i个物品,每种属性的状态奇偶状态为j的最大价值; 这里 ...
- 【[Offer收割]编程练习赛11 C】岛屿3
[题目链接]:http://hihocoder.com/problemset/problem/1487 [题意] 中文题 [题解] 岛屿的数目对应了这个图中联通块的数目; 面积则对应有多少个方块; 周 ...
- hihocoder [Offer收割]编程练习赛61
[Offer收割]编程练习赛61 A:最小排列 给定一个长度为m的序列b[1..m],再给定一个n,求一个字典序最小的1~n的排列A,使得b是A的子序列. 贪心即可,b是A的子序列,把不在b中的元素, ...
- hihocoder [Offer收割]编程练习赛4
描述 最近天气炎热,小Ho天天宅在家里叫外卖.他常吃的一家餐馆一共有N道菜品,价格分别是A1, A2, ... AN元.并且如果消费总计满X元,还能享受优惠.小Ho是一个不薅羊毛不舒服斯基的人,他希望 ...
- [Offer收割]编程练习赛46
[Offer收割]编程练习赛46赛后题解 A.AEIOU 分析
随机推荐
- 01背包模板、全然背包 and 多重背包(模板)
转载请注明出处:http://blog.csdn.net/u012860063 贴一个自觉得解说不错的链接:http://www.cppblog.com/tanky-woo/archive/2010/ ...
- 【Ubuntu】小技巧
1.在 usr/share/applications/ 中可以找到 .desktop 文件,修改其内容可以修改你的桌面快捷方式, 例如图标或者分类还可以新建你的 .desktop ,如果你安装的软件没 ...
- luogu1026 统计单词个数
题目大意 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1< k< =40),且每份中包含 ...
- luogu2278 [HNOI2003]操作系统
题目大意 写一个程序来模拟操作系统的进程调度.假设该系统只有一个CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的.其中运行优先级用自然数表示,数字越大,则优先级越高.如果一个进程到达的时候 ...
- python pickle to json
ref: https://gist.github.com/Samurais/567ebca0f59c612eb977065008aad867 ''' Convert a pkl file into j ...
- codeforces 899F Letters Removing set+树状数组
F. Letters Removing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- kafka参数在线修改
当kafka集群单个节点出现磁盘满了,需要清理历史topic数据:方法如下 1): 停掉kafka进程,将kafka的server.properties中的log.retention.hours=1/ ...
- MSSQL:查看作业情况
select j.name 'Job名', j.description '描述', j.ENABLED job_enabled, cast(js.last_r ...
- mybatis 传map参数
第一步在你的mapper写上: List<WeixinUserLocationList> findweixinUserLocations(@Param("params" ...
- Leetcode0037--Sudoku Solver 数独游戏
[转载请注明]http://www.cnblogs.com/igoslly/p/8719622.html 来看一下题目: Write a program to solve a Sudoku puzzl ...