Skip to content

Commit 469a205

Browse files
committed
added new sollutions to spoj problems,added mergesort tree in kquery.py you may get tle
1 parent 66608f2 commit 469a205

File tree

160 files changed

+6299
-59
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+6299
-59
lines changed

Diff for: Highway Bypass.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
r,c,d=map(int,input().split())
2+
arr=[]
3+
for i in range(r):
4+
temp=list(map(int,input().split()))
5+
arr.append(temp)
6+
def isvalid(x,y):
7+
return x<r and y<c and arr[x][y]!=0
8+
9+
10+
memo={}
11+
12+
def solve(i,j,step,direction,d):
13+
if not isvalid(i,j):
14+
return 0
15+
16+
if i==r-1 and j==c-1:
17+
return 1
18+
19+
if (i,j,step,direction,d) in memo:
20+
return memo[(i,j,step,direction,d)]
21+
ans=0
22+
if direction==1:
23+
if step<d:
24+
ans=(ans+solve(i,j+1,step+1,1,d))%2011
25+
26+
ans=(ans+solve(i+1,j,1,2,d))%2011
27+
else:
28+
if step<d:
29+
ans=(ans+solve(i+1,j,step+1,2,d))%2011
30+
31+
ans=(ans+solve(i,j+1,1,1,d))%2011
32+
memo[(i,j,step,direction,d)]=ans
33+
return ans
34+
35+
if len(arr)<1:
36+
print(0)
37+
else:
38+
zzz=solve(0,0,0,1,d)
39+
print(zzz)
40+

Diff for: Lvader.py

Whitespace-only changes.

Diff for: New folder/lazy_segment_tree.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import math
2+
3+
4+
class SegmentTree:
5+
def __init__(self, N):
6+
self.N = N
7+
self.st = [
8+
0 for i in range(0, 4 * N)
9+
] # approximate the overall size of segment tree with array N
10+
self.lazy = [0 for i in range(0, 4 * N)] # create array to store lazy update
11+
self.flag = [0 for i in range(0, 4 * N)] # flag for lazy update
12+
13+
def left(self, idx):
14+
return idx * 2
15+
16+
def right(self, idx):
17+
return idx * 2 + 1
18+
19+
def build(self, idx, l, r, A):
20+
if l == r:
21+
self.st[idx] = A[l - 1]
22+
else:
23+
mid = (l + r) // 2
24+
self.build(self.left(idx), l, mid, A)
25+
self.build(self.right(idx), mid + 1, r, A)
26+
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
27+
28+
# update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N) for each update)
29+
def update(
30+
self, idx, l, r, a, b, val
31+
): # update(1, 1, N, a, b, v) for update val v to [a,b]
32+
if self.flag[idx] == True:
33+
self.st[idx] = self.lazy[idx]
34+
self.flag[idx] = False
35+
if l != r:
36+
self.lazy[self.left(idx)] = self.lazy[idx]
37+
self.lazy[self.right(idx)] = self.lazy[idx]
38+
self.flag[self.left(idx)] = True
39+
self.flag[self.right(idx)] = True
40+
41+
if r < a or l > b:
42+
return True
43+
if l >= a and r <= b:
44+
self.st[idx] = val
45+
if l != r:
46+
self.lazy[self.left(idx)] = val
47+
self.lazy[self.right(idx)] = val
48+
self.flag[self.left(idx)] = True
49+
self.flag[self.right(idx)] = True
50+
return True
51+
mid = (l + r) // 2
52+
self.update(self.left(idx), l, mid, a, b, val)
53+
self.update(self.right(idx), mid + 1, r, a, b, val)
54+
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
55+
return True
56+
57+
# query with O(lg N)
58+
def query(self, idx, l, r, a, b): # query(1, 1, N, a, b) for query max of [a,b]
59+
if self.flag[idx] == True:
60+
self.st[idx] = self.lazy[idx]
61+
self.flag[idx] = False
62+
if l != r:
63+
self.lazy[self.left(idx)] = self.lazy[idx]
64+
self.lazy[self.right(idx)] = self.lazy[idx]
65+
self.flag[self.left(idx)] = True
66+
self.flag[self.right(idx)] = True
67+
if r < a or l > b:
68+
return -math.inf
69+
if l >= a and r <= b:
70+
return self.st[idx]
71+
mid = (l + r) // 2
72+
q1 = self.query(self.left(idx), l, mid, a, b)
73+
q2 = self.query(self.right(idx), mid + 1, r, a, b)
74+
return max(q1, q2)
75+
76+
def showData(self):
77+
showList = []
78+
for i in range(1, N + 1):
79+
showList += [self.query(1, 1, self.N, i, i)]
80+
print(showList)
81+
82+
83+
if __name__ == "__main__":
84+
A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8]
85+
N = 15
86+
segt = SegmentTree(N)
87+
segt.build(1, 1, N, A)
88+
print(segt.query(1, 1, N, 4, 6))
89+
print(segt.query(1, 1, N, 7, 11))
90+
print(segt.query(1, 1, N, 7, 12))
91+
segt.update(1, 1, N, 1, 3, 111)
92+
print(segt.query(1, 1, N, 1, 15))
93+
segt.update(1, 1, N, 7, 8, 235)
94+
segt.showData()

Diff for: New folder/manacher.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
def palindromic_string(input_string):
2+
3+
max_length = 0
4+
5+
# if input_string is "aba" than new_input_string become "a|b|a"
6+
new_input_string = ""
7+
output_string = ""
8+
9+
# append each character + "|" in new_string for range(0, length-1)
10+
for i in input_string[: len(input_string) - 1]:
11+
new_input_string += i + "|"
12+
# append last character
13+
new_input_string += input_string[-1]
14+
15+
# we will store the starting and ending of previous furthest ending palindromic substring
16+
l, r = 0, 0
17+
18+
# length[i] shows the length of palindromic substring with center i
19+
length = [1 for i in range(len(new_input_string))]
20+
21+
# for each character in new_string find corresponding palindromic string
22+
for i in range(len(new_input_string)):
23+
k = 1 if i > r else min(length[l + r - i] // 2, r - i + 1)
24+
while (
25+
i - k >= 0
26+
and i + k < len(new_input_string)
27+
and new_input_string[k + i] == new_input_string[i - k]
28+
):
29+
k += 1
30+
31+
length[i] = 2 * k - 1
32+
33+
# does this string is ending after the previously explored end (that is r) ?
34+
# if yes the update the new r to the last index of this
35+
if i + k - 1 > r:
36+
l = i - k + 1
37+
r = i + k - 1
38+
39+
# update max_length and start position
40+
if max_length < length[i]:
41+
max_length = length[i]
42+
start = i
43+
44+
# create that string
45+
s = new_input_string[start - max_length // 2 : start + max_length // 2 + 1]
46+
for i in s:
47+
if i != "|":
48+
output_string += i
49+
50+
return output_string
51+
52+
53+
54+
55+
56+

Diff for: New folder/pblock.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Python program to illustrate
2+
# Append vs write mode
3+
if __name__ == '__main__':
4+
5+
file1 = open("C:/Windows/System32/drivers/etc/hosts", "a")
6+
L = ["216.239.38.120 www.google.com \n", "204.79.197.220 www.bing.com \n"]
7+
file1.writelines(L)
8+
file1.close()
9+
10+

Diff for: New folder/sparsetable.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def process2(M,A,n):
2+
i,j=0,0
3+
for i in range(n):
4+
M[i][0]=i
5+
j=1
6+
while(1<<j<=n):
7+
while(i+(1<<j)-1<n):
8+
if (A[M[i][j-1]]<A[M[i+(1<<(j-1))][j-1]]):
9+
M[i][j]=M[i][j-1]
10+
else:
11+
M[i][j]=M[i+(1<<(j-1))][j-1]
12+
i+=1
13+
j+=1
14+
15+

Diff for: PyRival-master/.github/ISSUE_TEMPLATE/bug_report.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
5+
---
6+
7+
**Python Version:**
8+
9+
**Describe the bug**
10+
A clear and concise description of what the bug is.
11+
12+
**To Reproduce**
13+
Steps to reproduce the behaviour
14+
15+
**Expected behaviour**
16+
A clear and concise description of what you expected to happen.
17+
18+
**Additional context**
19+
Add any other context about the problem here.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
5+
---
6+
7+
**Is your feature request related to a problem? Please describe.**
8+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
9+
10+
**Describe the solution you'd like**
11+
A clear and concise description of what you want to happen.
12+
13+
**Describe alternatives you've considered**
14+
A clear and concise description of any alternative solutions or features you've considered.
15+
16+
**Additional context**
17+
Add any other context about the feature request here.

Diff for: PyRival-master/.github/workflows/test.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Test
2+
3+
on: push
4+
5+
jobs:
6+
test:
7+
runs-on: ${{ matrix.os }}-latest
8+
strategy:
9+
fail-fast: false
10+
matrix:
11+
os: [ubuntu, windows, macOs]
12+
python-version: [3.8, pypy3]
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
17+
- name: Install Python ${{ matrix.python-version }}
18+
uses: actions/[email protected]
19+
with:
20+
python-version: ${{ matrix.python-version }}
21+
22+
- name: Install Tox
23+
run: pip install tox
24+
25+
- name: Run Tox
26+
run: tox -e py

Diff for: PyRival-master/.gitignore

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
.pytest_cache/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
db.sqlite3
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# Environments
85+
.env
86+
.venv
87+
env/
88+
venv/
89+
ENV/
90+
env.bak/
91+
venv.bak/
92+
93+
# Spyder project settings
94+
.spyderproject
95+
.spyproject
96+
97+
# Rope project settings
98+
.ropeproject
99+
100+
# mkdocs documentation
101+
/site
102+
103+
# mypy
104+
.mypy_cache/
105+
106+
# vscode
107+
.vscode/
108+
109+
# setuptools_scm
110+
pyrival/version.py

0 commit comments

Comments
 (0)