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 pbl pair <bool, long long>
- 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, mxn, K; //mxn - округлена до верхней стпени двойки
- //в конце ф-иии build: n = mxn
- vector <int> a;
- ll inf = 2e9;
- struct tree{
- vector <ll> t;
- vector < ll > to_push;//куда что присвоить
- void bld(){
- 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_push.resize(2 * mxn - 1, -1 );
- a.resize(mxn, 0);
- for (int i = 0; i < n; ++i){
- //cin>>a[i];
- a[i] = 0;
- }
- 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];
- }
- n = mxn;
- }
- void sh(){
- cout<<"tree\n";
- cvl(t);
- cout<<"\n\n";
- }
- void push(int v, int tl, int tr){
- if (to_push[v] == -1) return;
- t[v] = (tr - tl + 1) * to_push[v];
- if (tl != tr){
- to_push[2*v+1] = to_push[v];
- to_push[2*v + 2] = to_push[v];
- }
- to_push[v] = -1;
- }
- ll get(int v, int tl, int tr){
- push(v, tl, tr);
- //cout<<"tl tr = "<<tl<<' '<<tr<<"\n";
- if (inter(tl, tr) == 0) {
- //cout<<"not intersect\n";
- return 0;
- }
- else if (inter(tl, tr) == 2){
- //cout<<"contains\n";
- return t[v];
- }
- else if (inter(tl, tr) == 1){
- //cout<<"partly intersect\n";
- int tm = (tl + tr) / 2;
- return get(v*2+1,tl, tm) + get(2*v+2, tm+1, tr);
- }
- }
- void change(int v, int tl, int tr, int val){
- //cout<<"tl tr = "<<tl<<' '<<tr<<"\n";
- push(v, tl, tr);
- if (inter(tl, tr) == 0) {
- //cout<<"not intersect\n";
- return;
- }
- else if (inter(tl, tr) == 2){
- //cout<<"contains\n";
- to_push[v] = val;
- push(v, tl, tr);
- return;
- }
- else{
- //cout<<"partly intersect\n";
- int tm = (tl + tr) / 2;
- change(2*v+1, tl, tm, val);
- change(2*v+2, tm+1, tr, val);
- t[v] = t[v*2+1] + t[v*2+2];
- }
- }
- void req(){
- char x; cin>>x;
- cin>>l>>r;
- l--; r--;
- //cout<<"request = "<<x<<"\n";
- //cout<<"l r = "<<l<<" "<<r<<"\n";
- if (x == 'Q'){
- ll res = get(0,0,n-1);
- //cout<<"max("<<l<<","<<r<<") = "<<res<<"\n";
- cout<<res<<"\n";
- }else{
- int val; cin>>val;
- //cout<<"val = "<<val<<"\n";
- change(0, 0, n-1, val);
- //sh();
- }
- //sh();
- }
- };
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin>>n>>K;
- tree wrk;
- wrk.bld();
- //wrk.sh();
- //int a,b; cin>>a>>b; cout<<inter(a, b);
- for (int i=0;i<K;++i) {
- wrk.req();
- }
- }
- /*
- 10 5
- Q 1 10
- A 1 10 10
- Q 1 10
- A 1 2 5
- Q 1 10
- 10 100
- 1 4
- 2
- */
Add Comment
Please, Sign In to add comment