Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************************************
- * ####### # *
- * # # # ###### # # # # #### # # # # # *
- * # # # # # # ## # # # # # # # *
- * # ###### ##### # # # # # #### ###### # # # *
- * # # # # ####### # # # # # # # # # *
- * # # # # # # # ## # # # # # # # *
- * # # # ###### # # # # #### # # #### ###### *
- ***************************************************************************/
- #include<bits/stdc++.h>
- #define ll long long
- #define pb push_back
- #define endl '\n'
- #define pii pair<ll int,ll int>
- #define vi vector<ll int>
- #define all(a) (a).begin(),(a).end()
- #define F first
- #define S second
- #define sz(x) (ll int)x.size()
- #define hell 1000000007
- #define rep(i,a,b) for(ll int i=a;i<b;i++)
- #define lbnd lower_bound
- #define ubnd upper_bound
- #define bs binary_search
- #define mp make_pair
- using namespace std;
- #define N 100005
- ll tree[N];
- ll MaxVal;
- ll BitMask;
- ll read(ll idx)
- {
- ll sum=0;
- while(idx>0)
- {
- sum+=tree[idx];
- idx-=(idx & -idx);
- }
- return sum;
- }
- void update(ll idx,ll val)
- {
- while(idx<=MaxVal)
- {
- tree[idx]+=val;
- idx+=(idx & -idx);
- }
- }
- ll readSingle(ll idx)
- {
- ll sum=tree[idx];
- if(idx>0)
- {
- ll z=idx-(idx & -idx);
- idx--;
- while(idx!=z)
- {
- sum-=tree[idx];
- idx-=(idx & -idx);
- }
- }
- return sum;
- }
- void scale(ll c)
- {
- for(ll i=1;i<=MaxVal;i++)
- tree[i]=tree[i]*c;
- }
- ll find(ll cumFre)
- {
- ll idx=0;
- ll fBitMask=BitMask;
- while(fBitMask!=0 && idx<MaxVal)
- {
- ll tIdx=idx+fBitMask;
- if(cumFre==tree[tIdx])
- return tIdx;
- else if(cumFre>tree[tIdx])
- {
- idx=tIdx;
- cumFre-=tree[tIdx];
- }
- fBitMask>>=1;
- }
- if(cumFre!=0)
- return -1;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int TESTS=1;
- // cin>>TESTS;
- while(TESTS--)
- {
- ll a[]={2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9};
- MaxVal=12;
- BitMask=8;
- rep(i,1,MaxVal+1)
- {
- for(ll j=i;j>(i & -i);j--)
- tree[i]+=a[j];
- }
- cout<<read(6)<<endl;
- // update(4,6);
- scale(3);
- cout<<readSingle(6)<<endl;
- cout<<read(6);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment