Skip to content

Commit 491d51a

Browse files
committed
update
1 parent 01d55ac commit 491d51a

File tree

4 files changed

+229
-7
lines changed

4 files changed

+229
-7
lines changed

Important.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
11->lower_bound return the iterator of element if present and if not then its greater one!
1212
12-> upper_bound return the iterator of the elemen with is greatere then it;
1313
13->int set/map we use upper and lower bound diff as set.upper_bound(3); in vector upper_bound(v.begin(),v.end(),target);because working in vector is BS and in set its tree
14+
1415
14->int sum=accumulate(v.begin(),v.end(),0(initial sum value)); to get sum of vector;
16+
1517
15-> transform(s.begin(), s.end(), s.begin(), ::tolower); to make string lower ;
1618
sweepline adhoc
19+
1720
16->s.erase(unique(s.begin(), s.end()), s.end()); to remove all duplicates of string or vector;
18-
17->print
19-
vector<pair<int, int>> e(n);
20-
for (auto [x, y]: e) {
21-
cerr << x << ' ' << y << '\n';
22-
}
23-
cerr << '\n';
24-
18->xor of l to r range is even number of ele then twice xor is ==0;
21+
22+
18->xor of l to r range is even number of ele then twice xor is ==0;
23+
24+
19->if (all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )cout <<"sare odd h"<<endl;
25+
else cout<<"sare odd nhi h "
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class sgt{
2+
public:
3+
vector<int>seg;
4+
sgt(int n)
5+
{
6+
seg.resize(4*n+1);
7+
}
8+
void build(int i,int low,int high,vector<int>&v)
9+
{
10+
if(low==high){seg[i]=v[low];return;}
11+
int mid=(low+high)>>1;
12+
build(2*i+1,low,mid,v);
13+
build(2*i+2,mid+1,high,v);
14+
seg[i]=min(seg[2*i+1],seg[2*i+2]);
15+
}
16+
int query(int i,int low ,int high ,int l,int r)
17+
{
18+
if(l>high||r<low)return INT_MAX;//out of range
19+
if(l<=low&&r>=high)return seg[i];//given range(ans)
20+
int mid=(low+high)>>1;
21+
int left=query(2*i+1,low,mid,l,r);
22+
int right=query(2*i+2,mid+1,high,l,r);
23+
return min(left,right);
24+
}
25+
void update(int i,int low,int high,int ind,int val)
26+
{
27+
if(low==high){seg[i]=val;return;}
28+
int mid=(low+high)>>1;
29+
if(ind<=mid)update(2*i+1,low,mid,ind,val);
30+
else update(2*i+2,mid+1,high,ind,val);
31+
seg[i]=min(seg[2*i+1],seg[2*i+2]);
32+
}
33+
};
34+
/*
35+
int main()
36+
{
37+
ios_base::sync_with_stdio(false);
38+
cin.tie(NULL);
39+
#ifndef DISABLE_STACK_SIZE_CHANGE
40+
rlimit rlim;
41+
if (getrlimit(RLIMIT_STACK, &rlim) != 0) { return 1; }
42+
rlim.rlim_cur = 1024 * 1024 * 1024;
43+
if (setrlimit(RLIMIT_STACK, &rlim) != 0) { return 2; }
44+
#endif
45+
46+
int n;cin>>n;
47+
vector<int>v(n),u(n);
48+
loop(i,0,n)cin>>v[i];
49+
50+
sgt s1(n);
51+
s1.build(0,0,n-1,v);
52+
int q;cin>>q;
53+
while(q--)
54+
{
55+
int type;cin>>type;
56+
if(type==1)
57+
{
58+
int a,b;cin>>a>>b;
59+
cout<<s1.query(0,0,n-1,a,b)<<nl;
60+
61+
}
62+
else
63+
{
64+
int i,val;cin>>i>>val;
65+
s1.update(0,0,n-1,i,val);
66+
67+
}
68+
}
69+
}
70+
*/
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
class sgt{
2+
public:
3+
vector<int>seg,lazy;
4+
sgt(int n)
5+
{
6+
seg.resize(4*n+1);
7+
lazy.resize(4*n+1);
8+
}
9+
void build(int i,int low,int high,vector<int>&v)
10+
{
11+
if(low==high){seg[i]=v[low];return;}
12+
int mid=(low+high)>>1;
13+
build(2*i+1,low,mid,v);
14+
build(2*i+2,mid+1,high,v);
15+
seg[i]=seg[2*i+1]+seg[2*i+2];
16+
}
17+
int query(int i,int low,int high ,int l,int r)
18+
{
19+
if(lazy[i]!=0)
20+
{
21+
seg[i]+=(high-low+1)*lazy[i];
22+
if(low!=high)
23+
{
24+
lazy[2*i+1]+=lazy[i];
25+
lazy[2*i+2]+=lazy[i];
26+
}
27+
lazy[i]=0;
28+
}
29+
if(high<=r&&l<=low)return seg[i];
30+
if(high<l||low>r)return 0;
31+
int mid=(low+high)>>1;
32+
int left=query(2*i+1,low,mid,l,r);
33+
int right=query(2*i+2,mid+1,high,l,r);
34+
return left+right;
35+
}
36+
void update(int i,int low ,int high,int l,int r,int val)
37+
{
38+
if(lazy[i]!=0)
39+
{
40+
seg[i]+=(high-low+1)*lazy[i];
41+
if(low!=high)
42+
{
43+
lazy[2*i+1]+=lazy[i];
44+
lazy[2*i+2]+=lazy[i];
45+
}
46+
lazy[i]=0;
47+
}
48+
if(l>high||r<low)return ;
49+
if(low>=l&&high<=r)
50+
{
51+
seg[i]+=(high-low+1)*val;
52+
if(high!=low)
53+
{
54+
lazy[2*i+1]+=val;
55+
lazy[2*i+2]+=val;
56+
}
57+
return ;
58+
}
59+
int mid=(low+high)>>1;
60+
update(2*i+1,low,mid,l,r,val);
61+
update(2*i+2,mid+1,high,l,r,val);
62+
seg[i]=seg[2*i+1]+seg[2*i+2];
63+
}
64+
};
65+
// int main()
66+
// {
67+
68+
// int n;cin>>n;
69+
// vector<int>v(n);
70+
// loop(i,0,n)cin>>v[i];
71+
// sgt s1(n);
72+
73+
// s1.build(0,0,n-1,v);
74+
// int q;cin>>q;
75+
// while(q--)
76+
// {
77+
// int type;cin>>type;
78+
// if(type==1)
79+
// {
80+
// int l,r;cin>>l>>r;cout<<s1.query(0,0,n-1,l,r)<<nl;
81+
// }
82+
// else
83+
// {
84+
// int l,r,val;cin>>l>>r>>val;s1.update(0,0,n-1,l,r,val);
85+
// }
86+
// }
87+
// }
88+
89+
90+
//to update the given range with the val;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class sgt{
2+
public:
3+
vector<int>seg;
4+
sgt(int n)
5+
{
6+
seg.resize(4*n+1);
7+
}
8+
void build(int i,int low,int high,vector<int>&v)
9+
{
10+
if(low==high){seg[i]=v[low];return;}
11+
int mid=(low+high)>>1;
12+
build(2*i+1,low,mid,v);
13+
build(2*i+2,mid+1,high,v);
14+
seg[i]=seg[2*i+1]+seg[2*i+2];
15+
}
16+
int query(int i,int low ,int high ,int l,int r)
17+
{
18+
if(l>high||r<low)return 0;
19+
if(l<=low&&r>=high)return seg[i];
20+
int mid=(low+high)>>1;
21+
int left=query(2*i+1,low,mid,l,r);
22+
int right=query(2*i+2,mid+1,high,l,r);
23+
return left+right;
24+
}
25+
void update(int i,int low,int high,int ind,int val)
26+
{
27+
if(low==high){seg[i]=val;return;}
28+
int mid=(low+high)>>1;
29+
if(ind<=mid)update(2*i+1,low,mid,ind,val);
30+
else update(2*i+2,mid+1,high,ind,val);
31+
seg[i]=seg[2*i+1]+seg[2*i+2];
32+
}
33+
};
34+
/*
35+
int main()
36+
{
37+
int n;cin>>n;
38+
vector<int>v(n),u(n);
39+
loop(i,0,n)cin>>v[i];
40+
41+
sgt s1(n);
42+
s1.build(0,0,n-1,v);
43+
int q;cin>>q;
44+
while(q--)
45+
{
46+
int type;cin>>type;
47+
if(type==1)
48+
{
49+
int a,b;cin>>a>>b;
50+
cout<<s1.query(0,0,n-1,a,b)<<nl;
51+
52+
}
53+
else
54+
{
55+
int i,val;cin>>i>>val;
56+
s1.update(0,0,n-1,i,val);
57+
58+
}
59+
}
60+
}
61+
*/

0 commit comments

Comments
 (0)