Thursday, May 18, 2023

Competive Programming ||

 #include <iostream>

#include "bits/stdc++.h"

using namespace std;


typedef unsigned long long ull;


int main()

{

ull n,k,q;

cin >> n >> k >> q;

vector<ull> temp(200001, 0);

vector<ull> bt(200001, 0);


for (int i = 0; i < n-1; ++i)

{

ull x,y;

cin >> x >> y;

temp[x]++;

temp[y+1]--;

}


ull sum = 0;

for(int i =0; i < 200001; i++)

{

sum += temp[i];

bt[i] = sum;

if(bt[i] >= k){

bt[i] = 1;

}

else

{

bt[i] = 0;

}

}


// final loop

for(int i = 1; i < 200001; i++)

{

bt[i] += bt[i- 1];

}


while(q--)

{

ull x,y;

cin >> x >> y;

ull ans = bt[y];

if(x - 1 >= 0){

ans -= bt[x - 1];

}

cout << ans << endl;

}


    return 0;

}


Tuesday, May 9, 2023

Competitive Programming

  • Build system in Sublime

    { "cmd": ["g++.exe", "-std=c++14", "${file}", "-o", "${file_base_name}.exe", "", "${file_base_name}.exeoutputf.in"], "shell": true, "working_dir": "${file_path}", "selector": "source.cpp", "variants": [ { "name": "Run", "cmd": ["cmd", "/C", "echo '${file_base_name}, it's build success :)'"], "shell": true, "working_dir": "${file_path}" } ] }
  • Multidimensional Array { int R, C;
    cin >> R >> C;
    // Input matrix A int A[100][100];
    for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
    cin >> A[i][j];
    } }
    // Input matrix B int B[100][100];
    for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
    cin >> B[i][j];
    } } // Call the function to add matrices A and B addMatrices(A, B, R, C, 0, 0);
    // Print the resulting matrix for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
    cout << A[i][j] << " ";
    } cout << endl;
    } return 0;


  • 1D Array


    #include
    using namespace std;
    const int MAX_SIZE = 100;
    int main() {
    int n;
    int arr[MAX_SIZE];
    cout << "Enter the size of the array (up to " << MAX_SIZE << "): ";
    cin >> n;
    cout << "Enter " << n << " integers: ";
    for (int i = 0; i < n; i++) {
    cin >> arr[i];
    } cout << "The array contains: ";
    for (int i = 0; i < n; i++) {
    cout << arr[i] << " ";
    } cout << endl;
    return 0;
    }