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>
- using namespace std;
- using ll = long long;
- using ld = long 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";
- }
- int l=0,r=10;
- int inter(int tl, int tr){
- if (tl >= l && tr <= r) return 2;
- else if (tr >= l && tl < l) return 1;
- else if (tl <= r && tr > r) return 1;
- else return 0;
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- int n;
- vector <int> a;
- struct tree{
- vector <ll> t;
- vector <ll> to_add;
- void bld(){
- int mxn = log2(n);
- cout<<"n mxn = "<<n<<' '<<mxn<<"\n";
- if (pow(2, mxn) < n){
- mxn++;
- }
- mxn = pow(2, mxn);
- t.assign(2 * mxn - 1, 0);
- to_add.assign(2 * mxn - 1, 0);
- a.resize(mxn);
- for (int i = 0; i < n; ++i){
- cin>>a[i];
- }
- int id = 2*mxn-2;
- for (int i = mxn-1; i >= 0; --i){
- t[id] = a[i];
- id--;
- }
- for (int i = 2 * mxn - 2; i >= 2; i -= 2){
- t[(i-1)/2] = t[i] + t[i-1];
- }
- }
- void sh(){
- cout<<"tree\n";
- cvl(t);
- }
- void push(int v, int tl, int tr){
- if (to_add[v] == 0) return;
- int to = 2*v+1;
- int tm = (tl + tr) / 2;
- to_add[to] += to_add[v];
- t[to] += to_add[v] * (tm - tl + 1);
- to++;
- to_add[to] = to_add[v];
- t[to] = to_add[v] * (tr - (tm + 1) + 1);
- to_add[v] = 0;
- }
- ll get(int v, int tl, int tr){
- if (inter(tl, tr) == 0) return 0;
- else if (inter(tl, tr) == 2){
- return t[v];
- }
- else if (inter(tl, tr) == 1){
- push(v, tl, tr);
- int tm = (tl + tr) / 2;
- return get(v*2+1,tl, tm) + get(2*v+2, tm+1, tr);
- }
- }
- void add(int v, int tl, int tr, int val){
- if (inter(tl, tr) == 0) return;
- else if (inter(tl, tr) == 2){
- to_add[v] += val;
- t[v] += val * (tr - tl + 1);
- }
- else{
- int tm = (tl + tr) / 2;
- add(2*v+1, tl, tm, val);
- add(2*v+2, tm+1, tr, val);
- }
- }
- void req(){
- char x; cin>>x;
- cin>>l>>r;
- if (x == 'm'){
- cout<<get(0,l,r);
- }else{
- int val; cin>>val;
- add(0, 0, n-1, val);
- }
- }
- };
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin>>n;
- tree wrk;
- wrk.bld();
- wrk.sh();
- //int a,b; cin>>a>>b; cout<<inter(a, b);
- }
- /*
- 5
- 2 4 3 1 5
- 5
- m 1 3
- a 2 4 100
- m 1 3
- a 5 5 10
- m 1 5
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement