嘟嘟嘟




做了几道题之后,对整体二分有点感觉了。




整体二分的本质就是二分答案。所以这道题二分的就是次数。

然后就是套路了,把小于\(mid\)的操作都添加减去,然后查询,如果查询的值\(x\)比给定值大,就把这个询问放到左区间,否则减去\(x\),放到右区间。

具体的操作,要支持区间加和单点查,第一反应是线段树,结果有两个点TLE的很惨。所以改成了树状数组差分维护前缀和,竟然过了,而且还跑的特别块。常数真是致命啊……

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 3e5 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, m, k, val[maxn];
vector<int> con[maxn];
struct Node
{
int L, R, w, id;
}t[maxn << 1], tl[maxn << 1], tr[maxn << 1];
int ans[maxn]; ll c[maxn];
int lowbit(int x) {return x & -x;}
inline void clear(int pos)
{
for(; pos <= m; pos += lowbit(pos))
if(c[pos]) c[pos] = 0;
else break;
}
inline void add(int pos, int d)
{
if(!pos) return;
for(; pos <= m; pos += lowbit(pos)) c[pos] += d;
}
inline ll query(int pos)
{
ll ret = 0;
for(; pos; pos -= lowbit(pos)) ret += c[pos];
return ret;
} inline void Change(Node q)
{
if(q.L > q.R) add(q.L, q.w), add(1, q.w), add(q.R + 1, -q.w);
else add(q.L, q.w), add(q.R + 1, -q.w);
}
inline void Clear(Node q)
{
if(q.L > q.R) clear(q.L), clear(1), clear(q.R + 1);
else clear(q.L), clear(q.R + 1);
} inline void solve(int kl, int kr, int ql, int qr)
{
if(ql > qr) return;
if(kl == kr)
{
for(int i = ql; i <= qr; ++i)
if(t[i].id > k + 1) ans[t[i].id - k - 1] = kl;
return;
}
int mid = (kl + kr) >> 1, id1 = 0, id2 = 0;
for(int i = ql; i <= qr; ++i)
{
if(t[i].id <= k + 1)
{
if(t[i].id <= mid) Change(t[i]), tl[++id1] = t[i];
else tr[++id2] = t[i];
}
else
{
ll tot = 0; int x = t[i].id - k - 1;
for(int j = 0; j < (int)con[x].size(); ++j)
if((tot += query(con[x][j])) >= t[i].w) break;
if(tot >= t[i].w) tl[++id1] = t[i];
else t[i].w -= tot, tr[++id2] = t[i];
}
}
for(int i = 1; i <= id1; ++i) if(tl[i].id <= k + 1 && tl[i].id <= mid) Clear(tl[i]);
for(int i = 1; i <= id1; ++i) t[ql + i - 1] = tl[i];
for(int i = 1; i <= id2; ++i) t[ql + id1 + i - 1] = tr[i];
solve(kl, mid, ql, ql + id1 - 1);
solve(mid + 1, kr, ql + id1, qr);
} int main()
{
n = read(); m = read();
for(int i = 1, x; i <= m; ++i) x = read(), con[x].push_back(i);
for(int i = 1; i <= n; ++i) val[i] = read();
k = read();
for(int i = 1; i <= k; ++i)
t[i].L = read(), t[i].R = read(), t[i].w = read(), t[i].id = i;
t[k + 1] = (Node){1, m, INF, k + 1};
for(int i = 1; i <= n; ++i) t[k + 1 + i] = (Node){0, 0, val[i], k + 1 + i};
solve(1, k + 1, 1, k + n + 1);
for(int i = 1; i <= n; ++i)
if(ans[i] > k) puts("NIE");
else write(ans[i]), enter;
return 0;
}

[POI2011]Meteors的更多相关文章

  1. BZOJ2527: [Poi2011]Meteors

    补一发题解.. 整体二分这个东西,一开始感觉复杂度不是很靠谱的样子 问了po姐姐,说套主定理硬干.. #include<bits/stdc++.h> #define ll long lon ...

  2. 2527: [Poi2011]Meteors[整体二分]

    2527: [Poi2011]Meteors Time Limit: 60 Sec  Memory Limit: 128 MB Submit: 1528  Solved: 556 [Submit][S ...

  3. 【BZOJ2527】[Poi2011]Meteors 整体二分

    [BZOJ2527][Poi2011]Meteors Description Byteotian Interstellar Union (BIU) has recently discovered a ...

  4. 【bzoj2527】[Poi2011]Meteors(树状数组(单点查询,区间修改)+整体二分)

    [bzoj2527][Poi2011]Meteors Description Byteotian Interstellar Union (BIU) has recently discovered a ...

  5. bzoj 2527: [Poi2011]Meteors 整体二分

    给每个国家建一个链表,这样分治过程中的复杂度就和序列长度线形相关了,无脑套整体二分就可以. (最坑的地方是如果所有位置都是一个国家,那么它的样本个数会爆longlong!!被这个坑了一次,大于p[i] ...

  6. BZOJ2527[Poi2011]Meteors——整体二分+树状数组

    题目描述 Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The ...

  7. 【bzoj 2527】[Poi2011]Meteors

    Description Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby gala ...

  8. BZOJ2527 [Poi2011]Meteors 整体二分 树状数组

    原文链接http://www.cnblogs.com/zhouzhendong/p/8686460.html 题目传送门 - BZOJ2527 题意 有$n$个国家. 太空里有$m$个太空站排成一个圆 ...

  9. Luogu3527 POI2011 Meteors 整体二分、树状数组、差分

    传送门 比较板子的整体二分题目,时限有点紧注意常数 整体二分的过程中将时间在\([l,mid]\)之间的流星使用树状数组+差分进行维护,然后对所有国家查看一遍并分好类,递归下去,记得消除答案在\([m ...

  10. bzoj 2527: [Poi2011]Meteors

    昨天写了一晚,越写复杂度越感觉不对,早上一想果然是假的. (这里n,m,k我就不区分了) 首先一个城市的询问可以很容易的二分 check用树状数组维护区间(区间修改,单点查询的那种) 一次是\(O(n ...

随机推荐

  1. 安卓开发笔记①:利用高德地图API进行定位、开发电子围栏、天气预报、轨迹记录、搜索周边(位置)

    高德地图开发时需要导入的包在下面的网盘链接中:(由于高德地图api更新得太快,官网上最新的包使用起来没有之前的方便,所以以下提供最全面的原始包) 链接:http://pan.baidu.com/s/1 ...

  2. bootstrap-datepicker 开始时间-结束时间 thinkphp

    <!DOCTYPE html> <head> <title>开始-结束时间测试</title> </head> <body> & ...

  3. Java异常捕获之一道try-catch-finally语句题

    今天,学习了try-catch-finally语句,本来觉得蛮简单.易懂的.搜了一道相关类型的题.结果信心被泼了盆冷水.先把题Mark一下,出去透透风. public class TestEx { p ...

  4. Oracle总结之plsql编程(基础九)

    原创作品,转自请注明出处:https://www.cnblogs.com/sunshine5683/p/10344302.html 接着上次总结,继续今天的总结,今天主要总结plsql中控制语句,如条 ...

  5. LINQ to Objects系列(2)两种查询语法介绍

    LINQ为我们提供了两种查询语法,分别是查询表达式和查询方法语法.这篇文章分为以下几个方面进行总结. 1,一个包含两种查询语法的简单示例 2,查询表达式的结构 3,查询方法相关的运算符 一个包含两种查 ...

  6. AutoFac实现WebAPI依赖注入(EF以及Mysql)

    什么是依赖注入? 我们以实际的例子来加以介绍 实体如下 public class Product { public int ID { get; set; } public string Name { ...

  7. jQuery获取子元素的个数

    一.获取div下的子元素的个数 $("div").children().length; 二.获取div下的span子元素的个数 $("div").childre ...

  8. Thinkphp5+PHPExcel实现批量上传表格数据

    1.首先要下载PHPExcel放到vendor文件夹下,我的路径是:项目/vendor/PHPExcel/,把下载的PHPExcel文件放在这里 2.前端代码 <!DOCTYPE html> ...

  9. python中matplotlib.pyplot中cm的属性

    https://matplotlib.org/gallery/color/colormap_reference.html

  10. OSGI企业应用开发(二)Eclipse中搭建Felix运行环境

    上篇文章介绍了什么是OSGI以及使用OSGI构建应用的优点,接着介绍了两款常用的OSGI实现,分别为Apache Felix和Equinox,接下来开始介绍如何在Eclipse中使用Apache Fe ...