仓颉造代码
2024/11/1 - 简述
三周前,由华为编写的新编程语言横空出世。这就是仓颉语言。受到信息差的限制,到了今天我才知道他们把编译器写好了,于是下载来试了一下。
结果光是理解怎么 IO 就看了一整天文档
于是浅浅打了一个 \(a+b\) 出来。
接下来大概会在这里投一些用仓颉语言写的杂项。
Markdown 还不支持仓颉语言,所以我会套一个 Javascript 的语法高亮,因为语法比较像。
package cangjieTrial
import std.console.*
import std.convert.*
func readLine(): Array<String> {
let str: String = Console.stdIn.readln() ?? ""
return str.split(" ", removeEmpty: true)
}
main(): Int64 {
let lst1 = readLine()
var a: Int64 = Int64.parse(lst1[0])
var b: Int64 = Int64.parse(lst1[1])
println(a + b)
return 0
}
2024/11/2 - IO 重构 / 并查集
之前的 IO 有点难用,今天新封装了一个,好用了很多。虽然还是没有 C++ 的好用。另外写了一个并查集。
package cangjieForOI.io
import std.console.*
import std.convert.*
public class IOBuffer {
var buf: Array<String> = Array<String>()
var tidx: Int64 = 0
public init() {
this.tidx = 0
this.buf = Array<String>()
}
public func getNext(): String {
this.tidx++
if (buf.size <= tidx) {
this.tidx = 0
this.buf = (Console.stdIn.readln() ?? "").split(" ", removeEmpty: true)
}
this.buf[this.tidx]
}
public func getNextInt64(): Int64 {
Int64.parse(this.getNext())
}
}
package cangjieForOI.dsu
import cangjieForOI.io.*
public class DSU {
var n: Int64 = 0
var f: Array<Int64> = Array<Int64>()
public init(tn: Int64) {
n = tn
f = Array<Int64>(n) {i => i}
}
public func find(tx: Int64): Int64 {
var x = tx
while (x != f[x]) {
x = f[x]
}
return x
}
public func merge(tx: Int64, ty: Int64) {
var x = tx
var y = ty
x = find(x)
y = find(y)
if (x == y) {
return
}
f[y] = x
}
}
2024/11/3 - 线段树
略微整了一下线段树,感觉类型写后面还是挺别扭的。
package cangjieForOI.segmentTree
public class SegmentTree {
var sgt: Array<Int64> = Array<Int64>()
var tag: Array<Int64> = Array<Int64>()
var n: Int64 = 0
var src: Array<Int64> = Array<Int64>()
func psh(l: Int64, r: Int64, x: Int64): Bool {
let mid: Int64 = (l + r) >> 1
this.sgt[x << 1] += this.tag[x] * (mid - l + 1)
this.sgt[x << 1 | 1] += this.tag[x] * (r - mid)
this.tag[x << 1] += this.tag[x]
this.tag[x << 1 | 1] += this.tag[x]
this.tag[x] = 0
return true
}
func build(l: Int64, r: Int64, x: Int64): Int64 {
if (l == r) {
this.sgt[x] = this.src[l - 1]
return this.sgt[x]
}
let mid: Int64 = (l + r) >> 1
this.build(l, mid, x << 1)
this.build(mid + 1, r, x << 1 | 1)
this.sgt[x] = this.sgt[x << 1] + this.sgt[x << 1 | 1]
return this.sgt[x]
}
public init(tn: Int64, tsrc: Array<Int64>) {
this.n = tn
this.src = tsrc
this.sgt = Array<Int64>(tn << 2, item: 0)
this.tag = Array<Int64>(tn << 2, item: 0)
this.build(1, n, 1)
}
public init(tn: Int64) {
this.n = tn
this.sgt = Array<Int64>(tn << 2, item: 0)
this.tag = Array<Int64>(tn << 2, item: 0)
}
func update(l: Int64, r: Int64, x: Int64, lb: Int64, rb: Int64, v: Int64): Bool {
if (l >= lb && r <= rb) {
this.sgt[x] += v * (r - l + 1)
this.tag[x] += v
return true
}
if (this.sgt[x] != 0) {
this.psh(l, r, x)
}
let mid: Int64 = (l + r) >> 1
if (lb <= mid) {
this.update(l, mid, x << 1, lb, rb, v)
}
if (rb > mid) {
this.update(mid + 1, r, x << 1 | 1, lb, rb, v)
}
this.sgt[x] = this.sgt[x << 1] + this.sgt[x << 1 | 1]
return true
}
func query(l: Int64, r: Int64, x: Int64, lb: Int64, rb: Int64): Int64 {
if (l >= lb && r <= rb) {
return this.sgt[x]
}
if (this.tag[x] != 0) {
this.psh(l, r, x)
}
let mid: Int64 = (l + r) >> 1
var res: Int64 = 0
if (lb <= mid) {
res += query(l, mid, x << 1, lb, rb)
}
if (rb > mid) {
res += query(mid + 1, r, x << 1 | 1, lb, rb)
}
this.sgt[x] = this.sgt[x << 1] + this.sgt[x << 1 | 1]
return res
}
func update(l: Int64, r: Int64, v: Int64) {
update(1, n, 1, l, r, v)
}
func query(l: Int64, r: Int64) {
query(1, n, 1, l, r)
}
}