Exemplo n.º 1
0
 int dfs(int curr, vb& path, int len, int inv, Bit& bit) const
 {
     path[curr] = true;
     ++len;
     bit.add(V[curr], 1);
     inv += bit.rangeCount(V[curr] + 1, 1000);
     int ret = INT_MAX;
     do {
         if (K == len) {
             ret = inv;
             break;
         }
         if (K < len)
             break;
         for (int i = 0; i < G[curr].size(); ++i) {
             int node = G[curr][i];
             if (!path[node]) {
                 ret = min(ret, dfs(node, path, len, inv, bit));
             }
         }
     } while (false);
     path[curr] = false;
     bit.add(V[curr], -1);
     return ret;
 }
Exemplo n.º 2
0
int solve()
{
    sort(sticks, sticks + N);
    int hi = sticks[N - 1];

    int ans = 0;
    tree.init(sticks[N - 1]);

    for (int i = N - 4; i >= 2; --i) {
        int a = i + 1;
        for (int b = a + 1; b < N; ++b)
            for (int c = b + 1; c < N; ++c) {
                int v = max(1, sticks[c] - (sticks[a] + sticks[b]));
                tree.add(v, 1);
            }

        for (int j = i - 1; j >= 1; --j)
            for (int k = j - 1; k >= 0; --k) {
                int v = min(hi, sticks[i] + sticks[j] + sticks[k]);
                ans += tree.query(v - 1);
            }
    }

    return ans;
}
Exemplo n.º 3
0
int HuffmanDecoder::decode(IOReader& reader, IOWriter& writer, BitCode codes[], size_t codeCnt, size_t excessBit) {
	const size_t fileSize = reader.size() - sizeof(HuffmanFileHeader);

	size_t firstCode = 0;
	while (firstCode < codeCnt && codes[firstCode].bit.len == 0)
		++firstCode;
	if (firstCode >= codeCnt)
		return 0;

	const size_t minCodeLength = codes[firstCode].bit.len;

	const __int64 availableBits = excessBit ? ((fileSize -1 ) * 8 + excessBit) : fileSize * 8;
	__int64 consumeBits = 0;

	while(consumeBits < availableBits) {
		size_t codeLength = minCodeLength;
		Bit data = reader.readBits(codeLength);
		if (data.len != codeLength)
			return 1;

		consumeBits += data.len;

		bool find = false;

		for (size_t i = firstCode; i< codeCnt; ++i ) {
			const Bit& bit = codes[i].bit;
			if (bit.len > codeLength) {
				Bit appendBits = reader.readBits( bit.len - codeLength);

				if (appendBits.len != bit.len - codeLength)
					return 1;

				consumeBits += appendBits.len;

				data.add(appendBits);

				codeLength = bit.len;
			}			

			if (bit.bits == data.bits) {
				writer.write(&(codes[i].value), 1);
				find = true;
				break;
			}
		}

		if (!find )
			return 2;
	};

	return 0;	
}
Exemplo n.º 4
0
int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &N, &M);

        bit.init(N + M);

        for (int i = 1; i <= N; i++) {
            bit.add(i, 1);
            no[i] = N + 1 - i;
        }

        for (int i = 1; i <= M; i++) {
            scanf("%d", &A[i]);
            printf("%d%c", N - bit.sum(no[A[i]]), i < M ? ' ' : '\n');

            bit.add(no[A[i]], -1);
            no[A[i]] = N + i;
            bit.add(no[A[i]], 1);
        }
    }

    return 0;
}
Exemplo n.º 5
0
Arquivo: j.cpp Projeto: M4573R/pc-code
int main()
{
	int T;
	scanf("%d", &T);

	int ncase = 0;
	while (T--) {
		scanf("%d", &N);
		tree.init(N);
		for (int i = 1; i <= N; ++i) {
			int x;
			scanf("%d", &x);
			tree.add(i, x);
		}

		printf("Case %d:\n", ++ncase);
		scanf("%d", &M);
		while (M--) {
			int cmd;
			scanf("%d", &cmd);
			if (cmd == 1) {
				int x, y;
				scanf("%d%d", &x, &y);
				printf("%d\n", tree.query(x, y));
			}
			else if (cmd == 2) {
				int x, y, p;
				scanf("%d%d%d", &x, &y, &p);
				tree.add(x, -p);
				tree.add(y, p);
			}
		}
	}

	return 0;
}
int main(){
   // freopen("input.txt", "r", stdin);
    Node lis[SZ];
    int n;
    int test, val, pos,  tc = 1;
    scanf("%d", &test);
    while( test-- ){
        scanf("%d", &n);
        for(int i = 1; i <= n; i++){
            scanf("%d", &val);
            lis[i] = Node(val, i);
        }
        sort(lis+1, lis+n+1);

        Bit bt = Bit(n);
        for(int idx = 1; idx <= n; idx++){
            pos = lis[idx].pos;
            bt.add(pos, 1 + bt.query(pos-1));
        }
        printf("Case %d: %lld\n", tc++, bt.query(n));
    }

    return 0;
}