BZOJ2527

整体二分模板题

整体二分: 主要用于解决第K大问题

#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<string>
#include<climits>
using namespace std;
inline int read()
{
int x = 0, flag = 1;
char c;
while(! isgraph(c = getchar()))
if(c == '-')
flag *= - 1;
while(isgraph(c))
x = x * 10 + c - '0', c = getchar();
return x * flag;
}
void println()
{
putchar('N'), putchar('I'), putchar('E'), putchar('\n');
}
void println(int x)
{
if(x < 0)
putchar('-'), x *= - 1;
if(x == 0)
putchar('0');
int ans[1 << 4], top = 0;
while(x)
ans[top ++] = x % 10, x /= 10;
for(; top; top --)
putchar(ans[top - 1] + '0');
putchar('\n');
}
const int MAXN = 1 << 19, MAXM = 1 << 19, MAXK = 1 << 19;
int n, m;
vector<int> a[MAXN];
int p[MAXN];
int ID[MAXN];
struct rain
{
int L, R, delta;
rain(){}
rain(int L, int R, int delta): L(L), R(R), delta(delta){}
}opt[MAXK << 1];
const int oo = INT_MAX;
int T;
long long tree[MAXM];
void add(int u, int delta)
{
for(int i = u; i <= m; i += (i & (- i)))
tree[i] += (long long)delta;
}
void modify(int u, int flag)
{
if(opt[u].L <= opt[u].R)
add(opt[u].L, flag * opt[u].delta), add(opt[u].R + 1, - flag * opt[u].delta);
else
{
add(1, flag * opt[u].delta), add(opt[u].R + 1, - flag * opt[u].delta);
add(opt[u].L, flag * opt[u].delta);
}
}
long long query(int u)
{
long long ret = 0;
for(int i = u; i; i -= (i & (- i)))
ret += tree[i];
return ret;
}
int tmp[MAXN], mark[MAXN], ans[MAXN];
void solve(int L, int R, int optL, int optR)
{
if(optL >= optR)
{
for(int i = L; i <= R; i ++)
ans[ID[i]] = optL;
return;
}
int mid = (optL + optR) >> 1;
while(T < mid)
modify(++ T, 1);
while(T > mid)
modify(T --, - 1);
int cnt = 0;
for(int i = L; i <= R; i ++)
{
long long sum = 0, now = ID[i];
for(int j = 0; j < a[now].size(); j ++)
{
sum += query(a[now][j]);
if(sum >= p[now])
break;
}
if(sum >= p[now])
mark[now] = 1, cnt ++;
else
mark[now] = 0;
}
int L1 = L, L2 = L + cnt;
for(int i = L; i <= R; i ++)
if(mark[ID[i]])
tmp[L1 ++] = ID[i];
else
tmp[L2 ++] = ID[i];
for(int i = L; i <= R; i ++)
ID[i] = tmp[i];
solve(L, L1 - 1, optL, mid);
solve(L1, L2 - 1, mid + 1, optR);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("BZOJ2527.in", "r", stdin);
freopen("BZOJ2527.out", "w", stdout);
#endif
n = read(), m = read();
for(int i = 1; i <= m; i ++)
a[read()].push_back(i);
for(int i = 1; i <= n; i ++)
p[i] = read();
int k = read();
int cnt;
for(int i = 1; i <= k; i ++)
{
int L = read(), R = read(), delta = read();
opt[i] = rain(L, R, delta);
}
opt[k + 1] = rain(1, m, oo);
int T = 0;
memset(tree, 0, sizeof(tree));
for(int i = 1; i <= n; i ++)
ID[i] = i;
solve(1, n, 1, k + 1);
for(int i = 1; i <= n; i ++)
{
if(ans[i] > k)
println();
else
println(ans[i]);
}
}

BZOJ2527Meteors的更多相关文章

随机推荐

  1. Hadoop4.2HDFS测试报告之五

    第二组:文件存储读过程记录 NameNode:1 DataNode:1 本地存储 scp romotepath localpath 500 2 1 23.05 NameNode:1 DataNode: ...

  2. studio rendering problems

    问题--------> Rendering Problems The following classes could not be instantiated: - android.support ...

  3. Hydux: 一个 Elm-like 的 全功能的 Redux 替代品

    在学习和使用 Fable + Elmish 一段时间之后,对 Elm 架构有了更具体的了解, 和预料中的一样,Redux 这种来自 Elm 的风格果然还是和强类型的 Meta Language 语言更 ...

  4. Notepad++ WebEdit插件

    虽然PHP的IDE有很多,但是,我还是比较喜欢用Notepad++这款编辑器,因为比较轻量级,而且用起来比较顺手. 但是最近在改别人写的代码的时候,经常要在选定的php前后插入<?php  ?& ...

  5. [python篇] [伯乐在线][1]永远别写for循环

    首先,让我们退一步看看在写一个for循环背后的直觉是什么: 1.遍历一个序列提取出一些信息 2.从当前的序列中生成另外的序列 3.写for循环已经是我的第二天性了,因为我是一个程序员 幸运的是,Pyt ...

  6. 紫书第三章训练1 E - DNA Consensus String

    DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of ...

  7. 建立RSA协商加密的安全信道

    在基于TCP长连接的CS链路中,如何保证数据流的安全性是开发者最关注的问题之一.本文深入浅出的给大家介绍一下在TCP连接中,使用RSA协商加密的方式,建立一个安全加密的通信链路,保证数据传输的安全性. ...

  8. 漫谈登录桩(MockStub)的实现

    2014年6月4日,6月的第一个星期三,我正式入职百度,开始baiduer的工作.这不到2个月的时间,因为人力资源这边原因,我从INF部门离开,拉到了百度Hi-Server团队中来.2个完全不着调的岗 ...

  9. 【bzoj4184】shallot 线段树+高斯消元动态维护线性基

    题目描述 小苗去市场上买了一捆小葱苗,她突然一时兴起,于是她在每颗小葱苗上写上一个数字,然后把小葱叫过来玩游戏. 每个时刻她会给小葱一颗小葱苗或者是从小葱手里拿走一颗小葱苗,并且 让小葱从自己手中的小 ...

  10. BZOJ4514 [Sdoi2016]数字配对 【费用流】

    题目 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci×c ...