Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int, int>
- #define pb(x) push_back(x)
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvl(vector <ll> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvv(vector <vector <int> > &v) {
- for (auto x : v) cv(x);
- cout << "\n";
- }
- void cvb(vector <bool> v) {
- for (bool x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvs(vector <string> v) {
- for (auto a : v) {
- cout << a << "\n";
- }
- }
- void cvp(vector <pii> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- int n, N, L, R,k;
- bool sh = 0;
- struct tree{
- vector <ll> v;
- vector <ll> d;
- //d[id] - какое значение надо присвоить всем эелементам на отрезке в-ны id
- void bld(){
- cin >> n >> k;
- N = log2(n);
- if (pow(2, N) < n) N++;
- N = pow(2, N);
- v.assign(2 * N - 1, 0);
- d.assign(2 * N - 1, -1);
- }
- void seeT() {
- cout << "v\n";
- int cnt = -1;
- for (int i = 0; i <= log2(N); ++i) {
- for (int j = 0; j < pow(2, i); ++ j) {
- cnt ++;
- cout << v[cnt] << ' ';
- } cout << "\n";
- }
- cout << "\n";
- cout << "d\n";
- cnt = -1;
- for (int i = 0; i <= log2(N); ++i) {
- for (int j = 0; j < pow(2, i); ++ j) {
- cnt ++;
- cout << d[cnt] << ' ';
- } cout << "\n";
- }
- cout << "\n";
- }
- void push(int id, int l, int r) {
- if (sh) {
- cout << "push\n";
- cout << "l r = " << l << ' ' << r << "\n";
- }
- if (d[id] == -1) {
- return;
- }
- v[id] = d[id] * (r - l + 1);
- if (r > l) {
- d[2 * id + 1] = d[id];
- d[2 * id + 2] = d[id];
- }
- d[id] = -1;
- }
- ll get(int id, int l, int r) {
- push(id, l, r);
- if (l >= L && r <= R) {
- return v[id];
- }
- else if (max(l, L) <= min(r, R)) {
- int m = (l + r) / 2;
- return get(2 * id + 1, l, m) + get(2 * id + 2, m + 1, r);
- }
- return 0;
- }
- void upd(int id, int l, int r, ll x) {
- push(id, l, r);
- if (sh) {
- cout << "upd\n";
- cout << "l r = " << l << ' ' << r << "\n";
- }
- //не вошел
- if (r < L || l > R) {
- if (sh) {
- cout << "NO INTERSECTION\n";
- }
- return;
- }
- else if (l >= L && r <= R) {//полностью вошел
- if (sh) {
- cout << "COMPLETE intersetion\n";
- }
- d[id] = x;
- push(id, l, r);
- return;
- }
- //частичное пересечение
- if (sh) {
- cout << "part intersection\n";
- }
- int m = (l + r) / 2;
- upd(2 * id + 1, l, m, x);
- upd(2 * id + 2, m + 1, r, x);
- v[id] = v[id * 2 + 1] + v[id * 2 + 2];
- }
- };
- /*
- 5 9
- A 2 3 2
- A 3 5 1
- A 4 5 2
- Q 1 3
- Q 2 2
- Q 3 4
- Q 4 5
- Q 5 5
- Q 1 5
- */
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- tree T;
- T.bld();
- for (int i = 0; i < k ; ++ i) {
- char q; cin >> q;
- cin >> L >> R;
- L--; R--;
- if (q == 'A') {
- ll x; cin >> x;
- T.upd(0, 0, N - 1, x);
- }
- else{
- cout << T.get(0, 0, N - 1) << "\n";
- }
- if (sh) {
- T.seeT();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement