Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include<ext/pb_ds/assoc_container.hpp>
- #define gp __gnu_pbds
- #define iset(T) gp::tree<T,gp::null_type,less<T>,gp::rb_tree_tag,gp::tree_order_statistics_node_update>
- using namespace std;
- #define SQ(x) ((x)*(x))
- #define u unsigned
- #define ull unsigned long long
- #define ll long long
- #define ld long double
- #define test(x) cerr<<"here "<<#x<<endl;
- #define putpls(x) #x
- #define casing(x,y) cout<<"Case #"<<x<<": "<<y<<'\n';
- #define inputstl(v) for(auto& x:v) cin>>x;
- #define input(arr,n) for(ull i=0;i<n;++i){cin>>(arr)[i];}
- #define print(arr,n) for(ull i=0;i<n;++i){cout<<(arr)[i]<<" ";}cout<<'\n';
- #define printall(x) {for(auto y:x){cout<<y<<' ';}cout<<'\n';}
- #define printallpii(x) for(auto y:x){cout<<y.fi<<','<<y.se<<' ';}cout<<'\n';
- #define print_err(arr,n) for(ull i=0;i<n;++i){cerr<<(arr)[i]<<" ";}cerr<<'\n';
- #define printall_err(x) for(auto y:x){cerr<<y<<' ';}cerr<<'\n';
- #define printallpii_err(x) for(auto y:x){cerr<<y.fi<<','<<y.se<<' ';}cerr<<'\n';
- #define fi first
- #define se second
- #define pb push_back
- #define eb emplace_back
- #define mp make_pair
- #define mt make_tuple
- #define pii pair<int,int>
- #define pll pair<long long,long long>
- #define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
- #define SENSITIVITY 1e-9
- #define EQUAL(xx1,xx2) [](auto x1,auto x2){double ep=0.0001;double pos=x1-x2;if(pos<0){pos=-pos;}double maxi=(x1>x2)?(x1):(x2);if(pos/(maxi*ep)<SENSITIVITY)return true;else return false;}(xx1,xx2)
- #define pack(x) x.begin(),x.end()
- constexpr int64_t mod=1e9+7;
- struct timer
- {
- private:
- static int __timer_number;
- public:
- int timer_number;
- chrono::high_resolution_clock::time_point t1;
- timer(): t1(chrono::high_resolution_clock::now()),timer_number(__timer_number++)
- {
- cout<<"timer number "<<timer_number<<" init\n";
- }
- timer(const timer& t):t1(t.t1),timer_number(__timer_number++)
- {
- cout<<"timer number "<<timer_number<<" init\n";
- }
- template<class T=chrono::milliseconds>
- auto give_time()
- {
- chrono::high_resolution_clock::time_point t2;
- t2=chrono::high_resolution_clock::now();
- return chrono::duration_cast<T>(t2-t1);
- }
- template<class T=chrono::milliseconds>
- void print_time()
- {
- chrono::high_resolution_clock::time_point t2;
- t2=chrono::high_resolution_clock::now();
- auto d=chrono::duration_cast<T>(t2-t1);
- cout<<"time (default:milli) until now by timer "<<timer_number<<" : "<<d.count()<<'\n';
- }
- ~timer()
- {
- __timer_number--;
- }
- };
- int timer::__timer_number=0;
- // //this does not support . character matching (globbing)
- // //to do that add the code from your solution of leetcode 211
- // //add a new static element to the node, named defaulter and use that for globbing
- // template<class T>
- // class trie{
- // private:
- // vector<T> hold;
- // int hold_size;
- // int found;
- // int _size;
- // struct node{
- // map<T,node*> link;
- // int ending;
- // node():link(map<T,node*>()),ending(0)
- // {
- // }
- // ~node()
- // {
- // }
- // };
- // node root;
- // void __traverse(node* const p,int index)
- // {
- // if(index==hold_size)
- // {
- // // if(p->ending>0)
- // // found=;
- // found=p->ending;
- // return ;
- // }
- // // if(hold[index]=='.')
- // // {
- // // for(auto& x:p->link)
- // // {
- // // traverse(x.second,index+1);
- // // if(found)
- // // return ;
- // // }
- // // }
- // // else
- // // {
- // auto it=p->link.find(hold[index]);
- // if(it!=p->link.end())
- // {
- // __traverse(it->second,index+1);
- // }
- // // }
- // }
- // bool __del_traverse(node* const p,int index)
- // {
- // if(index==hold_size)
- // {
- // if(!p->ending)
- // {
- // --p->ending;
- // return 1;
- // }
- // return 0;
- // }
- // auto it=p->link.find(hold[index]);
- // if(it!=p->link.end())
- // {
- // return __del_traverse(it->second,index+1);
- // }
- // return 0;
- // }
- // void __dfs_clear(node* const p)
- // {
- // for(auto& x:p->link)
- // {
- // __dfs_clear(x.second);
- // delete x.second;
- // }
- // }
- // public:
- // trie():_size(0)
- // {
- // }
- // void add_stuff(const vector<T>& v)
- // {
- // hold.assign(v.begin(),v.end());
- // hold_size=v.size();
- // int index=0;
- // node* cur=&root;
- // while(index!=hold_size)
- // {
- // auto it=cur->link.find(hold[index]);
- // if(it==cur->link.end())
- // {
- // // test(1);
- // cur->link[hold[index]]=new node();
- // // cur=cur->link[hold[index]];
- // }
- // cur=cur->link[hold[index]];
- // ++index;
- // }
- // ++(cur->ending);
- // ++_size;
- // }
- // int find_stuff(const vector<T>& v)
- // {
- // found=0;
- // hold.assign(v.begin(),v.end());
- // hold_size=v.size();
- // __traverse(&root,0);
- // return found;
- // }
- // bool delete_stuff(const vector<T>& v)
- // {
- // hold.assign(v.begin(),v.end());
- // hold_size=v.size();
- // auto val=__del_traverse(&root,0);
- // if(val)
- // --_size;
- // return val;
- // }
- // void clear()
- // {
- // __dfs_clear(&root);
- // root.link.clear();
- // _size=0;
- // }
- // int size()
- // {
- // return _size;
- // }
- // ~trie()
- // {
- // clear();
- // }
- // };
- template<class A>
- void ____internal__print(string s,A a)
- {
- auto index=s.find(',');
- cerr<<s.substr(0,index)<<": "<<a<<'\n';
- }
- template<class A,class ... B>
- void ____internal__print(string s,A a,B... b)
- {
- auto index=s.find(',');
- cerr<<s.substr(0,index)<<": "<<a<<", ";
- ____internal__print(s.substr(index+1,(int)s.length()-index-1),b...);
- }
- #define debug(...) \
- {string __debug__s__=string(#__VA_ARGS__)+",";____internal__print(__debug__s__,__VA_ARGS__);}
- // bool isPrime(ll a)
- // {
- // if(a==1)
- // return false;
- // for(ll i=2; i*i<=a; ++i)
- // {
- // if(a%i==0)
- // {
- // return false;
- // }
- // }
- // return true;
- // }
- // bool isPali(string s)
- // {
- // int _size=(s.length())/2;
- // for(int i=0; i<_size; ++i)
- // {
- // if(s.at(i)!=s.at(s.length()-1-i))
- // {
- // return false;
- // }
- // }
- // return true;
- // }
- // int gcd(int a,int b)
- // {
- // if(a==0)
- // {
- // return b;
- // }
- // else if(b==0)
- // {
- // return a;
- // }
- // else
- // {
- // int shift=__builtin_ctz(a|b);
- // a>>=__builtin_ctz(a);
- // do
- // {
- // b>>=__builtin_ctz(b);
- // if(a>b)
- // {
- // a=a^b;
- // b=a^b;
- // a=a^b;
- // }
- // b-=a;
- // }
- // while(b);
- // return a<<shift;
- // }
- // }
- // ll gcdll(ll a,ll b)
- // {
- // if(a==0)
- // {
- // return b;
- // }
- // else if(b==0)
- // {
- // return a;
- // }
- // else
- // {
- // ll shift=__builtin_ctzll(a|b);
- // a>>=__builtin_ctzll(a);
- // do
- // {
- // b>>=__builtin_ctzll(b);
- // if(a>b)
- // {
- // a=a^b;
- // b=a^b;
- // a=a^b;
- // }
- // b-=a;
- // }
- // while(b);
- // return a<<shift;
- // }
- // }
- // ll power(ll a,ll b,ll _mod)
- // {
- // a%=_mod;
- // ll temp=a;
- // ll ans=1;
- // if(!b)
- // return 1;
- // int limit=63-__builtin_clzll(b);
- // int index=0;
- // while(index<=limit)
- // {
- // if(b&(1ll<<index))
- // {
- // ans=(ans*temp)%_mod;
- // }
- // ++index;
- // temp=(temp*temp)%_mod;
- // }
- // return ans;
- // }
- // template<class T>
- // inline void swapper(const T& d1,const T& d2)
- // {
- // for(int i=0;i<sizeof(T);++i)
- // {
- // *((char*)(&d1)+i)=(*((char*)(&d1)+i))^(*((char*)(&d2)+i));
- // }
- // for(int i=0;i<sizeof(T);++i)
- // {
- // *((char*)(&d2)+i)=(*((char*)(&d1)+i))^(*((char*)(&d2)+i));
- // }
- // for(int i=0;i<sizeof(T);++i)
- // {
- // *((char*)(&d1)+i)=(*((char*)(&d1)+i))^(*((char*)(&d2)+i));
- // }
- // }
- // int kmp(const string& s,const string& t)
- // {
- // int s_size=s.size(),t_size=t.size();
- // if(s_size<t_size)
- // return 0;
- // vector<int> arr(t_size);
- // int len=0;
- // for(int i=0;i<t_size;++i)
- // {
- // if(!i)
- // {
- // arr[i]=0;
- // continue;
- // }
- // while(1)
- // {
- // if(t[len]==t[i])
- // {
- // arr[i]=++len;
- // break;
- // }
- // if(!len)
- // {
- // arr[i]=0;
- // break;
- // }
- // len=arr[len-1];
- // }
- // }
- // //printall(arr);
- // int ans=0;
- // for(int j=0,i=0;i<s_size;++i,++j)
- // {
- // // debug(i,j);
- // if(s[i]==t[j])
- // {
- // if(j==t.length()-1)
- // {
- // //debug(i,j)
- // j=arr[j]-1;
- // //debug(j);
- // ++ans;
- // }
- // continue;
- // }
- // if(!j)
- // {
- // --j;
- // continue;
- // }
- // --i;
- // j=arr[j-1]-1;
- // }
- // return ans;
- // }
- // template<class T>
- // vector<T> LIS(vector<T> a)
- // {
- // int n=a.size();
- // vector<pair<T,int>> v;
- // vector<T*> add(n);
- // add[0]=nullptr;
- // v.push_back(make_pair(a[0],0));
- // auto f=[](pair<T,int> a,pair<T,int> b){return a.first<b.first;};
- // for(int i=1;i<n;++i)
- // {
- // auto it=lower_bound(v.begin(),v.end(),make_pair(a[i],-1),f);
- // if(it==v.end())
- // {
- // add[i]=&a[0]+v.back().second;
- // v.push_back(make_pair(a[i],i));
- // }
- // else
- // {
- // it->first=a[i];
- // it->second=i;
- // if(it==v.begin())
- // {
- // add[i]=nullptr;
- // }
- // else
- // {
- // add[i]=&a[0]+(it-1)->second;
- // }
- // }
- // }
- // vector<T> ans;
- // ans.reserve(v.size());
- // T* p=&a[0]+v.back().second;
- // while(p!=nullptr)
- // {
- // ans.push_back(*p);
- // p=(add[p-&a[0]]);
- // }
- // reverse(ans.begin(),ans.end());
- // return ans;
- // }
- // ///O(N*N) NOT MANCHAR . BULLSHIT DP
- // string longest_palindromic_substring(const string& s)
- // {
- // if(s.size()<=1)
- // return s;
- // int len=s.size();
- // vector<bool> odd(s.size(),1);
- // vector<bool> even(s.size(),0);
- // string ans=string(1,s[0]);
- // for(int i=0;i<len-1;++i)
- // {
- // if(s[i]==s[i+1])
- // {
- // even[i]=1;
- // if(ans.size()<2)
- // ans=s.substr(i,2);
- // }
- // }
- // for(int i=3;i<=len;++i)
- // {
- // if(i&1)
- // {
- // for(int j=0;j<=len-i;++j)
- // {
- // odd[j]=(s[j]==s[j+i-1])&&(odd[j+1]);
- // if(odd[j]&&ans.size()<i)
- // ans=s.substr(j,i);
- // }
- // }
- // else
- // {
- // for(int j=0;j<=len-i;++j)
- // {
- // even[j]=(s[j]==s[j+i-1])&&(even[j+1]);
- // if(even[j]&&ans.size()<i)
- // ans=s.substr(j,i);
- // }
- // }
- // }
- // return ans;
- // }
- // template<class S>
- // S longestSubarraySum(auto start,auto end)
- // {
- // S val=0;
- // S maxi=*start;
- // for(auto s=start;s!=end;++s)
- // {
- // if(val+*s>=*s)
- // {
- // val+=*s;
- // }
- // else
- // {
- // val=*s;
- // }
- // if(maxi<val)
- // {
- // maxi=val;
- // }
- // }
- // return maxi;
- // }
- // template<class T>
- // T fibo(T x,T _mod)
- // {
- // if(x==1)
- // {
- // return (T)0;
- // }
- // else if(x==2)
- // {
- // return (T)1;
- // }
- // // x*=2;// made it twice to get the actual value
- // stack<pair<T,T>> st;
- // while(x>1)
- // {
- // st.push(make_pair(x,x+1));
- // x/=2;
- // }
- // //start with an even means the same components
- // //otherwise component change
- // T a=0;
- // T b=1;
- // T c;
- // while(st.size())
- // {
- // auto p=st.top();
- // st.pop();
- // T cur_a;
- // T cur_b;
- // if(p.first&1)
- // {
- // cur_a=(b*b+2*a*b)%_mod;
- // b=(a+b)%_mod;
- // a=(b-a+_mod)%_mod;
- // cur_b=(a*a+b*b)%_mod;
- // }
- // else
- // {
- // cur_a=(a*a+b*b)%_mod;
- // cur_b=(b*b+2*a*b)%_mod;
- // }
- // a=cur_a;
- // b=cur_b;
- // }
- // return a;
- // }
- // // #pragma GCC optimize("Ofast")
- // // #pragma GCC optimize("unroll-loops")
- // int main()
- // {
- // // #ifdef ONLINE_JUDGE
- // // #include <sys/resource.h>
- // // rlimit R;
- // // getrlimit(RLIMIT_STACK, &R);
- // // R.rlim_cur = R.rlim_max;
- // // setrlimit(RLIMIT_STACK, &R);
- // // #endif
- // //define cin fin
- // //ifstream fin;
- // //fin.open("input.txt",ios_base::in);
- // // fastio;
- // // freopen("input.txt","r",stdin);
- // //freopen("output.txt","w",stdout);
- // int tt=1;
- // // cin>>tt;
- // // scanf("%d",&tt);
- // // timer t;
- // int ittt=1;
- // //////CHECK FOR EDGE CASES DUMMY!!!!!!!!!!!/////
- // while(tt--)
- // {
- // vector<vector<int>> v;
- // int s,t;
- // int n,m;
- // int visited[(int)1e5+10][3]={};
- // int ans=-1;
- // cin>>n>>m;
- // v.assign(n+1,vector<int>());
- // for(int i=0;i<m;++i)
- // {
- // int x,y;
- // cin>>x>>y;
- // v[x].push_back(y);
- // }
- // cin>>s>>t;
- // {
- // visited[s][0]=1;
- // queue<pair<int,int>> q;
- // q.push(make_pair(s,0));
- // while(q.size())
- // {
- // auto p=q.front();
- // q.pop();
- // if(p.first==t && p.second%3==0)
- // {
- // ans=p.second;
- // break;
- // }
- // for(auto& t:v[p.first])
- // {
- // if(visited[t][(p.second+1)%3])
- // continue;
- // visited[t][(p.second+1)%3]=1;
- // q.push(make_pair(t,p.second+1));
- // }
- // }
- // }
- // if(ans==-1)
- // cout<<ans<<"\n";
- // else
- // cout<<ans/3<<"\n";
- // }
- // // t.print_time();
- // return 0;
- // }
- // Program to show segment tree to demonstrate laz
- #define MAX 400100
- // Ideally, we should not use global variables and large
- // constant-sized arrays, we have done it here for simplicity.
- int tree[MAX] = {0}; // To store segment tree
- int lazy[MAX] = {0}; // To store pending updates
- /* si -> index of current node in segment tree
- ss and se -> Starting and ending indexes of elements for
- which current nodes stores sum.
- us and ue -> starting and ending indexes of update query
- diff -> which we need to add in the range us to ue */
- void updateRangeUtil(int si, int ss, int se, int us,
- int ue, int diff)
- {
- // If lazy value is non-zero for current node of segment
- // tree, then there are some pending updates. So we need
- // to make sure that the pending updates are done before
- // making new updates. Because this value may be used by
- // parent after recursive calls (See last line of this
- // function)
- if (lazy[si] != 0)
- {
- // Make pending updates using value stored in lazy
- // nodes
- tree[si] += (se-ss+1)*lazy[si];
- // checking if it is not leaf node because if
- // it is leaf node then we cannot go further
- if (ss != se)
- {
- // We can postpone updating children we don't
- // need their new values now.
- // Since we are not yet updating children of si,
- // we need to set lazy flags for the children
- lazy[si*2 + 1] += lazy[si];
- lazy[si*2 + 2] += lazy[si];
- }
- // Set the lazy value for current node as 0 as it
- // has been updated
- lazy[si] = 0;
- }
- // out of range
- if (ss>se || ss>ue || se<us)
- return ;
- // Current segment is fully in range
- if (ss>=us && se<=ue)
- {
- // Add the difference to current node
- tree[si] += (se-ss+1)*diff;
- // same logic for checking leaf node or not
- if (ss != se)
- {
- // This is where we store values in lazy nodes,
- // rather than updating the segment tree itelf
- // Since we don't need these updated values now
- // we postpone updates by storing values in lazy[]
- lazy[si*2 + 1] += diff;
- lazy[si*2 + 2] += diff;
- }
- return;
- }
- // If not completely in rang, but overlaps, recur for
- // children,
- int mid = (ss+se)/2;
- updateRangeUtil(si*2+1, ss, mid, us, ue, diff);
- updateRangeUtil(si*2+2, mid+1, se, us, ue, diff);
- // And use the result of children calls to update this
- // node
- tree[si] = tree[si*2+1] ^ tree[si*2+2];
- }
- // Function to update a range of values in segment
- // tree
- /* us and eu -> starting and ending indexes of update query
- ue -> ending index of update query
- diff -> which we need to add in the range us to ue */
- void updateRange(int n, int us, int ue, int diff)
- {
- updateRangeUtil(0, 0, n-1, us, ue, diff);
- }
- /* A recursive function to get the sum of values in given
- range of the array. The following are parameters for
- this function.
- si --> Index of current node in the segment tree.
- Initially 0 is passed as root is always at'
- index 0
- ss & se --> Starting and ending indexes of the
- segment represented by current node,
- i.e., tree[si]
- qs & qe --> Starting and ending indexes of query
- range */
- int getSumUtil(int ss, int se, int qs, int qe, int si)
- {
- // If lazy flag is set for current node of segment tree,
- // then there are some pending updates. So we need to
- // make sure that the pending updates are done before
- // processing the sub sum query
- if (lazy[si] != 0)
- {
- // Make pending updates to this node. Note that this
- // node represents sum of elements in arr[ss..se] and
- // all these elements must be increased by lazy[si]
- tree[si] += (se-ss+1)*lazy[si];
- // checking if it is not leaf node because if
- // it is leaf node then we cannot go further
- if (ss != se)
- {
- // Since we are not yet updating children os si,
- // we need to set lazy values for the children
- lazy[si*2+1] += lazy[si];
- lazy[si*2+2] += lazy[si];
- }
- // unset the lazy value for current node as it has
- // been updated
- lazy[si] = 0;
- }
- // Out of range
- if (ss>se || ss>qe || se<qs)
- return 0;
- // At this point we are sure that pending lazy updates
- // are done for current node. So we can return value
- // (same as it was for query in our previous post)
- // If this segment lies in range
- if (ss>=qs && se<=qe)
- return tree[si];
- // If a part of this segment overlaps with the given
- // range
- int mid = (ss + se)/2;
- return getSumUtil(ss, mid, qs, qe, 2*si+1) ^
- getSumUtil(mid+1, se, qs, qe, 2*si+2);
- }
- // Return sum of elements in range from index qs (query
- // start) to qe (query end). It mainly uses getSumUtil()
- int getSum(int n, int qs, int qe)
- {
- // Check for erroneous input values
- if (qs < 0 || qe > n-1 || qs > qe)
- {
- // printf("Invalid Input");
- return -1;
- }
- return getSumUtil(0, n-1, qs, qe, 0);
- }
- // A recursive function that constructs Segment Tree for
- // array[ss..se]. si is index of current node in segment
- // tree st.
- void constructSTUtil(int arr[], int ss, int se, int si)
- {
- // out of range as ss can never be greater than se
- if (ss > se)
- return ;
- // If there is one element in array, store it in
- // current node of segment tree and return
- if (ss == se)
- {
- tree[si] = arr[ss];
- return;
- }
- // If there are more than one elements, then recur
- // for left and right subtrees and store the sum
- // of values in this node
- int mid = (ss + se)/2;
- constructSTUtil(arr, ss, mid, si*2+1);
- constructSTUtil(arr, mid+1, se, si*2+2);
- tree[si] = tree[si*2 + 1] ^ tree[si*2 + 2];
- }
- /* Function to construct segment tree from given array.
- This function allocates memory for segment tree and
- calls constructSTUtil() to fill the allocated memory */
- void constructST(int arr[], int n)
- {
- // Fill the allocated memory st
- constructSTUtil(arr, 0, n-1, 0);
- }
- int findXOR(int n)
- {
- int mod = n % 4;
- // If n is a multiple of 4
- if (mod == 0)
- return n;
- // If n % 4 gives remainder 1
- else if (mod == 1)
- return 1;
- // If n % 4 gives remainder 2
- else if (mod == 2)
- return n + 1;
- // If n % 4 gives remainder 3
- else if (mod == 3)
- return 0;
- return 1;
- }
- // Function to return the XOR of elements
- // from the range [l, r]
- int findXOR(int l, int r)
- {
- return (findXOR(l - 1) ^ findXOR(r));
- }
- // Driver program to test above functions
- int main()
- {
- int n,q;
- cin>>n>>q;
- int* arr=new int[n+10];
- for(int i=0;i<n+10;++i)
- {
- arr[i]=1;
- }
- // int* arr[] = {1, 3, 5, 7, 9, 11};
- // Build segment tree from given array
- constructST(arr, n);
- for(int i=0;i<q;++i)
- {
- for(int i=0;i<n;++i)
- {
- cout<<getSum(n,i,i)<<" ";
- }
- cout<<"\n";
- // debug(i);
- int a1,a2,a3,a4;
- cin>>a1>>a2>>a3>>a4;
- int val1=findXOR(a1,a2);//correct xor//0 based answer
- int val2=findXOR(a3,a4);//correct xor
- if(val1>val2)
- swap(val1,val2);
- int xo=a1^a2^a3^a4;
- // debug(xo);
- updateRange(n,val1,val2,xo);
- }
- // // Print sum of values in array from index 1 to 3
- // // printf("Sum of values in given range = %d\n",
- // getSum(n, 1, 3));
- // // Add 10 to all nodes at indexes from 1 to 5.
- // updateRange(n, 1, 5, 10);
- // // Find sum after the value is updated
- // // printf("Updated sum of values in given range = %d\n",
- // getSum( n, 1, 3));
- for(int i=0;i<n;++i)
- {
- cout<<getSum(n,i,i)<<" ";
- }
- cout<<"\n";
- return 0;
- }
Add Comment
Please, Sign In to add comment