BZOJ2527Meteors
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的更多相关文章
随机推荐
- Do not pour out HDU - 5954 数学积分
题目:题目链接 思路:纯高等数学问题,不过不是很好积分,具体积分思路及过程参考大佬博客——https://blog.csdn.net/danliwoo/article/details/53002695 ...
- poj 2718 切数问题 穷竭搜索
题意: 给一个已经排序号的数字,从中间切一刀,成两个数,要求这两个数的差最小 思路:暴力比较差最小值 stl中的next_permutation()函数进行排列 注意:这个函数必须从小到大才可以排序 ...
- poj 2531 分权问题 dfs算法
题意:一个集合(矩阵) m[i][j]=m[j][i]权值,分成两个集合,使其权值最大.注:在同一个集合中权值只能算一个. 思路:dfs 假设都在集合0 遍历 id 的时候拿到集合1 如果与 id 相 ...
- Latex:插入伪代码
*本文属于转载. *转载链接:https://blog.csdn.net/lwb102063/article/details/53046265 目录 clrscode algorithm algori ...
- Linux学习-编译前的任务:认识核心与取得核心原始码
什么是核心 (Kernel) Kernel 其实核心就是系统上面的一个文件而已, 这个文件包含了驱动主机各项硬 件的侦测程序与驱动模块. 核心文件通常被放置成 /boot/vmlinuz-xxx ,不 ...
- bash实例
1写一个脚本,完成如下功能(使用函数):1.脚本使用格式:mkscript.sh [-D|--description "script description"] [-A|--aut ...
- ubuntu 设置Path 开机启动脚本
vim /etc/rc.local export PATH=$PATH:/work/apps/node-v0.12.7-linux-x64/bin:/work/apps/ledisdb/binexpo ...
- 可持久化treap(FHQ treap)
FHQ treap 的整理 treap = tree + heap,即同时满足二叉搜索树和堆的性质. 为了使树尽可能的保证两边的大小平衡,所以有一个key值,使他满足堆得性质,来维护树的平衡,key值 ...
- CodeForces Round #320 Div2
A. Raising Bacteria 计算一下x的bitcount就是答案. #include <iostream> #include <cstdio> #include & ...
- java内存模型学习
根据 JVM 规范,JVM 内存共分为虚拟机栈.堆.方法区.程序计数器.本地方法栈五个部分. 虚拟机的内存模型分为两部分:一部分是线程共享的,包括 Java 堆和方法区:另一部分是线程私有的,包括虚拟 ...