bzoj 2527 Meteors - 整体二分 - 树状数组

Description

Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The planet is unsuitable for colonisation due to strange meteor showers, which on the other hand make it an exceptionally interesting object of study.

The member states of BIU have already placed space stations close to the planet‘s orbit. The stations‘ goal is to take samples of the rocks flying by. The BIU Commission has partitioned the orbit into msectors, numbered from 1to m, where the sectors 1and mare adjacent. In each sector there is a single space station, belonging to one of the nmember states.

Each state has declared a number of meteor samples it intends to gather before the mission ends. Your task is to determine, for each state, when it can stop taking samples, based on the meter shower predictions for the years to come.

Byteotian Interstellar Union有N个成员国。现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站。

这个星球经常会下陨石雨。BIU已经预测了接下来K场陨石雨的情况。
BIU的第i个成员国希望能够收集Pi单位的陨石样本。你的任务是判断对于每个国家,它需要在第几次陨石雨之后,才能收集足够的陨石。
输入:
第一行是两个数N,M。
第二行有M个数,第i个数Oi表示第i段轨道上有第Oi个国家的太空站。
第三行有N个数,第i个数Pi表示第i个国家希望收集的陨石数量。
第四行有一个数K,表示BIU预测了接下来的K场陨石雨。
接下来K行,每行有三个数Li,Ri,Ai,表示第K场陨石雨的发生地点在从Li顺时针到Ri的区间中(如果Li<=Ri,就是Li,Li+1,...,Ri,否则就是Ri,Ri+1,...,m-1,m,1,...,Li),向区间中的每个太空站提供Ai单位的陨石样本。
输出:
N行。第i行的数Wi表示第i个国家在第Wi波陨石雨之后能够收集到足够的陨石样本。如果到第K波结束后仍然收集不到,输出NIE。
数据范围:

数据范围: 1<=n,m,k<=3*10^5 1<=Pi<=10^9 1<=Ai<10^9

Input

The first line of the standard input gives two integers, n and m(1<=n,m<=3*10^5) separated by a single space, that denote, respectively, the number of BIU member states and the number of sectors the orbit has been partitioned into.

In the second line there are mintegers Oi(1<=Oi<=n) separated by single spaces, that denote the states owning stations in successive sectors.

In the third line there are nintegers Pi(1<=Pi<=10^9) separated by single spaces, that denote the numbers of meteor samples that the successive states intend to gather.

In the fourth line there is a single integer k(1<=k<=3*10^5) that denotes the number of meteor showers predictions. The following klines specify the (predicted) meteor showers chronologically. The i-th of these lines holds three integers Li, Ri, Ai(separated by single spaces), which denote that a meteor shower is expected in sectors Li,Li+1,…Ri (if Li<=Ri) or sectors Li,Li+1,…,m,1,…Ri (if Li>Ri), which should provide each station in those sectors with Aimeteor samples (1<=Ai<10^9).

In tests worth at least 20% of the points it additionally holds that .

Output

Your program should print nlines on the standard output. The i-th of them should contain a single integer Wi, denoting the number of shower after which the stations belonging to the i-th state are expected to gather at least Pi samples, or the word NIE (Polish for no) if that state is not expected to gather enough samples in the foreseeable future.

Sample Input

3 5
1 3 2 1 3
10 5 7
3
4 2 4
1 3 1
3 5 2

Sample Output

3
NIE
1

HINT

Source

鸣谢 Object022



  因为之前用可持久化线段树被卡内存了,表示不爽,于是用整体二分来"水掉"这道题。

  整体二分其实并没有什么特别高端的东西。

  单次询问二分答案使得总时间复杂度太高了,所以就把所有操作和询问放到一起进行二分答案。用一个数组来维护答案在当前二分的区间[l, r]内的询问。在每一层中"暴力"进行用数据结构进行计算1 ~ mid的操作对答案的贡献,如果比需求的多或者等于,就扔进左区间的队列,然后递归处理,否则扔进右区间的队列递归处理。

  同时注意保障每一层的时间复杂度,最坏的情况下,"解答树"是一棵满二叉树,如果真的是暴力执行 1 ~ mid 的操作和直接二分答案就没什么区别了(所以说要巧妙地利用已有的计算结果)。

  对于这道题,这个数据结构就用树状数组就好了(没有必要出动线段树这种大牛数据结构)

Code

  1 /**
  2  * bzoj
  3  * Problem#2527
  4  * Accepted
  5  * Time:13520ms
  6  * Memory:16336k
  7  */
  8 #include <iostream>
  9 #include <cstdio>
 10 #include <ctime>
 11 #include <cmath>
 12 #include <cctype>
 13 #include <cstring>
 14 #include <cstdlib>
 15 #include <fstream>
 16 #include <sstream>
 17 #include <algorithm>
 18 #include <map>
 19 #include <set>
 20 #include <stack>
 21 #include <queue>
 22 #include <vector>
 23 #ifndef WIN32
 24 #define Auto "%lld"
 25 #else
 26 #define Auto "%I64d"
 27 #endif
 28 using namespace std;
 29 typedef bool boolean;
 30 const signed int inf = (signed)((1u << 31) - 1);
 31 const signed long long llf = (signed long long)((1ull << 63) - 1);
 32 const double eps = 1e-6;
 33 const int binary_limit = 128;
 34 #define smin(a, b) a = min(a, b)
 35 #define smax(a, b) a = max(a, b)
 36 #define max3(a, b, c) max(a, max(b, c))
 37 #define min3(a, b, c) min(a, min(b, c))
 38 template<typename T>
 39 inline boolean readInteger(T& u){
 40     char x;
 41     int aFlag = 1;
 42     while(!isdigit((x = getchar())) && x != ‘-‘ && x != -1);
 43     if(x == -1) {
 44         ungetc(x, stdin);
 45         return false;
 46     }
 47     if(x == ‘-‘){
 48         x = getchar();
 49         aFlag = -1;
 50     }
 51     for(u = x - ‘0‘; isdigit((x = getchar())); u = (u * 10) + x - ‘0‘);
 52     ungetc(x, stdin);
 53     u *= aFlag;
 54     return true;
 55 }
 56
 57 #define LL long long
 58 #define lowbit(x) (x & (-x))
 59
 60 typedef class IndexedTree {
 61     public:
 62         LL* a;
 63         int local;
 64         int s;
 65         IndexedTree():a(NULL), s(0), local(0) {        }
 66         IndexedTree(int n):s(n), local(0) {
 67             a = new LL[(n + 2)];
 68             memset(a, 0, sizeof(LL) * (n + 2));
 69         }
 70
 71         inline void add(int idx, LL val) {
 72             for(; idx <= s; idx += lowbit(idx))
 73                 a[idx] += val;
 74         }
 75
 76         inline void add(int l, int r, LL val) {
 77             add(l, val);
 78             add(r + 1, -val);
 79         }
 80
 81         inline LL getSum(int idx) {
 82             LL rt = 0;
 83             for(; idx; idx -= lowbit(idx))
 84                 rt += a[idx];
 85             return rt;
 86         }
 87 }IndexedTree;
 88
 89 typedef class Updater {
 90     public:
 91         int l;
 92         int r;
 93         int val;
 94 }Updater;
 95
 96 int n, m, q;
 97 vector<int> *owns;
 98 int* goals;
 99 Updater *us;
100
101 inline void init() {
102     readInteger(n);
103     readInteger(m);
104     owns = new vector<int>[(n + 1)];
105     goals = new int[(n + 1)];
106     for(int i = 1, x; i <= m; i++) {
107         readInteger(x);
108         owns[x].push_back(i);
109     }
110     for(int i = 1; i <= n; i++)
111         readInteger(goals[i]);
112     readInteger(q);
113     us = new Updater[(q + 2)];
114     for(int i = 1; i <= q; i++) {
115         readInteger(us[i].l);
116         readInteger(us[i].r);
117         readInteger(us[i].val);
118     }
119     us[q + 1] = (Updater) {1, 1, 0};
120 }
121
122 inline void modify(IndexedTree& it, int i, int sign) {
123     if(us[i].l > us[i].r)
124         it.add(us[i].l, m, us[i].val * sign), it.add(1, us[i].r, us[i].val * sign);
125     else
126         it.add(us[i].l, us[i].r, us[i].val * sign);
127 }
128
129 int* res;
130 IndexedTree it;
131 void CDQDividing(queue<int> &que, int l, int r) {
132     if(que.empty())    return;
133     if(l == r) {
134         while(!que.empty()) {
135             int x = que.front();
136             que.pop();
137             res[x] = l;
138         }
139         return;
140     }
141
142     int mid = (l + r) >> 1;
143     while(it.local < mid)
144         modify(it, ++it.local, 1);//, cntit++;
145     while(it.local > mid)
146         modify(it, it.local--, -1);//, cntit++;
147
148 //    fprintf(stderr, "dep-%d  time %dms %d", dep, clock(), cntit);
149
150     queue<int> ql, qr;
151     while(!que.empty()) {
152         int i = que.front();
153         que.pop();
154         LL s = 0;
155         for(int j = 0; j < (signed)owns[i].size() && s <= goals[i]; j++)
156             s += it.getSum(owns[i][j]);
157         if(s < goals[i])
158             qr.push(i);
159         else
160             ql.push(i);
161     }
162
163 //    while(!que.empty())    que.pop();
164 //    memset(it.a, 0, sizeof(LL) * (m + 2));
165     CDQDividing(ql, l, mid);
166     CDQDividing(qr, mid + 1, r);
167 }
168
169 queue<int> que;
170 inline void solve() {
171     res = new int[(n + 1)];
172     for(int i = 1; i <= n; i++)
173         que.push(i);
174     it = IndexedTree(m + 1);
175     CDQDividing(que, 1, q + 1);
176     for(int i = 1; i <= n; i++)
177         if(res[i] <= q)
178             printf("%d\n", res[i]);
179         else
180             puts("NIE");
181 }
182
183 int main() {
184 //    freopen("meteors.in", "r", stdin);
185 //    freopen("meteors.out", "w", stdout);
186     init();
187     solve();
188     return 0;
189 }
时间: 2024-10-06 10:12:15

bzoj 2527 Meteors - 整体二分 - 树状数组的相关文章

【BZOJ-2527】Meteors 整体二分 + 树状数组

2527: [Poi2011]Meteors Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 831  Solved: 306[Submit][Status][Discuss] Description Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The planet is unsuitable for colo

【bzoj2527】[Poi2011]Meteors 整体二分+树状数组

题目描述 有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下陨石雨.BIU已经预测了接下来K场陨石雨的情况.BIU的第i个成员国希望能够收集Pi单位的陨石样本.你的任务是判断对于每个国家,它需要在第几次陨石雨之后,才能收集足够的陨石. 输入 第一行是两个数N,M. 第二行有M个数,第i个数Oi表示第i段轨道上有第Oi个国家的太空站. 第三行有N个数,第i个数Pi表示第i个国家希望收集的陨石数量. 第四行有一个

【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询

Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M 接下来M行,每行形如1 a b c或2 a b c Output 输出每个询问的结果 Sample Input 2 5 1 1 2 1 1 1 2 2 2 1 1 2 2 1 1 1 2 1 2 3 Sample Output 1 2 1 HINT

【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改

题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. 输入 第一行N,M接下来M行,每行形如1 a b c或2 a b c 输出 输出每个询问的结果 样例输入 2 5 1 1 2 1 1 1 2 2 2 1 1 2 2 1 1 1 2 1 2 3 样例输出 1 2 1 题解 整体二分+树状数组区间修改 当年naive的树套树题解 前两天由于要

luogu3242 接水果 (整体二分+树状数组)

考虑整体二分,问题就变成了每个(水果)路径有多少个满足条件(权值)的(盘子)子路径 考虑一个盘子(a,b)表示两端点(不妨设dfn[a]<dfn[b]),那么他能接到的水果(u,v)一定满足(不妨设dfn[u]<dfn[v]): 1.如果a是b的祖先,则u在(a的在(b,a)链上的孩子)这个子树外,v在b子树内 2.否则,u在a的子树内,v在b的子树内 那么把一个水果(a,b)看成是一个二维点(dfn[a],dfn[b]),对于每个盘子,就是做一个二维区间+1 差分以后变成一个二维数点问题,可

【POJ2104】【整体二分+树状数组】区间第k大

Description You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array

BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组

Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M 接下来M行,每行形如1 a b c或2 a b c Output 输出每个询问的结果 Sample Input 2 5 1 1 2 1 1 1 2 2 2 1 1 2 2 1 1 1 2 1 2 3 Sample Output 1 2 1 solve

11525 - Permutation(二分+树状数组)

题目链接:点击打开链接 题意:从1~k的所有排列中找到第n个排列, n由公式给出. 思路:可以发现, 这个公式就是康托展开公式(康托展开百科:点击打开链接). 那么s[i]的意思就是i个数中当前数排在第几. 如此, 可以用二分+树状数组快速求解, 和一道BC题目神似. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<st

Codeforces 374D Inna and Sequence 二分+树状数组

题目链接:点击打开链接 给定n个操作,m长的序列a 下面n个数 if(co>=0)则向字符串添加一个co (开始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞,简单模拟 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h&