diff --git a/codechef/apr18-div1/chefat.cpp b/codechef/apr18-div1/chefat.cpp new file mode 100644 index 0000000..2f13fdd --- /dev/null +++ b/codechef/apr18-div1/chefat.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vll; +typedef pair ii; +typedef vector vii; +typedef set si; +typedef map msi; + +#define rep(i, a, b) \ +for (int i = int(a); i < int(b); i++) // a to b, and variable i is local! +#define TRvi(c, it) \ +for (vi::iterator it = (c).begin(); it != (c).end(); it++) +#define TRvii(c, it) \ +for (vii::iterator it = (c).begin(); it != (c).end(); it++) +#define TRmsi(c, it) \ +for (msi::iterator it = (c).begin(); it != (c).end(); it++) +#define INF 2000000000 // 2 billion +#define mod 1000000007 + +int main() { + ll t,n,q,i,j,l,r,type; + cin>>n>>q; + + double P[q],ans,T; + rep(i,0,n) + { + cin>>P[i]; + } + cout<>type; + if(type == 0) + { + cin>>l>>r; + rep(i,l-1,r) + ans *= (1-P[i]); + cout<>l>>r>>T; + rep(i,l-1,r) + P[i] *= T; + } + } + + + return 0; +} diff --git a/codechef/apr18-div1/chefpar.cpp b/codechef/apr18-div1/chefpar.cpp new file mode 100644 index 0000000..835e3f2 --- /dev/null +++ b/codechef/apr18-div1/chefpar.cpp @@ -0,0 +1,119 @@ +#include +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vll; +typedef pair ii; +typedef vector vii; +typedef set si; +typedef map msi; + +#define rep(i, a, b) \ +for (int i = int(a); i < int(b); i++) // a to b, and variable i is local! +#define TRvi(c, it) \ +for (vi::iterator it = (c).begin(); it != (c).end(); it++) +#define TRvii(c, it) \ +for (vii::iterator it = (c).begin(); it != (c).end(); it++) +#define TRmsi(c, it) \ +for (msi::iterator it = (c).begin(); it != (c).end(); it++) +#define INF 2000000000 // 2 billion +#define mod 1000000007 +//logarithmic expo +ll power(ll x, ll y) +{ + ll val=1; + x = x%mod; + while(y>0) + { + if(y&1) + { + val = (val*x)%mod; + } + y = y>>1; + x = (x*x)%mod; + } + return val; +} +ll spf[1000001]; +bool prime[1000001]; + +void genprime() +{ + int i,j; + rep(i,0,1000001) + prime[i] = true; + prime[0] = prime[1] ; + for(i = 2;i<1000001;i++) + { + if(prime[i]) + { + for(j = 2*i;j<1000001;j+=i) + { + prime[j] = false; + } + } + } +} + +void genspf() +{ + spf[1] = 1;int i,j; + for ( i=2; i<1000001; i++) + spf[i] = i; + + for ( i=4; i<1000001; i+=2) + spf[i] = 2; + + for ( i=3; i*i<1000001; i++) + { + if (spf[i] == i) + { + for ( j=i*i; j<1000001; j+=i) + { + if (spf[j]==j) + spf[j] = i; + } + } + } + /*for(i=1;i<100;i++) + cout< factorise(ll n) +{ + vector ret,ret1; + while(n!=1) + { + ret.push_back(spf[n]); + n/=spf[n]; + } + ret1.push_back(ret[0]); + rep(i,1,ret.size()) + { + if(ret[i] != ret[i-1]) + ret1.push_back(ret[i]); + } + return ret1; +} + +int main() { + ll t,n,i,j,k,m; + cin>>n>>m>>k; + ll A[n], P[m]; + rep(i,0,n) + { + cin>>A[i]; + } + rep(i,0,m) + { + cin>>P[i]; + } + rep(i,0,n) + { + cout< +#include + +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vll; +typedef pair ii; +typedef vector vii; +typedef set si; +typedef map msi; + +#define rep(i, a, b) \ +for (int i = int(a); i < int(b); i++) // a to b, and variable i is local! +#define TRvi(c, it) \ +for (vi::iterator it = (c).begin(); it != (c).end(); it++) +#define TRvii(c, it) \ +for (vii::iterator it = (c).begin(); it != (c).end(); it++) +#define TRmsi(c, it) \ +for (msi::iterator it = (c).begin(); it != (c).end(); it++) +#define INF 2000000000 // 2 billion +#define mod 1000000007 +//seg tree rmax, rmin +struct node +{ + ll minimum; + ll maximum; +}; + +ll getMid(ll s, ll e) { return s + (e -s)/2; } + +struct node MaxMinUntill(struct node *st, ll ss, ll se, ll qs, + ll qe, ll index) +{ + // If segment of this node is a part of given range, then return + // the minimum and maximum node of the segment + struct node tmp,left,right; + if (qs <= ss && qe >= se) + return st[index]; + + // If segment of this node is outside the given range + if (se < qs || ss > qe) + { + tmp.minimum = INT_MAX; + tmp.maximum = INT_MIN; + return tmp; + } + + // If a part of this segment overlaps with the given range + ll mid = getMid(ss, se); + left = MaxMinUntill(st, ss, mid, qs, qe, 2*index+1); + right = MaxMinUntill(st, mid+1, se, qs, qe, 2*index+2); + tmp.minimum = min(left.minimum, right.minimum); + tmp.maximum = max(left.maximum, right.maximum); + return tmp; +} + +struct node MaxMin(struct node *st, ll n, ll qs, ll qe) +{ + struct node tmp; + if (qs < 0 || qe > n-1 || qs > qe) { + tmp.minimum = INT_MIN; + tmp.minimum = INT_MAX; + return tmp; + } + return MaxMinUntill(st, 0, n-1, qs, qe, 0); +} + +void constructSTUtil(ll arr[], ll ss, ll se, struct node *st, + ll si) +{ + // If there is one element in array, store it in current node of + // segment tree and return + if (ss == se) + { + st[si].minimum = arr[ss]; + st[si].maximum = arr[ss]; + return ; + } + + // If there are more than one elements, then recur for left and + // right subtrees and store the minimum and maximum of two values + // in this node + ll mid = getMid(ss, se); + constructSTUtil(arr, ss, mid, st, si*2+1); + constructSTUtil(arr, mid+1, se, st, si*2+2); + + st[si].minimum = min(st[si*2+1].minimum, st[si*2+2].minimum); + st[si].maximum = max(st[si*2+1].maximum, st[si*2+2].maximum); +} +struct node *constructST(ll arr[], ll n) +{ + // Allocate memory for segment tree + // Height of segment tree + ll x = (ll)(ceil(log2(n))); + + // Maximum size of segment tree + ll max_size = 2*(ll)pow(2, x) - 1; + + struct node *st = new struct node[max_size]; + + // Fill the allocated memory st + constructSTUtil(arr, 0, n-1, st, 0); + + // Return the constructed segment tree + return st; +} + +int main() +{ + ll t,n,j,i,ctr,flag,mx,mi; + cin>>t; + + while(t--) + { + ctr=flag =0; + cin>>n; + ll A[n], B[n]; + bool alive[n]; + unordered_map > mymap; + + rep(i,0,n) + { + cin>>A[i]; + } + rep(i,0,n) + { + cin>>B[i]; + if(B[i] > A[i]) + flag = 1; + if(B[i]!=A[i]) + mymap[B[i]].push(i); + } + if(flag == 1) + { + cout<<"-1\n"; + continue; + } + i=0; + while(iB[i]) + { + flag = 1; + break; + } + i++; + } + if(flag == 0) + { + cout<<"0\n"; + continue; + } + // kaam ka + + struct node *stA = constructST(A, n),*stB = constructST(B, n);; + + int qs = 2; // Starting index of query range + int qe = 6; // Ending index of query range + struct node result=MaxMin(stA, n, qs, qe); + + //cout< "; + + if(mymap[B[i]].size() == 1) + { + ctr++; + continue; + } + //cout< "; + while(!mymap[B[i]].empty()) + { + qs = mymap[B[i]].front(); + mymap[B[i]].pop(); + if(mymap[B[i]].empty()) + break; + qe = mymap[B[i]].front(); + + //cout<= B[i]) + { + alive[qe] = false; + //cout<<"false\n"; + } + else + { + //cout<<"break\n"; + break; + + } + } + /* + rep(j,i+1,n) + { + //cout<B[i] || A[j] +#include + +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vll; +typedef pair ii; +typedef vector vii; +typedef set si; +typedef map msi; + +#define rep(i, a, b) \ +for (int i = int(a); i < int(b); i++) // a to b, and variable i is local! +#define TRvi(c, it) \ +for (vi::iterator it = (c).begin(); it != (c).end(); it++) +#define TRvii(c, it) \ +for (vii::iterator it = (c).begin(); it != (c).end(); it++) +#define TRmsi(c, it) \ +for (msi::iterator it = (c).begin(); it != (c).end(); it++) +#define INF 2000000000 // 2 billion +#define mod 1000000007 + +ll power(ll x, ll y) +{ + ll val=1; + x = x%mod; + while(y>0) + { + if(y&1) + { + val = (val*x)%mod; + } + y = y>>1; + x = (x*x)%mod; + } + return val; +} + +int main() +{ + /*clock_t start,end; + start = clock();*/ + ll t,n,i,j,a,b,ans,term,d,len; + string s; + cin>>t; + while(t--) + { + cin>>s>>n; + + len = s.size(); + ll A[len], B[len]; + vll indices,indices2; + a=b=ans=0; + rep(i,0,len) + { + if(s[i] == 'a') + a++; + else + b++; + A[i] = a; + B[i] = b; + + if(A[i]>B[i]) + { + ans++; + indices2.push_back(i); + } + else + indices.push_back(i); + } + + d = a - b; + + if(d>0) + { + ans *= n; + rep(i,0,indices.size()) + { + //cout<n) + term = 0; + if(term>0) + ans += (n-term); + //cout< +#include + +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vll; +typedef pair ii; +typedef vector vii; +typedef set si; +typedef map msi; + +#define rep(i, a, b) \ +for (int i = int(a); i < int(b); i++) // a to b, and variable i is local! +#define TRvi(c, it) \ +for (vi::iterator it = (c).begin(); it != (c).end(); it++) +#define TRvii(c, it) \ +for (vii::iterator it = (c).begin(); it != (c).end(); it++) +#define TRmsi(c, it) \ +for (msi::iterator it = (c).begin(); it != (c).end(); it++) +#define INF 2000000000 // 2 billion +#define mod 1000000007 + +ll power(ll x, ll y) +{ + ll val=1; + x = x%mod; + while(y>0) + { + if(y&1) + { + val = (val*x)%mod; + } + y = y>>1; + x = (x*x)%mod; + } + return val; +} + +int main() +{ + /*clock_t start,end; + start = clock();*/ + ll t,i,j; + long double n,s,y,time; + cin>>t; + while(t--) + { + cin>>n>>s>>y; + vector V(n), D(n), P(n),C(n), I(n); + rep(i,0,n) + cin>>V[i]; + rep(i,0,n) + cin>>D[i]; + rep(i,0,n) + { + cin>>P[i]; + I[i] = P[i]; + } + rep(i,0,n) + cin>>C[i]; + + time = 0.0; + rep(i,0,n) + { + if(D[i] == 1) + { + P[i] = I[i] + (V[i]*time); + } + else + { + P[i] = I[i] - (V[i]*time); + } + if(P[i] >= 0 && D[i] == 0) + { + if( P[i]/V[i] <= y/s || (P[i] - V[i]*y/s) <= 0.000001) + time += (P[i]+C[i])/V[i]; + } + else if((P[i] < 0 && D[i] == 1)) + { + if((-1.0*P[i])/V[i] <= y/s || abs(P[i] + V[i]*y/s) <= 0.000001) + time += (abs(P[i])+C[i])/V[i]; + } + else if(P[i] >= 0 && D[i] == 1 && P[i] < C[i]) + { + time += abs(C[i] - P[i])/V[i]; + } + else if(P[i] < 0 && D[i] == 0 && -1*P[i] < C[i]) + { + time += (C[i] - abs(P[i]))/V[i]; + } + time +=y/s; + } + cout< +#include + +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vll; +typedef pair ii; +typedef vector vii; +typedef set si; +typedef map msi; + +#define rep(i, a, b) \ +for (int i = int(a); i < int(b); i++) // a to b, and variable i is local! +#define TRvi(c, it) \ +for (vi::iterator it = (c).begin(); it != (c).end(); it++) +#define TRvii(c, it) \ +for (vii::iterator it = (c).begin(); it != (c).end(); it++) +#define TRmsi(c, it) \ +for (msi::iterator it = (c).begin(); it != (c).end(); it++) +#define INF 2000000000 // 2 billion +#define mod 1000000007 + +ll power(ll x, ll y) +{ + ll val=1; + x = x%mod; + while(y>0) + { + if(y&1) + { + val = (val*x)%mod; + } + y = y>>1; + x = (x*x)%mod; + } + return val; +} + +int main() +{ + /*clock_t start,end; + start = clock();*/ + ll t,d,n,i,j,K,A,B,q,l,r,ans,a,b,Asq, c; + + cin>>n>>K>>A>>B; + + vll dp(n+1);//,sum(n+1); + //ll s=0; + dp[1] = K; + //sum[1] =(power(K,2))%mod; + dp[2] = (((A%mod)*(K%mod))%mod + ((B%mod)*power(K,2))%mod)%mod; + //cout<=0) + dp[i] = a-b; + else + dp[i] = a-b+mod; + + dp[i] = (power(i,mod-2)*dp[i])%mod; + + //cout<>q; + while(q--) + { + cin>>l>>r; + ans =0; + + if(l == 1) + { + ans = sum[r-1]; + } + else + { + ans = (sum[r-1] - sum[l-2])%mod; + } + if(ans <0) + ans+=mod; + cout< +#include + +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vll; +typedef pair ii; +typedef vector vii; +typedef set si; +typedef map msi; + +#define rep(i, a, b) \ +for (int i = int(a); i < int(b); i++) // a to b, and variable i is local! +#define TRvi(c, it) \ +for (vi::iterator it = (c).begin(); it != (c).end(); it++) +#define TRvii(c, it) \ +for (vii::iterator it = (c).begin(); it != (c).end(); it++) +#define TRmsi(c, it) \ +for (msi::iterator it = (c).begin(); it != (c).end(); it++) +#define INF 2000000000 // 2 billion +#define mod 1000000007 + +ll power(ll x, ll y) +{ + ll val=1; + x = x%mod; + while(y>0) + { + if(y&1) + { + val = (val*x)%mod; + } + y = y>>1; + x = (x*x)%mod; + } + return val; +} + +int main() +{ + /*clock_t start,end; + start = clock();*/ + ll t,d,n,i,j,K,A,B,q,l,r,ans,a,b,Asq, c; + + cin>>n>>K>>A>>B; + + vll dp(n+1),sum(n+1); + //ll s=0; + dp[1] = K; + sum[1] =(power(K,2))%mod; + dp[2] = (((A%mod)*(K%mod))%mod + ((B%mod)*power(K,2))%mod)%mod; + //cout<=0) + dp[i] = a-b; + else + dp[i] = a-b+mod; + + dp[i] = (power(i,mod-2)*dp[i])%mod; + + //cout<>q; + while(q--) + { + cin>>l>>r; + ans =0; + + if(l == 1) + { + ans = sum[r]; + } + else + { + ans = (sum[r] - sum[l-1])%mod; + } + if(ans <0) + ans+=mod; + cout< +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vll; +typedef pair ii; +typedef vector vii; +typedef set si; +typedef map msi; + +#define rep(i, a, b) \ +for (int i = int(a); i < int(b); i++) // a to b, and variable i is local! +#define TRvi(c, it) \ +for (vi::iterator it = (c).begin(); it != (c).end(); it++) +#define TRvii(c, it) \ +for (vii::iterator it = (c).begin(); it != (c).end(); it++) +#define TRmsi(c, it) \ +for (msi::iterator it = (c).begin(); it != (c).end(); it++) +#define INF 2000000000 // 2 billion +#define mod 1000000007 + +int main() { + ll t,n,p,q,c,i,j,k,m,g,b,flag=0; + set > myset; + cin>>p>>q>>c>>m; + //p = 15; q = 15;m=0;c=0; + rep(i,0,m) + { + cin>>g>>b; + if(g