Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define rep(i,a,n) for(ll i=a;i<n;i++)
- #define rev(i,n,a) for(ll i=n-1;i>=a;i--)
- #define f first
- #define s second
- #define vll vector<ll>
- #define vvll vector<vector<ll>>
- #define vpii vector<pair<int,int>>
- #define vpll vector<pll>
- #define pll pair<ll,ll>
- #define secreterror() ios_base::sync_with_stdio(false);cin.tie(NULL);
- #define N 1000001
- ll BIT[N][32];
- void update(ll pos,ll id,ll val){
- for(ll i=pos;i<N;i+=i&(-i)){
- BIT[i][id]+=val;
- }
- }
- ll query(ll id,ll idx){
- ll ans=0;
- for(ll j=id;j>=1;j-=j&(-j)){
- ans+=BIT[j][idx];
- }
- return ans;
- }
- void solve(){
- ll n,q;
- cin>>n>>q;
- vector<ll>arr(n);
- rep(i,0,n){
- cin>>arr[i];
- // count set bit in x;
- ll cnt=0;
- for(ll j=0;j<32;j++){
- if(arr[i]&(1<<j))
- {
- update(i+1,j,1);
- }
- }
- }
- while (q--)
- {
- ll type;
- cin>>type;
- if(type==1){
- ll x,i;
- cin>>x>>i;
- ll del=0;
- for(ll j=0;j<30;j++){
- if(arr[i-1]&(1<<j)){
- update(i,j,-1);
- }
- }
- for(ll j=0;j<30;j++){
- if(x&(1<<j)){
- update(i,j,1);
- }
- }
- }
- else
- {
- string s;
- ll l,r;
- cin>>s>>l>>r;
- ll temp[32]={0};
- // cout<<"heAre"<<endl;
- for(ll i=0;i<28;i++){
- temp[i]=query(r,i);
- }
- for(ll i=0;i<28;i++){
- temp[i]=temp[i]-query(l-1,i);
- }
- // cout<<"heAre"<<endl;
- ll ans=0;
- if(s=="OR"){
- // cout<<"here"<<endl;
- for(ll i=0;i<28;i++){
- ll one=temp[i];
- ll zero=(r-l+1)-one;
- ll tot=one+zero;
- ll pair=tot*(tot-1);
- pair=pair/2;
- pair=pair-((zero)*(zero-1))/2;
- ans+=(1<<i)*pair;
- }
- }
- else if(s=="XOR"){
- for(ll i=0;i<28;i++){
- ll one=temp[i];
- ll zero=(r-l+1)-one;
- ll pair=one*zero;
- ans+=(1<<i)*pair;
- }
- }
- else
- {
- for(ll i=0;i<28;i++){
- ll one=temp[i];
- ll zero=(r-l+1)-one;
- ll pair=(one)*(one-1);
- pair=pair/2;
- ans+=(1<<i)*pair;
- }
- }
- if(l!=r)
- cout<<ans<<endl;
- else
- {
- cout<<"0"<<endl;
- }
- }
- }
- }
- int main()
- {
- secreterror()
- // ll t;
- // cin>>t;
- //while(t--){
- solve();
- //}
- }
Add Comment
Please, Sign In to add comment