-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parallel Binary Search.cpp
82 lines (75 loc) · 1.24 KB
/
Parallel Binary Search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
int lo[N], mid[N], hi[N];
vector<int> vec[N];
void clear() //Reset
{
memset(bit, 0, sizeof(bit));
}
void apply(int idx) //Apply ith update/query
{
if(ql[idx] <= qr[idx])
update(ql[idx], qa[idx]), update(qr[idx]+1, -qa[idx]);
else
{
update(1, qa[idx]);
update(qr[idx]+1, -qa[idx]);
update(ql[idx], qa[idx]);
}
}
bool check(int idx) //Check if the condition is satisfied
{
int req=reqd[idx];
for(auto &it:owns[idx])
{
req-=pref(it);
if(req<0)
break;
}
if(req<=0)
return 1;
return 0;
}
void work()
{
for(int i=1;i<=q;i++)
vec[i].clear();
for(int i=1;i<=n;i++)
if(mid[i]>0)
vec[mid[i]].push_back(i);
clear();
for(int i=1;i<=q;i++)
{
apply(i);
for(auto &it:vec[i]) //Add appropriate check conditions
{
if(check(it))
hi[it]=i;
else
lo[it]=i+1;
}
}
}
void parallel_binary()
{
for(int i=1;i<=n;i++)
lo[i]=1, hi[i]=q+1;
bool changed = 1;
while(changed)
{
changed=0;
for(int i=1;i<=n;i++)
{
if(lo[i]<hi[i])
{
changed=1;
mid[i]=(lo[i] + hi[i])/2;
}
else
mid[i]=-1;
}
work();
}
}
//Problem 1: https://www.spoj.com/problems/METEORS/
//Solution 1: http://p.ip.fi/Z8qa
//Problem 2: https://atcoder.jp/contests/agc002/tasks/agc002_d
//Solution 2: http://p.ip.fi/C4ZG