码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-07-23 16:47:20      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:结束   hose   out   pre   stat   sig   rmi   push   future   

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


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

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

  单次询问二分答案使得总时间复杂度太高了,所以就把所有操作和询问放到一起进行二分答案。用一个数组来维护答案在当前二分的区间[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 }

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

标签:结束   hose   out   pre   stat   sig   rmi   push   future   

原文地址:http://www.cnblogs.com/yyf0309/p/7224882.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!