Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
130 changes: 130 additions & 0 deletions Convex_Hull.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
struct point //Replace double with int if not required
{
double x, y;

point () {}

point(int x, int y) : x(x), y(y) {}

void operator =(const point &p)
{
x = p.x, y = p.y;
}

bool operator <(const point&p)
{
if (x == p.x)
return y < p.y;
return x < p.x;
}

point operator +(const point&p) const
{
point pt(x + p.x, y + p.y);
return pt;
}

point operator -(const point&p) const
{
point pt(x - p.x, y - p.y);
return pt;
}

double crossProduct(const point &p) const
{
return x * p.y - y * p.x;
}

int dotProduct(const point &p) const
{
return x * p.x + y * p.y;
}

double dist()
{
return x * x + y * y;
}
};


bool comp(point &p1, point &p2)
{
if (p1.x != p2.x)
return p1.x < p2.x;
return p1.y < p2.y;
}

bool cw(point &a, point &b, point &c)
{
int area = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y);
return area < 0;
}

bool ccw(point &a, point &b, point &c)
{
int area = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y);
return area > 0;
}
// 1 => Strictly inside; -1 => Border; 0 => Outside
int point_in_poly(const vector<point> & poly, point p) {
int many = 0;
for (int i = 0; i < (int)poly.size(); i++) {
Point a = poly[i], b = poly[i + 1 < (int) poly.size() ? i + 1 : 0];
if (a.x > b.x) swap(a, b);
if (a.x <= p.x && p.x <= b.x) {
if (abs(a.x - b.x) == 0) {
if (min(a.y, b.y) <= p.y && p.y <= max(a.y, b.y)) return -1;
} else {
double y = a.y + 1. * (b.y - a.y) / (b.x - a.x) * (p.x - a.x);
if (abs(y - p.y) <= E) return -1;
if (y >= p.y && p.x < b.x) many++;
}
}
}
return many % 2;
}
vector<point> convex_hull(vector<point> &v)
{
if (v.size() == 1)
return v;

sort(v.begin(), v.end(), comp);

point p1 = v[0], p2 = v.back();

vector<point> up, down;
up.push_back(p1);
down.push_back(p1);

for (int i = 1; i < v.size(); i++)
{
if (i == v.size() - 1 || cw(p1, v[i], p2))
{
while (up.size() >= 2 && !cw(up[up.size() - 2], up[up.size() - 1], v[i]))
up.pop_back();
up.push_back(v[i]);
}
if (i == v.size() - 1 || ccw(p1, v[i], p2))
{
while (down.size() >= 2 && !ccw(down[down.size() - 2], down[down.size() - 1], v[i]))
down.pop_back();
down.push_back(v[i]);
}
}

for (int i = down.size() - 2; i > 0; i--)
up.push_back(down[i]);

return up;
}

//Problem 0: https://www.codechef.com/ACM16CHN/problems/CHN16B
//Solution 0: Print the sum of cost of all points in Convex Hull https://ideone.com/lEt7K1

//Problem 1 (Polygon Congruence): http://codeforces.com/contest/1017/problem/E
//Solution 1: http://codeforces.com/contest/1017/submission/41401690

//Problem 2 and Solution: http://codeforces.com/gym/101606/submission/41541222

//Problem 3:https://www.codechef.com/JUNE20A/problems/CONTAIN
//Solution 3: https://www.codechef.com/viewsolution/34657860
17 changes: 17 additions & 0 deletions Rod_Cutting_Problem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
int main()
{
//CODE FOR FINDING MAX VALUE OBTAINED BY CUTTING ROD INTO PIECES OF Ith SIZE GIVES PRICE OF A[i] AND FINDING FOR ROD OF LENGTH N WITH PRICE[0..N-1] GIVEN.
ll n; cin >> n; ll a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int dp[n + 5] = {0};
for (int i = 1; i <= n; i++)
{
int max_val = INT_MIN;
for (ll j = 0; j < i; j++)
{
max_val = max(max_val, a[j] + dp[i - j - 1]); //i-j is difference needed for j to complete till i
}
dp[i] = max_val;
}
cout << dp[n];
}
116 changes: 116 additions & 0 deletions Sweepline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
PRINTS AREA OF NESTED REACTANGLE UNION USING SWEEPLINE
*/

/*
KUNAL RAUT (ZUKONIT14) :- "DO WHAT YOU LIKE!"
-PICT,PUNE! :)
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007
#define mod9 1000000009
#define pi 3.1415926535
#define MAXN 1000005
#define N 1000001
#define MAX2N 1*1000 + 10
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define ms(s, n) memset(s, n, sizeof(s))
#define prec(n) fixed<<setprecision(n)
#define forci(p,n) for(ll i=p;i<(ll)n;i++)
#define forcj(p,n) for(ll j=p;j<(ll)n;j++)
#define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define bits(a) __builtin_popcount(a)
#define test ll t;cin>>t;for(ll testi=1;testi<=t;testi++)
#define djokovic freopen("input00.txt", "r", stdin);freopen("output00.txt", "w", stdout);
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b);}
ll lcm(ll a, ll b) { return (a / gcd(a, b) * b);}
ll expo(ll x, ll y) {
ll res = 1; x = x % mod; while (y > 0) {
if (y & 1)res = (1ll * res * x) % mod;
y = y >> 1; x = (1ll * x * x) % mod;
} return res;
}
ll ncr(ll n, ll r) { ll res = 1; if (r > n - r)r = n - r; for (ll i = 0; i < r; i++) { res *= n - i; res /= i + 1; } return res;}
ll max(ll a, ll b) {return (a > b) ? a : b;}
bool prime(ll n) {ll i; for (i = 2; i <= sqrt(n); i++) {if (n % i == 0)return false;} return true;}
bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b)
{ if (a.second == b.second)
return a.first < b.first;
return (a.second < b.second);
}

//https://www.youtube.com/watch?v=WTJSt4wP2ME

ll rr[] = {0, 1, 1, 1, 0, -1, -1, -1};
ll cc[] = {1, 1, 0, -1, -1, -1, 0, 1};
struct event
{
ll x, t, y1, y2;
};
bool comp(const event &a, const event &b)
{
if (a.x == b.x)
return (a.t < b.t);
return (a.x < b.x);
}
ll n, k, ans, p[MAXN], a[MAXN], vis[MAXN];
vector<event>ve;
multiset<ll>low, up;
void solve()
{
cin >> n;
for (ll i = 1; i <= n; i++)
{
ll x1, x2, y1, y2; cin >> x1 >> x2 >> y1 >> y2;
ve.pb({x1, 1, y1, y2});
ve.pb({x2, -1, y1, y2});
}
sort(all(ve), comp);
ll laste = ve[0].x;
for (auto e : ve)
{
//cout << e.x << " " << e.t << "\n";
if (e.t == 1) //ADDING EVENT PUTTING Y1 INTO LOW SET AND Y2 IN UP SET AND TAKING THE MAX IN UP SET AND MIN IN LOW SET AND TAKING THEIR DIFFERENCE AND MUKLTIPLE NY X DIFF
{
if (up.size())
{
auto it = up.end(); it--;
auto it1 = low.begin();
ans += ((*it - *it1) * (e.x - laste));
}
up.insert(max(e.y1, e.y2));
low.insert(min(e.y1, e.y2));
}
else //REMOVING EVENT ALSO ERASING Y1 AND Y2 AND TAKING ANSWER AND THEN REMOVING THE Y1 AND Y2
{
auto it = up.end(); it--;
auto it1 = low.begin();
ans += ((*it - *it1) * (e.x - laste));
up.erase(up.find(max(e.y1, e.y2)));
low.erase(low.find(min(e.y1, e.y2)));
}
laste = e.x;
//cout << ans << " " << e.x << " " << e.t << " " << "\n";
}
cout << ans;
}
signed main()
{
bolt;
#ifndef ONLINE_JUDGE
djokovic;
#endif
solve();
}

//SOMETHING LIKE THIS : https://codeforces.com/blog/entry/20377
// FOR TGHE CODE ALIGNED THE BASE ON OX JUST TAKING ONE Y AND TAKIBNG THAT SET'S TOP WITH X DIFF