BZOJ 2243

2243: [SDOI2011]染色

树链剖分+线段树
题意是多少段,不是多少种。
所以维护好left color和right color就好了

BZOJ 2243

/**************************************************************
    Problem: 2243
    User: Dream_Tonight
    Language: C++
    Result: Accepted
    Time:6012 ms
    Memory:17404 kb
****************************************************************/
 
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
 
using namespace std;
#define ls rt << 1
#define rs rt << 1 | 1
#define lson l , mid , ls
#define rson mid + 1 , r , rs
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5 + 7;
const int inf = 2139062143;
const int mod = 10007;
 
int fa[maxn], son[maxn], sz[maxn], rk[maxn];
int top[maxn], head[maxn], deep[maxn], id[maxn];
int color[maxn], sum[maxn << 2], lc[maxn << 2], rc[maxn << 2], tag[maxn << 2];
int cnt = 0, ret = 0;
int a, b, c, LC, RC;
char ch;
 
struct node {
    int to, next;
} edge[maxn << 1];
 
void add(int u, int v) {
    edge[++ret] = (node) {v, head[u]}, head[u] = ret;
    edge[++ret] = (node) {u, head[v]}, head[v] = ret;
}
 
void dfs1(int x, int f) {
    sz[x] = 1, fa[x] = f, deep[x] = deep[f] + 1, son[x] = 0;
    for (int i = head[x]; i; i = edge[i].next) {
        int v = edge[i].to;
        if (v == f) continue;
        dfs1(v, x);
        if (sz[son[x]] < sz[v]) son[x] = v;
        sz[x] += sz[v];
    }
}
 
void dfs2(int x, int tp) {
    top[x] = tp, id[x] = ++cnt, rk[cnt] = color[x];
    if (son[x]) dfs2(son[x], tp);
    for (int i = head[x]; i; i = edge[i].next) {
        int v = edge[i].to;
        if (v == son[x] || v == fa[x]) continue;
        dfs2(v, v);
    }
}
 
void push_up(int rt) {
    sum[rt] = sum[ls] + sum[rs];
    if (rc[ls] == lc[rs]) sum[rt]--;
    lc[rt] = lc[ls], rc[rt] = rc[rs];
}
 
void push_down(int rt) {
    if (tag[rt] == -1) return;
    sum[ls] = sum[rs] = 1;
    lc[ls] = rc[ls] = tag[rt];
    lc[rs] = rc[rs] = tag[rt];
    tag[ls] = tag[rs] = tag[rt];
    tag[rt] = -1;
}
 
void build(int l, int r, int rt) {
    if (l == r) {
        sum[rt] = 1;
        lc[rt] = rc[rt] = rk[l];
        return;
    }
    int mid = (l + r) / 2;
    build(lson);
    build(rson);
    push_up(rt);
}
 
int query(int l, int r, int rt, int L, int R) {
    if (L <= l && r <= R) {
        if (l == L) LC = lc[rt];
        if (r == R) RC = rc[rt];
        return sum[rt];
    }
    push_down(rt);
    int mid = (l + r) / 2, res = 0;
    if (mid >= L) res += query(lson, L, R);
    if (mid < R) res += query(rson, L, R);
    if (L <= mid && mid < R) res -= rc[ls] == lc[rs];
    push_up(rt);
    return res;
}
 
void update(int l, int r, int rt, int L, int R, int V) {
    if (L <= l && r <= R) {
        tag[rt] = lc[rt] = rc[rt] = V;
        sum[rt] = 1;
        return;
    }
    push_down(rt);
    int mid = (l + r) / 2;
    if (mid >= L) update(lson, L, R, V);
    if (mid < R) update(rson, L, R, V);
    push_up(rt);
}
 
void update_range(int l, int r, int col, int n) {
    while (top[l] != top[r]) {
        if (deep[top[l]] < deep[top[r]]) swap(l, r);
        update(1, n, 1, id[top[l]], id[l], col);
        l = fa[top[l]];
    }
    if (deep[l] > deep[r]) swap(l, r);
    update(1, n, 1, id[l], id[r], col);
}
 
int query_range(int l, int r, int n) {
    int res = 0, ll = -1, rr = -1;
    while (top[l] != top[r]) {
        if (deep[top[l]] < deep[top[r]]) swap(l, r), swap(ll, rr);
        res += query(1, n, 1, id[top[l]], id[l]);
        if (RC == ll) res--;
        ll = LC;
        l = fa[top[l]];
    }
    if (deep[l] > deep[r]) swap(l, r), swap(ll, rr);
    res += query(1, n, 1, id[l], id[r]);
    if (LC == ll) res--;
    if (RC == rr) res--;
    return res;
}
 
int main() {
#ifndef ONLINE_JUDGE
    freopen("weather.in", "r", stdin);
#endif
    int x, y, n, m;
    scanf("%d%d", &n, &m);
    memset(tag, -1, sizeof(tag));
    for (int i = 1; i <= n; i++)scanf("%d", &color[i]);
    for (int i = 1; i < n; i++) {
        scanf("%d%d", &x, &y);
        add(x, y);
    }
    dfs1(1, 0), dfs2(1, 0);
    build(1, n, 1);
    for (int i = 1; i <= m; i++) {
        scanf(" %c", &ch);
        if (ch == 'Q') {
            scanf("%d%d", &a, &b);
            printf("%d\n", query_range(a, b, n));
        } else {
            scanf("%d%d%d", &a, &b, &c);
            update_range(a, b, c, n);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BZOJ 2908 题目是一个数据下载任务。这个任务要求下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要求。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值