嘟嘟嘟




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




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

然后就是套路了,把小于\(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. [日常] Linux下vim的常用命令总结

    vim按d表示剪切按dd剪切一行vim命令:命令模式 /关键字 n继续向下查找 vim的多行注释:1.按ctrl + v进入 visual block模式2.按上下选中要注释的行3.按大写字母I,再插 ...

  2. Linux常用基本命令( ls, alias)

    ls命令 作用:列举目录文件信息( list directory content ) 格式:ls [option] [file] 1,命令不跟任何选项与目录,表示列举当前目录的文件信息 ghostwu ...

  3. POJ2104(可持久化线段树)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 58759   Accepted: 20392 Ca ...

  4. JS 正则截取字符串

    1.js截取两个字符串之间的内容: varstr = "aaabbbcccdddeeefff"; str = str.match(/aaa(\S*)fff/)[1]; alert( ...

  5. BZOJ5323:[JXOI2018]游戏

    传送门 不难发现,所有不能被其他数筛掉的数是一定要选的,只有选了这些数字才能结束 假设有 \(m\) 个,枚举结束时间 \(x\),答案就是 \(\sum \binom{x-1}{m-1}m!(n-m ...

  6. 解除mysql只有本机可以访问的限制

    1.终端:sudo vim /etc/mysql/my.cnf 2.找到bind-address,注释掉(前面加#) 这里出现问题,发现my.cnf打开根本没有bind-address,但是多了两句话 ...

  7. 关于kernel-devel、kernel-header和kernel src的区别

    A kernel-header package would contain 'header files' needed by some applications which would be buil ...

  8. SQLSERVER中的元数据锁

    SQLSERVER中的元数据锁 网上对于元数据锁的资料真的非常少 元数据锁一般会出现在DDL语句里 下面列出数据库引擎可以锁定的资源 资源 说明 RID 用于锁定堆(heap)中的某一行 KEY 用于 ...

  9. C#多线程的用法8-线程间的协作AutoResetEvent

    AutoResetEvent自动重置事件,与ManualResetEvent是相对的而言.它同样用于线程间同步,请对照<C#多线程的用法7-线程间的协作ManualResetEvent>进 ...

  10. SQL Server中怎么查看每个数据库的日志大小,以及怎么确定数据库的日志文件,怎么用语句收缩日志文件

    一,找到每个数据库的日志文件大小 SQL Server:查看SQL日志文件大小命令:dbcc sqlperf(logspace) DBA 日常管理工作中,很重要一项工作就是监视数据库文件大小,及日志文 ...