Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- using namespace std;
- long long sqr(long long x) {
- return x * x;
- }
- int main() {
- long long sum_sqr = 0;
- int n;
- cin >> n;
- list<long long> segments;
- for (int i = 0; i < n; ++i) {
- long long len;
- cin >> len;
- segments.push_back(len);
- sum_sqr += sqr(len);
- }
- cout << sum_sqr << "\n";
- auto ptr = segments.begin();
- int index = 1;
- int k;
- cin >> k;
- for (int i = 0; i < k; ++i) {
- int e, v;
- cin >> e >> v;
- if (index < v) {
- while (index < v) {
- ++index;
- ++ptr;
- }
- } else {
- while (index > v) {
- --index;
- --ptr;
- }
- }
- long long len = *ptr;
- sum_sqr -= sqr(len);
- long long first_len = len / 2;
- long long second_len = len - first_len;
- if (e == 1) {
- auto next_ptr = ptr;
- ++next_ptr;
- if (next_ptr == segments.end()) { // ptr is the last element in list
- auto prev_ptr = ptr;
- --prev_ptr;
- sum_sqr -= sqr(*prev_ptr);
- *prev_ptr += len;
- sum_sqr += sqr(*prev_ptr);
- } else if (ptr == segments.begin()) {
- sum_sqr -= sqr(*next_ptr);
- *next_ptr += len;
- sum_sqr += sqr(*next_ptr);
- } else {
- auto prev_ptr = ptr;
- --prev_ptr;
- sum_sqr -= sqr(*prev_ptr);
- sum_sqr -= sqr(*next_ptr);
- *prev_ptr += first_len;
- *next_ptr += second_len;
- sum_sqr += sqr(*prev_ptr);
- sum_sqr += sqr(*next_ptr);
- }
- ptr = segments.erase(ptr);
- } else {
- *ptr = first_len;
- auto next_ptr = ptr;
- ++next_ptr;
- segments.insert(next_ptr, second_len);
- sum_sqr += sqr(first_len);
- sum_sqr += sqr(second_len);
- }
- cout << sum_sqr << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement