zoukankan      html  css  js  c++  java
  • BZOJ 2243 染色 (线段树+树链剖分)

    2243: [SDOI2011]染色

    Time Limit: 20 Sec  Memory Limit: 512 MB
    Submit: 9895  Solved: 3735
    [Submit][Status][Discuss]

    Description

    给定一棵有n个节点的无根树和m个操作,操作有2类:
    1、将节点a到节点b路径上所有点都染成颜色c;
    2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),
    如“112221”由3段组成:“11”、“222”和“1”。
    请你写一个程序依次完成这m个操作。

    Input

    第一行包含2个整数n和m,分别表示节点数和操作数;
    第二行包含n个正整数表示n个节点的初始颜色
    下面 行每行包含两个整数x和y,表示x和y之间有一条无向边。
    下面 行每行描述一个操作:
    “C a b c”表示这是一个染色操作,把节点a到节点b路径上所有点(包括a和b)都染成颜色c;
    “Q a b”表示这是一个询问操作,询问节点a到节点b(包括a和b)路径上的颜色段数量。

    Output

    对于每个询问操作,输出一行答案。

     

    Sample Input

    6 5
    2 2 1 2 1 1
    1 2
    1 3
    2 4
    2 5
    2 6
    Q 3 5
    C 2 1 1
    Q 3 5
    C 5 1 2
    Q 3 5

    Sample Output

    3
    1
    2

    HINT

    数N<=10^5,操作数M<=10^5,所有的颜色C为整数且在[0, 10^9]之间。

    Source

    析:真是一个好题,但是我TLE了两天,就是因为输入那个询问数,我当作边数了,结果就是一个TLE。。。

    大体思路,就是先进行用树链剖分,然后用线段树来维护,维护每个区间的不同数的个数,和每个数的值,在求的时候,在两个端点进行判断,是不是同一种,如果是就减去1,不是则不变。

    而且发现一个问题,就是网上的代码所以输出的和我的不一样,但是都AC了,不知道是不是数据水。也不知道谁的对。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 50;
    const int mod = 1000;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r > 0 && r <= n && c > 0 && c <= m;
    }
    
    struct Edge{
      int to, next;
    };
    Edge edge[maxn<<1];
    int head[maxn], tot, top[maxn], dep[maxn], a[maxn], fp[maxn];
    int son[maxn], fa[maxn], num[maxn], p[maxn], pos;
    int sum[maxn<<2], setv[maxn<<2], lx[maxn<<2], rx[maxn<<2], value[maxn<<2];
    
    void init(){
      tot = pos = 0;
      ms(head, -1);  ms(son, -1);
      ms(setv, -1);
    }
    
    void addEdge(int u, int v){
      edge[tot].to = v;
      edge[tot].next = head[u];
      head[u] = tot++;
    }
    
    void dfs1(int u, int f, int d){
      dep[u] = d;  fa[u] = f;
      num[u] = 1;
      for(int i = head[u]; ~i; i = edge[i].next){
        int v = edge[i].to;
        if(v == f)  continue;
        dfs1(v, u, d+1);
        num[u] += num[v];
        if(son[u] == -1 || num[son[u]] < num[v])  son[u] = v;
      }
    }
    
    void dfs2(int u, int sp){
      top[u] = sp;  p[u] = ++pos;
      fp[pos] = u;
      if(son[u] == -1)  return ;
      dfs2(son[u], sp);
      for(int i = head[u]; ~i; i = edge[i].next){
        int v = edge[i].to;
        if(v == son[u] || v == fa[u])  continue;
        dfs2(v, v);
      }
    }
    
    void push_up(int rt){
      int l = rt<<1, r = rt<<1|1;
      sum[rt] = sum[l] + sum[r];
      if(rx[l] == lx[r])  --sum[rt];
      lx[rt] = lx[l];  rx[rt] = rx[r];
    }
    
    void build(int l, int r, int rt){
      if(l == r){
        value[rt] = a[fp[l]];
        sum[rt] = 1;
        lx[rt] = rx[rt] = value[rt];
        return ;
      }
      int m = l + r >> 1;
      build(lson);
      build(rson);
      pu(rt);
    }
    
    void push_down(int rt){
      if(setv[rt] < 0)  return ;
      int l = rt<<1, r = rt<<1|1;
      sum[l] = sum[r] = 1;
      value[l] = value[r] = setv[rt];
      lx[l] = rx[l] = lx[r] = rx[r] = setv[rt];
      setv[l] = setv[r] = setv[rt];
      setv[rt] = -1;
    }
    
    void update(int L, int R, int val, int l, int r, int rt){
      if(L <= l && r <= R){
        sum[rt] = 1;
        value[rt] = setv[rt] = val;
        lx[rt] = rx[rt] = val;
        return ;
      }
      pd(rt);
      int m = l + r >> 1;
      if(L <= m)  update(L, R, val, lson);
      if(R > m)   update(L, R, val, rson);
      pu(rt);
    }
    
    int queryVal(int M, int l, int r, int rt){
      if(l == r)  return value[rt];
      pd(rt);
      int m = l + r >> 1;
      if(M <= m)  return queryVal(M, lson);
      return queryVal(M, rson);
    }
    
    int query(int L, int R, int l, int r, int rt){
      if(L <= l && r <= R)  return sum[rt];
      pd(rt);
      int m = l + r >> 1;
      if(L <= m && R > m){
        int ans = query(L, R, lson) + query(L, R, rson);
        if(rx[rt<<1] == lx[rt<<1|1])  --ans;
        return ans;
      }
      else if(L <= m)  return query(L, R, lson);
      else  return query(L, R, rson);
    }
    
    void update(int u, int v, int c){
      int f1 = top[u], f2 = top[v];
      while(f1 != f2){
        if(dep[f1] < dep[f2]){
          swap(f1, f2);
          swap(u, v);
        }
        update(p[f1], p[u], c, all);
        u = fa[f1];
        f1 = top[u];
      }
      if(dep[u] > dep[v])  swap(u, v);
      update(p[u], p[v], c, all);
    }
    
    int query(int u, int v){
      int f1 = top[u], f2 = top[v];
      int ans = 0;
      while(f1 != f2){
        if(dep[f1] < dep[f2]){
          swap(u, v);
          swap(f1, f2);
        }
        ans += query(p[f1], p[u], all);
        if(queryVal(p[f1], all) == queryVal(p[fa[f1]], all))  --ans;
        u = fa[f1];
        f1 = top[u];
      }
      if(dep[u] > dep[v])  swap(u, v);
      ans += query(p[u], p[v], all);
      return ans;
    }
    
    int main(){
      scanf("%d %d", &n, &m);
      init();
      for(int i = 1; i <= n; ++i)  scanf("%d", a+i);
      for(int i = 1; i < n; ++i){
        int u, v;
        scanf("%d %d", &u, &v);
        addEdge(u, v);
        addEdge(v, u);
      }
      dfs1(1, 0, 0);
      dfs2(1, 1);
      build(all);
      char op[5];
      while(m--){
        scanf("%s", op);
        int a, b, c;
        scanf("%d %d", &a, &b);
        if(op[0] == 'C'){
          scanf("%d", &c);
          update(a, b, c);
        }
        else  printf("%d
    ", query(a, b));
      }
      return 0;
    }
    

      

  • 相关阅读:
    调整心态,夯实基础
    js实现轮播图动画(更新"旋转木马")
    封装简单动画函数-由简到完善
    纯Css绘制三角形箭头三种方法
    JS实现图片''推拉门''效果
    一个基于 canvas 的画板
    Python 控制台进度条的实现
    Flask博客开发——Tinymce编辑器
    Flask博客开发——登录验证码
    用于水和水蒸汽物性计算的Python模块——iapws
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7481945.html
Copyright © 2011-2022 走看看