So i had recently encountered with problem 380C - Sereja and Brackets I solved this problem using vector<vector<int>>
of size (4*n X 3) which results in TLE. 299190493.
Basic segment tree was looking like this
vector<vector<int>> seg;
SegmentTree(int n) {
seg.resize(4 * n, {0ll, 0ll, 0ll});
}
Then i just replace this part of code with
class node {
public:
int full;
int opn;
int cls;
};
vector<node> seg;
SegmentTree(int n) {
seg.resize(4 * n);
}
And this code got accepted.299237670
Does this means using class or struct can reduce time complexity although i know 1st code will occupy more memory as compared to 2nd but i am not sure about time. If anyone have any insights, could you please explain why this happened? Do they differ in terms of time complexity?
Using
array<int, 3>
also results in AC. 299248827Means this issue arises only when using vectors
Use
tuple<int, int, int>
or astruct
as you did.vector<int>
has large overhead when you need to just a small constant number of elements, because it needs to allocate dynamic memory (vianew
) and do some extra bookkeeping (like the number of elements stored).Okay now i got it, also i think this question has very strict time limit otherwise this is not a very big problem (regarding time complexity).Right?
The time complexity is same, but you should generally avoid it because using
vector
is just wasteful for this purpose.