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. Python之简单Socket编程

    Socket编程这块儿还是比较重要的,记录一下:实现服务器端和客户端通信(客户端发送系统指令,如ipconfig等,服务器端执行该指令,然后将指令返回结果给客户端再传过去,设置一次最多直接收1024字 ...

  2. stm32L0系列学习(一)

    开发用到的具体芯片是stm32L011F3 stm32L0总体特性,定位: 可见容量是比较少的,功耗很低,adc12位,7种低功耗模式 jlink和sdk的引脚关系图: HAL的库框图 官方给出的HA ...

  3. Linux学习-Tarball 的管理与建议

    使用原始码管理软件所需要的基础软件 从原始码的说明我们晓得要制作一个 binary program 需要很多咚咚的呢!这包括底下这些基础的软件: gcc 或 cc 等 C 语言编译程序 (compil ...

  4. Hadoop4.2HDFS测试报告之二

    第一组:文件存储写过程记录 测试系统组成 存储类型 测试程序或命令 测试文件大小(Mb) 文件个数(个) 客户端并发数(个) 写速率(M/s) NameNode:1 DataNode:1 本地存储 s ...

  5. mongodb v2.4.9 快速操作记录

    参考链接:http://www.runoob.com/mongodb/mongodb-tutorial.html oschina链接:https://gitee.com/dhclly/icedog.s ...

  6. 《机器学习实战》笔记——AdaBoost

    笔记见备注 # _*_ coding:utf-8 _*_ from numpy import * # 简单数据集 def loadSimpData(): datMat = matrix([[1., 2 ...

  7. TOJ2680: 最大矩阵连乘次数

    2680: 最大矩阵连乘次数  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Submit: 144 ...

  8. 九度oj 题目1362:左旋转字符串(Move!Move!!Move!!!)

    题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”ab ...

  9. [AtCoderContest010D]Decrementing

    [AtCoderContest010D]Decrementing 试题描述 There are \(N\) integers written on a blackboard. The \(i\)-th ...

  10. (转)解决fasterxml中string字符串转对象json格式错误问题(无引号 单引号问题)

    原文地址:解决fasterxml中string字符串转对象json格式错误问题 com.fasterxml.jackson.databind.ObjectMapper mapper = new com ...