Exemplo n.º 1
0
void solve() {
  fill(cnt, cnt + A, 0);
  forn (i, n) cnt[s[i]]++;
  forn (i, A - 1) cnt[i + 1] += cnt[i];
  forn (i, n) p[--cnt[s[i]]] = i;
  c[p[0]] = 0;
  for (int i = 1, classes = 1; i < n; ++i) {
    if (s[p[i]] != s[p[i-1]]) classes++;
    c[p[i]] = classes - 1;
  }
  for (int h = 0; (1 << h) < n; ++h) {
    forn (i, n) {
      pn[i] = p[i] - (1 << h);
      if (pn[i] < 0) pn[i] += n;
    }
    fill(cnt, cnt + n, 0);
    forn (i, n) cnt[c[pn[i]]]++;
    forn (i, n - 1) cnt[i + 1] += cnt[i];
    for (int i = n - 1; i >= 0; --i) p[--cnt[c[pn[i]]]] = pn[i];
    cn[p[0]] = 0;
    for (int i = 1, classes = 1; i < n; ++i) {
      int mid1 = (p[i]+(1<<h)) % n,  mid2 = (p[i-1]+(1<<h)) % n;
      if (c[p[i]] != c[p[i-1]] || c[mid1] != c[mid2]) classes++;
      cn[p[i]] = classes - 1;
    }
    copy(cn, cn + n, c);
  }
Exemplo n.º 2
0
int main() {
    scanf("%d", &n);
    forn(i, n) scanf("%d%d", x + i, y + i);
    forn(i, n) ord[i] = i;
    qsort(ord, n, sizeof(int), cmp);
    forn(ii, n) {
        int i = ord[ii];
        ans[i] = fenGet(x[i]);
        fenInc(x[i]);
    }
Exemplo n.º 3
0
void augment() {
	if (max_match == n) return; 
	int x, y, root, q[N], wr = 0, rd = 0; 
	memset(S, false, sizeof(S)), memset(T, false, sizeof(T)); 
	memset(prev2, -1, sizeof(prev2)); 
	forn (x, n) if (xy[x] == -1){
		q[wr++] = root = x, prev2[x] = -2;
		S[x] = true; break; }
	forn (y, n) slack[y] = lx[root] + ly[y] - cost[root][y], slackx[y] = root;
	while (true){
		while (rd < wr){
			x = q[rd++];
			for (y = 0; y < n; y++) if (cost[x][y] == lx[x] + ly[y] && !T[y]){
				if (yx[y] == -1) break; T[y] = true; 
				q[wr++] = yx[y], add_to_tree(yx[y], x); }
			if (y < n) break; }
		if (y < n) break; 
		update_labels(), wr = rd = 0; 
		for (y = 0; y < n; y++) if (!T[y] && slack[y] == 0){
			if (yx[y] == -1){x = slackx[y]; break;}
			else{
				T[y] = true; 
				if (!S[yx[y]]) q[wr++] = yx[y], add_to_tree(yx[y], slackx[y]);
			}}
		if (y < n) break; }
	if (y < n){
		max_match++; 
		for (int cx = x, cy = y, ty; cx != -2; cx = prev2[cx], cy = ty)
			ty = xy[cx], yx[cy] = cx, xy[cx] = cy;
		augment(); }
}
Exemplo n.º 4
0
void multLL()
{
    int mod1 = 1.5e9;
    int mod2 = mod1 + 1;
    mult(mod1);
    forn(i, N) D[i] = C[i];
    mult(mod2);
    forn(i, N)
    {
        C[i] = D[i] + (C[i] - D[i] + (ll)mod2) * (ll)mod1 % mod2 * mod1;
    }
void BuildArray(){
	int ma = max(n, 256);
	forn(i, n)
		col[i] = s[i], p[i] = i;

	for (int k2 = 1; k2 / 2 < n; k2 *= 2){
		int k = k2 / 2;
		memset(num, 0, sizeof(num));
		forn(i, n)
			num[col[i] + 1]++;
		forn(i, ma)
			num[i + 1] += num[i];
		forn(i, n)
			p2[num[col[(p[i] - k + n) % n]]++] = (p[i] - k + n) % n;

		int cc = 0;
		forn(i, n){
			if (i && (col[p2[i]] != col[p2[i - 1]] ||
			col[(p2[i] + k) % n] != col[(p2[i - 1] + k) % n]))
				cc++;
			num[p2[i]] = cc;
		}
		forn(i, n)
			p[i] = p2[i], col[i] = num[i];
	}
 
	// make it stable
	memset(num, 0, sizeof(num));
	forn(i, n)
		num[col[i] + 1]++;
	forn(i, ma)
		num[i + 1] += num[i];
	forn(i, n)
		p2[num[col[i]]++] = i;
	forn(i, n)
		p[i] = p2[i];

	// calc inverse permutation
	forn(i, n)
		p2[p[i]] = i;
}
Exemplo n.º 6
0
void buildSA()
{
    forn(i, n) sa[i] = i;
    forn(i, n) bl[i] = s[i];
    nbl = *max_element(bl, bl+n) + 1;

    for (int d = 0; d < n; d = d ? d*2 : 1)
    {
        forn(i, n) nsa[i] = (sa[i]-d+n)%n;
        forn(i, nbl) cnt[i] = 0;
        forn(i, n) ++cnt[bl[sa[i]]];
        forn(i, nbl) if (i) cnt[i] += cnt[i-1];
        for (int i = n-1; i >= 0; --i) sa[--cnt[bl[nsa[i]]]] = nsa[i];

        nbl = -1;
        forn(i, n)
        {
            if (i == 0 || bl[sa[i]] != bl[sa[i-1]] || bl[(sa[i]+d)%n] != bl[(sa[i-1]+d)%n])
                ++nbl;
            pbl[sa[i]] = nbl; 
        }
        ++nbl;
        forn(i, n) bl[i] = pbl[i];
    }
}
int main() {
	int n;
	scanf("%d", &n);
	forn(i, n)
		scanf("%d%d", &p[i].x, &p[i].y), p[i].i = i;
	sort(p, p + n, xless);

	set <Pnt> s;
	int l = 0;
	forn(r, n){
		set<Pnt>::iterator it_r = s.lower_bound(p[r]), it_l = it_r;
		for (; it_r != s.end() && it_r->y - p[r].y < d; ++it_r)
			relax(*it_r, p[r]);
		while (it_l != s.begin() && p[r].y - (--it_l)->y < d)
			relax(*it_l, p[r]);
		s.insert(p[r]);
		while (l <= r && p[r].x - p[l].x >= d)
			s.erase(p[l++]);
	}
Exemplo n.º 8
0
Term &Term::operator=(const Term &other)
{
    if (this != &other)
    {
        type = other.type;
        id = other.id;
        amount_sub_terms = other.amount_sub_terms;
        if (other.sub_terms != NULL)
        {
            sub_terms = new Term[amount_sub_terms];
            forn(i, amount_sub_terms)
                sub_terms[i] = other.sub_terms[i];
        }
        else
        {
            sub_terms = NULL;
        }
    }

    return *this;
}
Exemplo n.º 9
0
//Mejor caso(O(N/M)): Si el alfabeto y el patron son grandes
//Peor caso(O(NM)): Como fuerza bruta, no es recomendable si no cumple el mejor caso
void BoyerMooreHorspool(const string &T, const string &P) {
  int N = T.size();
  int M = P.size();
  int sig[257];
  fill(sig, sig + 257, M);
  forn (i, M) sig[P[i]] = M - 1 - i;
  int i = M - 1;
  int j = M - 1;    
  while (i < N && j >= 0) {
    if (T[i] == P[j])
      i--, j--;
    else {
      i += sig[T[i]];
      j = M - 1;
    }
    if (j < 0) {
      cout << "Match : " << (i + 1) << endl;
      i += M + 1;
      j = M - 1;
    }
  }
}
Exemplo n.º 10
0
void init(int n) {
  p.resize(n);
  forn(i,n)
    p[i] = i;
}
Exemplo n.º 11
0
Arquivo: 3.cpp Projeto: nozdrenkov/sp
int diff(const string &a, const string &b) {
  int cnt = 0;
  forn(i, a.size()) cnt += int(a[i] != b[i]);
  return cnt;
}
Exemplo n.º 12
0
void process(const string &s) {
    sa_init();
    forn(i,si(s)) sa_extend(s[i]);
}
Exemplo n.º 13
0
tipo hungarian(){
	tipo ret = 0; max_match = 0, memset(xy, -1, sizeof(xy)); 
	memset(yx, -1, sizeof(yx)), init_labels(), augment(); //steps 1-3
	forn (x,n) ret += cost[x][xy[x]]; return ret;
}
Exemplo n.º 14
0
void init_labels(){
	zero(lx), zero(ly);
	forn (x,n) forn(y,n) lx[x] = max(lx[x], cost[x][y]);
}
 	void init(int n) {
		forn (i, n)
			rank[i] = 0, pr[i] = i;
 	}
Exemplo n.º 16
0
double area(vector<pto> &p){//O(sz(p))
	double area=0;
	forn(i, sz(p)) area+=p[i]^p[(i+1)%sz(p)];
	//if points are in clockwise order then area is negative
	return abs(area)/2;
}
	double simpson(double a, double b, int iterNumber) {
		double res = 0, h = (b - a) / iterNumber;
		forn (i, iterNumber + 1)
			res += f(a + h * i) * ((i == 0) || (i == iterNumber) ? 1 : ((i & 1) == 0) ? 2 : 4);
		return res * h / 3;
	}