Skip to content

Commit

Permalink
Codemania
Browse files Browse the repository at this point in the history
  • Loading branch information
harrypotter0 committed Feb 17, 2018
1 parent 4174431 commit 3b90ba5
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 3 deletions.
45 changes: 45 additions & 0 deletions CP/Codeforces Round #464 (Div. 2)/prob1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
n = int(input())
arr = map(int,raw_input().split())
a=[]
f=0
j=0
for i in range(0,n):
vec = arr[i]
while j<n and vec!=n:
if(arr[i] in a):
# print(a)
f=1
break
if(vec!=arr[vec-1]):
vec=arr[vec-1]
a.append(vec)
j+=1
print(a)
if(f and len(a)==3): break
del a[:]

# del lst[:]

# print(a)
if(f and len(a)==3):
print("YES")
else:
print("NO")


'''
5
2 4 5 1 3
5
4 2 1 3 5
5
4 1 2 3 5
5
5 5 5 5 1
'''
74 changes: 74 additions & 0 deletions CP/Codemania/prob5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll siz = 5e5 + 4;
const ll inf = 1e18;
ll arr[siz], tree[4 * siz]; //Size of Segment tree is never going to be more than 4 * siz.

void build(ll node, ll start, ll end) {
if(start == end)
tree[node] = arr[start]; // Leaf node will have a single element.
else {
ll mid = (start + end) / 2;
build(2 * node, start, mid); // Recurse on the left child.
build(2 * node + 1, mid + 1, end); // Recurse on the right child.
// Internal node will have the sum of both of its children.
tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}
}

ll query(ll node, ll start, ll end, ll l, ll r) {
if(r < start || end < l)
return -inf; // range represented by a node is completely outside the given range.
if(l <= start && end <= r)
return tree[node]; // range represented by a node is completely inside the given range.
// range represented by a node is partially inside and partially outside the given range.
ll mid = (start + end) / 2;
ll s1 = query(2 * node, start, mid, l, r);
ll s2 = query(2 * node + 1, mid + 1, end, l, r);
return max(s1 ,s2);
}

int main(){
ll n, q;
scanf("%lld %lld", &n, &q);
vector<pair<ll, ll> > v;
map<ll, ll> mp;
for(ll i = 0; i < n; ++i){
ll l, r, s;
scanf("%lld %lld %lld", &l, &r, &s);
mp[l] += s;
mp[r + 1] = mp[r + 1] - s;
}
vector<ll> v1;
for(auto it = mp.begin(); it != mp.end(); ++it){
v.push_back({it->first, it->second});
v1.push_back(it->first);
}
sort(v.begin(), v.end());
sort(v1.begin(), v1.end());
arr[0] = v[0].second;
for(ll i = 1; i < v1.size(); ++i){
arr[i] = arr[i - 1] + v[i].second;
}
/*for(int i = 0; i < v1.size(); ++i){
cout<<v[i].first<<" "<<arr[i]<<"\n";
}*/
build(1, 0, v1.size() - 1);
while(q--){
ll l, r;
scanf("%lld %lld", &l, &r);
ll left = lower_bound(v1.begin(), v1.end(), l) - v1.begin();
if(v1[left] > l && left > 0)
left--;
ll right = upper_bound(v1.begin(), v1.end(), r) - v1.begin();
right--;
if(right < 0 || v1[v1.size() - 1] < l){
//cout<<"OK\n";
printf("0\n");
}
else{
printf("%lld\n", query(1, 0, v1.size() - 1, left, right));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
ans=min(a[n-1]-x,y-a[0])
for i in range(n-1):
ans=min(ans,max(a[i]-x,y-a[i+1]) )
print ans

print ans
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
ps[1] = True
for i in range(len(ps)):
if not ps[i]:
for k in range(i*i, i, len(ps)):
for k in range( i*i, i, len(ps)):
ps[k] = True
print(ps)
primes = [i for i, v in enumerate(ps) if not v]

t = int(raw_input())
Expand Down

0 comments on commit 3b90ba5

Please sign in to comment.