-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathballoons.cpp
107 lines (81 loc) · 1.96 KB
/
balloons.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "stdio.h"
#include "iostream"
#include "vector"
#include "algorithm"
#define get getchar_unlocked
using namespace std;
inline long int scan2(){
long int n=0,s=1;
char p=get();
if(p=='-') s=-1;
while((p<'0'||p>'9')&&p!=EOF&&p!='-') p=get();
if(p=='-') s=-1,p=get();
while(p>='0'&&p<='9') { n = (n<< 3) + (n<< 1) + (p - '0'); p=get(); }
return n*s;
}
struct my_pair{
long int l;
long int r;
unsigned long long int contains;
};
int main(){
long int i = 0,k=0;
long int boxes[100002]; //Contains the number of boxes.
long int n_boxes,box_no;
long int m_pairs;
long int n_bursts;
long int templ,tempr;
// long int belongsto[100][100]; //This contains the array of my_pairs a box belongs to
vector< int > a (1,0);
vector< vector<int> > belongsto(100001,a);
long int ans_pre,ans,x,sum,j,y;
struct my_pair my_pairs[100002];
n_boxes = scan2();
m_pairs = scan2();
for(i=1;i<=n_boxes;i++){
boxes[i] = scan2();
}
for(i=1;i<=m_pairs;i++){
templ = scan2();
tempr = scan2();
my_pairs[i].l = templ;
my_pairs[i].r = tempr;
sum = 0;
for(j=templ;j<=tempr;j++){
sum += boxes[j];
}
my_pairs[i].contains = sum;
}
//Working fine upto this
for(i=1;i<=n_boxes;i++){
belongsto[i].reserve(100);
for(j=1;j<=m_pairs;j++){
if(my_pairs[j].l <= i && my_pairs[j].r >= i){
belongsto[i][0]++;
belongsto[i].push_back(j);
}
}
}
// for(i=1;i<=n_boxes;i++){
// printf("%ld box belongs to %d my_pairs\n",i,belongsto[i][0]);
// for(j=1;j<=belongsto[i][0];j++){
// printf("%d ",belongsto[i][j]);
// }
// printf("\n");
// }
n_bursts = scan2();
ans_pre = 0;
ans = 0;
for(i=1;i<=n_bursts;i++){
x = scan2();
y = x + ans_pre; //Now ans contains the index of the box of which the ballonng in burst;
//printf("boxindex = %ld corresponding to x = %ld\n",y,x);
for(k=1;k <= belongsto[y][0];k++){
if(--my_pairs[belongsto[y][k]].contains == 0)
ans++;
}
printf("%ld\n",ans);
ans_pre = ans;
}
return 0;
}