锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
{
int c[1100000]; // element id must start at 1
int size;
int lowbit(int x)
{
return x & (-x);
}
public:
void init(int s = N - 1)
{
size = s;
memset(c,0,size * sizeof(c[0]));
}
int sum(int n) // 鍓峮涓暟鐨勫拰錛屽寘鎷琻
{
int s = 0;
while (n > 0)
{
s += c[n];
n -= lowbit(n);
}
return s;
}
void plus(int p,int x) // add x to the element at position p
{
while (p <= size)
{
c[p] += x;
p += lowbit(p);
}
}
};
]]>
{
BigInteger up, down;
public Fraction (Fraction f)
{
this.up = f.up;
this.down = f.down;
}
public Fraction(String s)
{
this.up = new BigInteger(s);
this.down = new BigInteger("1");
}
public Fraction(BigInteger a, BigInteger b)
{
this.up = a;
this.down = b;
}
public BigInteger getUp()
{
return this.up;
}
public BigInteger getDown()
{
return this.down;
}
public Fraction subtract(Fraction f)
{
BigInteger save1 = this.up.multiply (f.down);
BigInteger save2 = f.up.multiply(this.down);
Fraction tmp = new Fraction(save1.subtract (save2), f.down .multiply ( this.down));
return simplex(tmp);
}
public Fraction add(Fraction f)
{
Fraction tmp = new Fraction ("0");
tmp = tmp.subtract(f);
Fraction ans = new Fraction (this.subtract(tmp));
return ans;
}
public Fraction multiply(Fraction f)
{
Fraction tmp;
tmp = new Fraction(this.up.multiply (f.up), this.down.multiply (f.down));
if (tmp.down.compareTo(new BigInteger("0")) == -1)
{
tmp.down = tmp.down.multiply (new BigInteger("-1"));
tmp.up = tmp.up.multiply (new BigInteger("-1"));
}
return simplex(tmp);
}
public Fraction divide(Fraction f)
{
Fraction tmp = null;
tmp = new Fraction(this.up.multiply (f.down), this.down.multiply (f.up));
if (tmp.down.compareTo(new BigInteger("0")) == -1)
{
tmp.down = tmp.down.multiply (new BigInteger("-1"));
tmp.up = tmp.up.multiply (new BigInteger("-1"));
}
return simplex(tmp);
}
BigInteger gcd(BigInteger a, BigInteger b)
{
if (b.compareTo(new BigInteger("0")) == 0)
return a;
return gcd(b, a.remainder (b));
}
Fraction simplex(Fraction f)
{
BigInteger tmp = gcd(f.up.abs(), f.down.abs ());
f.up = f.up.divide (tmp);
f.down = f.down.divide (tmp);
return f;
}
void print()
{
BigInteger a, b, c;
a = this.getUp ();
b = this.getDown();
c = gcd(a.abs(), b.abs());
a = a.divide (c);
b = b.divide (c);
if (b.compareTo (new BigInteger("1")) == 0)
System.out.println(a);
else
System.out.println (a + "/" + b);
}
}
]]>
#include <string>
#include <algorithm>
using namespace std;
const int SIZE = 10010;
struct node // the node of line tree
{
int i,j; // 鍖洪棿鑼冨洿
node * lson;
node * rson;
int count; // 綰挎瑕嗙洊鏉℃暟
int m; // 嫻嬪害
int line; // 榪炵畫孌墊暟
int lbd,rbd; // 鐢ㄦ潵璁$畻榪炵畫孌墊暟
node(int l,int r)
{
i = l,j = r;
count = m = line = lbd = rbd = 0;
lson = rson = NULL;
}
};
class LineTree
{
node * head;
/* 浠ヤ笅鍑芥暟鍐呴儴浣跨敤,鍙笉鐢ㄨ冭檻 */
void init(node * pnode = NULL)
{
head = pnode;
}
void updateM()
{
if (head->count > 0) // 琚鐩栨弧
head->m = head->j - head->i;
else if (head->j - head->i == 1) // 璇ヨ妭鐐逛負鍙惰妭鐐?/span>
head->m = 0;
else // 鍏朵粬鍐呴儴鑺傜偣鐨勬儏鍐?/span>
head->m = (head->lson)->m + (head->rson)->m;
}
void updateLine()
{
if (head->count > 0)
head->lbd = head->rbd = head->line = 1;
else if (head->j - head->i == 1)
head->lbd = head->rbd = head->line = 0;
else
{
head->lbd = (head->lson)->lbd;
head->rbd = (head->rson)->rbd;
head->line = (head->lson)->line + (head->rson)->line - (head->lson)->rbd * (head->rson)->lbd;
}
}
public:
LineTree();
void clear(); // 娓呯┖綰挎鏁?
void build(int l,int r); // 寤虹珛綰挎鏍?鍖洪棿[l,r];
void insert(int l,int r); // 鎻掑叆涓鏉$嚎孌?
void del(int l,int r); // 鍒犻櫎涓鏉$嚎孌?
int GetM(); // 嫻嬪害;
int GetLine(); // 榪炵畫孌墊暟;
int GetCov(); // 瑕嗙洊綰挎鏁?
~LineTree();
};
LineTree::LineTree()
{
head = NULL;
}
void LineTree::clear() // 娓呯┖綰挎鏁?/span>
{
if (head == NULL)
return;
LineTree temp;
temp.init(head->lson);
temp.clear();
temp.init(head->rson);
temp.clear();
delete head;
head = NULL;
}
void LineTree::build(int l,int r) // 寤虹珛綰挎鏍?鍖洪棿[l,r]
{
head = new node(l,r);
if (r - l > 1)
{
int k = (l + r) / 2;
LineTree temp;
temp.build(l,k);
head->lson = temp.head;
temp.init();
temp.build(k,r);
head->rson = temp.head;
}
}
void LineTree::insert(int l,int r) // 鎻掑叆涓鏉$嚎孌?/span>
{
if (l <= head->i && r >= head->j)
(head->count)++;
else
{
LineTree temp;
if (l < (head->i+head->j)/2)
{
temp.init(head->lson);
temp.insert(l,r);
}
if (r > (head->i+head->j)/2)
{
temp.init(head->rson);
temp.insert(l,r);
}
}
updateM();
updateLine();
}
void LineTree::del(int l,int r) // 鍒犻櫎涓鏉$嚎孌?/span>
{
if (l <= head->i && head->j <= r)
(head->count)--;
else
{
LineTree temp;
if (l < (head->i+head->j)/2)
{
temp.init(head->lson);
temp.del(l,r);
}
if (r > (head->i+head->j)/2)
{
temp.init(head->rson);
temp.del(l,r);
}
}
updateM();
updateLine();
}
int LineTree::GetM() // 嫻嬪害
{
return head->m;
}
int LineTree::GetLine() // 榪炵畫孌墊暟
{
return head->line;
}
int LineTree::GetCov() // 瑕嗙洊綰挎鏁?/span>
{
return head->count;
}
LineTree::~LineTree()
{
this->clear();
}
]]>
#include <string>
#include <stdio.h>
using namespace std;
#define SIZE 500000
void swap(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}
class Heap
{
int size;
int heap[SIZE];
public:
virtual bool cmp(int a,int b) = 0;
private:
inline int fathter(int p)
{
return p / 2;
}
inline int LeftSon(int p)
{
int son = 2 * p;
if (son > size)
return 0;
return son;
}
inline int RightSon(int p)
{
int son = 2 * p + 1;
if (son > size)
return 0;
return son;
}
int ShiftUp(int p)
{
if (p == 1)
return p;
if (cmp(heap[p],heap[fathter(p)]))
{
swap(heap[p],heap[fathter(p)]);
return fathter(p);
}
return p;
}
int ShiftDown(int p)
{
int lagest = p;
if ((LeftSon(p)) && (cmp(heap[LeftSon(p)],heap[lagest])))
lagest = LeftSon(p);
if ((RightSon(p)) && (cmp(heap[RightSon(p)],heap[lagest])))
lagest = RightSon(p);
if (lagest != p)
swap(heap[lagest],heap[p]);
return lagest;
}
public:
Heap() { size = 0; }
int insert(int n);
void del(int p);
void DelHead();
int head();
void init();
bool IsEempty();
};
int Heap::insert(int n)
{
size++;
heap[size] = n;
int where = size;
int p;
while (((p = ShiftUp(where)) != where))
{
where = p;
continue;
}
return where;
}
void Heap::del(int p)
{
heap[p] = heap[size];
size--;
int where;
while (((where = ShiftDown(p)) != p))
{
p = where;
continue;
}
}
void Heap::DelHead()
{
del(1);
}
int Heap::head()
{
if (size == 0)
return -1;
return heap[1];
}
void Heap::init()
{
size = 0;
}
bool Heap::IsEempty()
{
if (size == 0)
return 1;
else
return 0;
}
class MaxHeap : public Heap
{
bool cmp(int a,int b)
{
return a > b;
}
};
class MinHeap : public Heap
{
bool cmp(int a,int b)
{
return a < b;
}
};
int main()
{
return 0;
}
]]>