loading...

Daily Coding Challenge #65

wingkwong profile image Wing-Kam ・2 min read

About

This is a series of Daily Coding Challenge. Each day I show a few solutions written in C++. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.


/*
Angle Between Hands of a Clock

Given two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand.


Example 1:

Input: hour = 12, minutes = 30
Output: 165
Example 2:

Input: hour = 3, minutes = 30
Output: 75
Example 3:

Input: hour = 3, minutes = 15
Output: 7.5
Example 4:

Input: hour = 4, minutes = 50
Output: 155
Example 5:

Input: hour = 12, minutes = 0
Output: 0


Constraints:

1 <= hour <= 12
0 <= minutes <= 59
Answers within 10^-5 of the actual value will be accepted as correct.
*/

class Solution {
public:
    double angleClock(int hour, int minutes) {
        // each minute is 6 degree (360/60)
        // each hour is 30 degree (360/12) 
        // echo hour has 60 mins so each min is 0.5 degree (30/60)
        double minute=minutes*6, hr=hour*30+(double)minutes/2, angle=abs(hr-minute);
        // if it is obtuse, make it acute
        return min(angle, 360-angle);
    }
};

/*
ITMO Academy: pilot course - Segment Tree - A. Segment Tree for the Sum
https://codeforces.com/edu/course/2/lesson/4/1/practice/contest/273169/problem/A
*/

#include <bits/stdc++.h>
using namespace std; 
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

struct segtree {
    int size;
    vector<long long> sums;

    void init(int n){
        size = 1;
        while(size < n) size *= 2;
        sums.assign(2*size,0LL);
    }

    void set(int i, int v, int x, int lx, int rx){
        if(rx-lx==1){
            sums[x]=v;
            return;
        }

        int m = (lx+rx)/2;
        if(i<m) set(i,v,2*x+1,lx,m);
        else set(i,v,2*x+2,m,rx);
        sums[x] = sums[2*x+1]+sums[2*x+2];
    }

    void set(int i, int v){
        set(i, v, 0, 0, size);
    }

    long long sum(int l, int r, int x, int lx, int rx){
        // no intersection
        if(lx>=r||l>=rx) return 0;
        // inside
        if(lx>=l&&rx<=r) return sums[x];
        // go to both left and right side
        int m=(lx+rx)/2;
        long long s1 = sum(l,r,2*x+1,lx,m);
        long long s2 = sum(l,r,2*x+2,m,rx);
        return s1+s2;
    }

    long long sum(int l, int r){
        return sum(l,r,0,0,size);
    }

};


int main()
{
    FAST_INP;
    int n,m,v;
    cin >> n >> m;
    segtree st;
    st.init(n);
    for(int i=0;i<n;i++){
        cin >> v;
        st.set(i,v);
    }

    for(int i=0;i<m;i++){
        int op;
        cin >> op;
        if(op==1) {
            int i,v;
            cin >> i >> v;
            st.set(i,v);
        } else {
            int l, r;
            cin >> l >> r;
            cout << st.sum(l,r) << endl;
        }
    }

    return 0;
}



The source code is available in corresponding repo below. Star and watch for timely updates!

GitHub logo wingkwong / leetcode

🏆 A Collection of my LeetCode Solutions with Explanations 🏆

GitHub logo wingkwong / hackerrank

🏆 A Collection of my HackerRank Solutions with Explanations 🏆

GitHub logo wingkwong / codeforces

🏆 A Collection of my Codeforces Solutions with Explanations 🏆

GitHub logo wingkwong / atcoder

🏆 A Collection of my AtCoder Solutions with Explanations 🏆

Posted on by:

wingkwong profile

Wing-Kam

@wingkwong

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.

Discussion

markdown guide