Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int mas[1000001],n;
- struct Node {
- Node *l,*r;
- int left,right;
- int sum;
- Node (int l2,int r2){
- left=l2;
- right=r2;
- sum=0;
- l=r=NULL;
- }
- };
- int cnt=0;
- int f (Node *v,int a,int b){
- // a left right b
- if (!v->l){
- v->l=new Node (v->left,((v->left+v->right)>>1));
- v->r=new Node (((v->left+v->right)>>1)+1,v->right);
- }
- int left=v->left;
- int right=v->right;
- if (a<=left&&right<=b){
- cnt++;
- return v->sum;
- }
- //a b left right left right a b
- if (left>b||a>right){
- return 0;
- }
- return f(v->l,a,b)+f(v->r,a,b);
- }
- void update (Node *v,int a,int x){
- if (a==v->left&&a==v->right){
- v->sum=x;
- return ;
- }
- if (!v->l){
- v->l=new Node (v->left,((v->left+v->right)>>1));
- v->r=new Node (((v->left+v->right)>>1)+1,v->right);
- }
- if (v->l->left<=a&&a<=v->l->right){
- update(v->l,a,x);
- }
- else{
- update(v->r,a,x);
- }
- v->sum=v->l->sum+v->r->sum;
- }
- int main () {
- Node *root=new Node (1,1000000000);
- /*
- root->left=1;
- root->right=1000000000;
- root->sum=0;
- es shecdoma iyoo ^^^^^^^^^^^^^^^^
- */
- update (root,100000000,3);
- update (root,5,1);
- cout<<f(root,6,100000300)<<endl;
- cout<<cnt<<endl;
- return 0;
- }
- /*
- _ _ _ _
- | \ | | (_) | |
- | \| | _ ___ | | __ ___ ___ _ __ __ _
- | . ` | | | / __| | |/ / / __| / _ \ | '_ \ / _` |
- | |\ | | | | (__ | < \__ \ | (_) | | | | | | (_| |
- |_| \_| |_| \___| |_|\_\ |___/ \___/ |_| |_| \__,_|
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement