Thank you for participating in the contest! We hope you enjoy the problems.This is an editorial Blog written for the Contest CP101 2022. In order to solve the problems of the contest click here.
Ritam learns to Dance
Kuldeep is a member of UDC. Ritam wants to learn dance from Kuldeep. Kuldeep gives Ritam a sequence of steps. He has to perform all the substrings of the steps. But he faces a problem where the substring of the string is an even number. Find out the number of step sequences where he faces problems. The substrings can have leading zeros. You have to solve the problem for “t” test cases.
First line of input consists of “t” denoting the number of test cases. Following “t” lines consist of a string containing digits between 0 and 9.
You have to output one number denoting the number of substrings which are even.
Constraints:
1 <= t <= 1e5
1 <= length of string <= 1e5
Sum of length of strings over all test cases does not exceed 1e5.
In this task you have to calculate the number of substrings which are even.A substring is a continuous subsequence of string for example (if s=“abcde” then “abc”,”cde”,”bcd” are substrings but “ac”,”acd” are not substrings of s).
As we know the even number has an even digit at last. So we have to traverse the string and if we found an even digit then all the substring which have that digit in the end will be considered in the answer ( for example let s=”123456” and we are at 5th position (zero based indexing) where the character is 6 then all 123456, 23456,3456,456,56,6 will be counted in the answer) hence if we are at position i (zero based indexing) from the start then there will be i+1 numbers which have s[i] digit at the end.
So we will get our answer by simply adding the (i+1) to our answer at every such position i where s[i] is a multiple of 2.So the answer to our example will be 2 + 4 + 6 = 12.
// code by Aniket Sukhija
#include<bits/stdc++.h>
using namespace std;
#define ll long long
void solve(){
string s; cin>>s;
ll n = s.size();
ll ans = 0;
for(int i=0;i<n;i++){
if(int(s[i] - '0')%2==0){
ans += (i+1);
}
}
cout<<ans<<endl;
return;
}
int main(){
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}
Scavenger Hunt
You are participating in a game organized by seniors. What you need to do in this game is that you have to find some objects which are hidden all around the campus. For the sake of simplicity we assume that the campus is a simple straight line. Now you have been given “n” objects. Now you ask a particular guy Paras for help who decides to help you out. To make it more fun, he decides not to tell you all the answers correctly. You ask him about the item “m” and he answers the query as l, r, which means that the item is present between l’th and r’th position in the campus. You need to find out whether he lied or is telling the truth.
Input:
In the first line you have given n, the number of elements in the array 1<=n<=1e5.
In the next line you have given n elements of the array. Each element is unique and in the range [0,1e9].
In the next line you have given q ,number of queries which will be asked 1<=q<=1e5.
In the next q lines you have given l,r and m. (1<=l<=r<=n and m is in the range [0,1e9]
Output:
You have to print the answer of q queries in q lines where ith line has the answer of ith query.
In this task you have given an array (let the array be a) with distinct digits and you need to tell whether a particular number lies in (a[l],a[l+1],........,a[r-1],a[r]) or not.
So from problem it is clear that you have to store the position of a particular number but a vector is not feasible here because the range of numbers is 10^9 and we cannot allocate that much memory hence we are using std::map for hashing in this problem.Let suppose the position of the number is k then if both l and r is less than k (1....l………r….k…….n) or both l and r are greater than k (1….k…l….r…..n) then the answer is no otherwise the answer will always be yes because now the number lies between the lth and the rth index i.e. (1……l….k….r…….n) k lies between l and r.
//Code by Ritam
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define vi vector<int>
#define vll vector<ll>
#define tests int t; cin >> t; while(t--)
#define pb push_back
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n; cin >> n;
map<ll, int> m;
for(int i = 0; i < n; i++) {
ll x; cin >> x;
m[x] = i+1;
}
tests {
int l, r; ll x; cin >> l >> r >> x;
if(l <= m[x] && m[x] <= r) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}