Codeforces Round #368 (Div. 2)

A. Brain’s Photos

给你一张照片每个像素的颜色,问这是黑白照片还是彩色。。
‘C’ (cyan)
‘M’ (magenta)
‘Y’ (yellow)
‘W’ (white)
‘G’ (grey)
‘B’ (black)
注意G是灰色!!

#include 
#include 
#include 
#include 
#include 

using namespace std;

int n, m;
char c[3];
int main() {
    cin >> n >> m;
    bool flag = true;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            scanf("%s", c);
            if ((c[0] != 'B') && (c[0] != 'W') && (c[0] != 'G'))
                flag = false;
        }
    }
    if (flag) cout << "#Black&White" << endl;
    else cout << "#Color" << endl;
    return 0;
}

B. Bakery

一个无向图。有k个仓库,现在要选一个不是仓库的点开面包店,使它到最近的仓库距离最短。
枚举每一个仓库连的边。注意-1的情况。

#include 
#include 
#include 
#include 
#include 

using namespace std;

struct Edge{
    int to, v;
    Edge() {}
    Edge(int a, int b) : to(a), v(b) {}
};

int n, m, k;
vector edges[100005];
int sto[100005];
int vis[100005];
int main() {
    cin >> n >> m >> k;
    int a, b, c;
    for (int i = 0; i < m; i++) {
        scanf("%d %d %d", &a, &b, &c);
        edges[a].push_back(Edge(b, c));
        edges[b].push_back(Edge(a, c));
    }
    for (int i = 0; i < k; i++) {
        scanf("%d", &sto[i]);
        vis[sto[i]] = 1;
    }
    if (!k || k == n) {
        printf("-1\n");
        return 0;
    }
    int ans = 0x3f3f3f3f;
    for (int i = 0; i < k; i++) {
        int now = sto[i];
        for (int j = 0; j < edges[now].size(); j++) {
            Edge e = edges[now][j];
            if (!vis[e.to]) {
                ans = min(ans, e.v);
            }
        }
    }
    if (ans == 0x3f3f3f3f) ans = -1;
    printf("%d\n", ans);
    return 0;
}

C. Pythagorean Triples

给你直角三角形的一条边,问另外两条边,边长必须是整数。
勾股数

#include 
#include 
#include 
#include 
#include 

using namespace std;

long long k;
int main() {
    freopen("c.in", "r", stdin);
    cin >> k;
    if ((k == 1) || (k == 2)) cout << "-1" << endl;
    else {
        if (k & 1) {
            long long i = k - 1 >> 1;
            long long a = i * i * 2 + i * 2;
            long long b = i * i * 2 + i * 2 + 1;
            cout << a << ' ' << b << endl;
        } else {
            long long i = k >> 1;
            long long a = i * i - 1;
            long long b = i * i + 1;
            cout << a << ' ' << b << endl;
        }
    }
    return 0;
}

D. Persistent Bookcase

一个n×m的书架,维护4个操作:
* 1 i j 如果 (i,j) 没有书就放一本
* 2 i j 如果 (i,j) 有书就取掉
* 3 i 将第i行的状态翻转(有书拿掉,没书放上)
* 4 x: 回到第x个操作之后的状态

每一个操作之后输出书架上书的数目。

根据时间关系建一颗树,然后dfs。。。

#include 

using namespace std;

const int MAXN = 100005;

int n, m, q;
int que[MAXN][3];

int arr[1005][1005];
bool rev[1005];
int cnt[1005];
int num = 0;
int pre[MAXN];
bool vali[MAXN];

bool ins(int i, int j) {
    if (!(arr[i][j] ^ rev[i])) {
        arr[i][j] ^= 1;
        num++;
        cnt[i]++;
        return true;
    }
    return false;
}
bool del(int i, int j) {
    if (arr[i][j] ^ rev[i]) {
        arr[i][j] ^= 1;
        num--;
        cnt[i]--;
        return true;
    }
    return false;
}
bool rever(int i) {
    rev[i] ^= 1;
    num += m - cnt[i] * 2;
    cnt[i] = m - cnt[i];
    return true;
}

int ans[MAXN];
vector<int> edges[MAXN];

void dfs(int x, int pre) {
    if (que[x][0] == 1) vali[x] = ins(que[x][1], que[x][2]);
    else if (que[x][0] == 2) vali[x] = del(que[x][1], que[x][2]);
    else if (que[x][0] == 3) vali[x] = rever(que[x][1]);
    ans[x] = num;
    for (int i = 0; i < edges[x].size(); i++)
        if (edges[x][i] != pre) dfs(edges[x][i], x);
    if (vali[x]) {
        if (que[x][0] == 1) del(que[x][1], que[x][2]);
        else if (que[x][0] == 2) ins(que[x][1], que[x][2]);
        else if (que[x][0] == 3) rever(que[x][1]);
    }
}

int main() {
    freopen("d.in", "r", stdin);
    cin >> n >> m >> q;
    for (int i = 1; i <= q; i++) {
        cin >> que[i][0];
        if (que[i][0] < 3) cin >> que[i][1] >> que[i][2];
        else cin >> que[i][1];
        if (que[i][0] == 4) {
            edges[i].push_back(que[i][1]);
            edges[que[i][1]].push_back(i);
        } else {
            edges[i].push_back(i - 1);
            edges[i - 1].push_back(i);
        }
    }
    dfs(0, 0);
    for (int i = 1; i <= q; i++)
        cout << ans[i] << endl;
    return 0;
}

E. Garlands

太长懒得翻译。
首先子矩阵和用二维树状数组。
注意到ASK只有2000个,所以离线搞一搞,把每条链在每个ASK询问的子矩阵中的权值和存起来。然后每次询问的时候看那些链亮着加上就行了。

#include 

using namespace std;

const int MAXN = 2005;
const int MAXQ = 1000005;

int n, m, k, q;
long long c[MAXN][MAXN];
long long pos[MAXN][MAXN][3];
int len[MAXN];
char op[MAXQ][8];
int que[MAXQ][4];
vector<int> ask;
long long sum[MAXN][MAXN];

int lowbit(int x) {
    return x & (-x);
}
void add(int x, int y, int val) {
    for (int i = x; i < MAXN; i += lowbit(i))
        for (int j = y; j < MAXN; j += lowbit(j))
            c[i][j] += val;
}
long long getsum(int x, int y) {
    long long ans = 0;
    for (int i = x; i; i -= lowbit(i))
        for (int j = y; j; j -= lowbit(j))
            ans += c[i][j];
    return ans;
}

bool offf[MAXN];
map<int, int> idx;
int main() {
    scanf("%d %d %d", &n, &m, &k);
    for (int i = 1; i <= k; i++) {
        scanf("%d", &len[i]);
        for (int j = 1; j <= len[i]; j++) {
            scanf("%lld %lld %lld", &pos[i][j][0], &pos[i][j][1], &pos[i][j][2]);
        }
    }
    scanf("%d", &q);
    for (int i = 1; i <= q; i++) {
        scanf("%s", op[i]);
        if (op[i][0] == 'S') scanf("%d", &que[i][0]);
        else {
            scanf("%d %d %d %d", &que[i][0], &que[i][1], &que[i][2], &que[i][3]);
            ask.push_back(i);
            idx[i] = ask.size() - 1;
        }
    }
    for (int i = 1; i <= k; i++) {
        for (int j = 1; j <= len[i]; j++)
            add(pos[i][j][0], pos[i][j][1], pos[i][j][2]);
        for (int j = 0; j < ask.size(); j++) {
            sum[i][j] = getsum(que[ask[j]][2], que[ask[j]][3])
                - getsum(que[ask[j]][2], que[ask[j]][1] - 1)
                - getsum(que[ask[j]][0] - 1, que[ask[j]][3])
                + getsum(que[ask[j]][0] - 1, que[ask[j]][1] - 1);
        }
        for (int j = 1; j <= len[i]; j++)
            add(pos[i][j][0], pos[i][j][1], -pos[i][j][2]);
    }
    for (int i = 1; i <= q; i++) {
        if (op[i][0] == 'S')
            offf[que[i][0]] ^= 1;
        else {
            long long ans = 0;
            for (int j = 1; j <= k; j++) {
                if (offf[j]) continue;
                ans += sum[j][idx[i]];
            }
            cout << ans << endl;
        }
    }
    return 0;
}

你可能感兴趣的:(codeforces)