Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4b9a4a8

Browse files
Added problems
1 parent 238082e commit 4b9a4a8

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed
File renamed without changes.

‎others/fenwick-tree.cpp‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int lli;
6+
typedef vector<lli> vi;
7+
8+
class FenwickTree
9+
{
10+
11+
vi BITree;
12+
lli n;
13+
14+
public:
15+
FenwickTree(lli n)
16+
{
17+
this->BITree.resize(n + 1, 0);
18+
this->n = n;
19+
}
20+
21+
void update(lli pos, lli ele)
22+
{
23+
24+
lli curr = this->BITree[pos];
25+
lli diff = ele - curr;
26+
lli idx = pos;
27+
28+
while (idx <= n)
29+
{
30+
BITree[idx] += diff;
31+
idx += idx & (-idx);
32+
}
33+
}
34+
35+
lli getSum(lli idx)
36+
{
37+
38+
lli sum = 0;
39+
40+
while (idx)
41+
{
42+
sum += this->BITree[idx];
43+
idx -= idx & (-idx);
44+
}
45+
46+
return sum;
47+
}
48+
49+
};
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int lli;
6+
typedef vector<lli> vi;
7+
typedef tuple<lli, lli> ii;
8+
typedef tuple<lli, lli, lli> iii;
9+
10+
class FenwickTree
11+
{
12+
13+
vi BITree;
14+
lli n;
15+
16+
public:
17+
FenwickTree(lli n)
18+
{
19+
this->BITree.resize(n + 1, 0);
20+
this->n = n;
21+
}
22+
23+
void add(lli pos, lli ele)
24+
{
25+
lli idx = pos;
26+
while (idx <= n)
27+
{
28+
BITree[idx] += ele;
29+
idx += idx & (-idx);
30+
}
31+
}
32+
33+
lli prefixSum(lli idx)
34+
{
35+
36+
lli sum = 0;
37+
38+
while (idx)
39+
{
40+
sum += this->BITree[idx];
41+
idx -= idx & (-idx);
42+
}
43+
44+
return sum;
45+
}
46+
47+
lli rangeSum(lli a, lli b) {
48+
return this->prefixSum(b) - this->prefixSum(a-1);
49+
}
50+
};
51+
52+
void task()
53+
{
54+
55+
lli lenNumbers, lenQueries;
56+
cin >> lenNumbers >> lenQueries;
57+
58+
vi numbers(lenNumbers + 1);
59+
60+
FenwickTree ft(lenNumbers);
61+
62+
numbers[0] = 0;
63+
64+
for (lli i = 1; i <= lenNumbers; i++)
65+
{
66+
cin >> numbers[i];
67+
ft.add(i, numbers[i]);
68+
}
69+
70+
lli type, a, b;
71+
vi res;
72+
73+
for (lli i = 0; i < lenQueries; i++)
74+
{
75+
cin >> type >> a >> b;
76+
if (type == 2)
77+
{
78+
res.emplace_back(ft.rangeSum(a, b));
79+
}
80+
else
81+
{
82+
ft.add(a, b - numbers[a]);
83+
numbers[a] = b;
84+
}
85+
}
86+
87+
for (auto r : res)
88+
{
89+
cout << r << '\n';
90+
}
91+
}
92+
93+
int main()
94+
{
95+
ios::sync_with_stdio(false);
96+
cin.tie(NULL);
97+
98+
task();
99+
100+
return 0;
101+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int lli;
6+
typedef vector<lli> vi;
7+
typedef tuple<lli, lli> ii;
8+
typedef tuple<lli, lli, lli> iii;
9+
10+
void task()
11+
{
12+
13+
lli lenNumbers, lenQueries;
14+
cin >> lenNumbers >> lenQueries;
15+
16+
vi numbers(lenNumbers + 1);
17+
18+
numbers[0] = 0;
19+
20+
for (lli i = 1; i <= lenNumbers; i++)
21+
{
22+
cin >> numbers[i];
23+
}
24+
25+
vi prefix(lenNumbers + 1, 0);
26+
27+
for (lli i = 1; i <= lenNumbers; i++)
28+
{
29+
prefix[i] = prefix[i - 1] + numbers[i];
30+
}
31+
32+
lli type, a, b;
33+
vi res;
34+
35+
for (lli i = 0; i < lenQueries; i++)
36+
{
37+
cin >> type >> a >> b;
38+
if (type == 2)
39+
{
40+
res.emplace_back(prefix[b] - prefix[a - 1]);
41+
}
42+
else
43+
{
44+
lli diff = b - numbers[a];
45+
numbers[a] = b;
46+
47+
for (lli j = a; j <= lenNumbers; j++)
48+
{
49+
prefix[j] += diff;
50+
}
51+
}
52+
}
53+
54+
for (auto r : res)
55+
{
56+
cout << r << '\n';
57+
}
58+
}
59+
60+
int main()
61+
{
62+
ios::sync_with_stdio(false);
63+
cin.tie(NULL);
64+
65+
task();
66+
67+
return 0;
68+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /