浴谷八连测#2

A. string

给定两个字符串s,t,其中s只包含小写字母以及*,t只包含小写字母。
s中的每个*都可修改为任意多个(可以是0个)它的前一个字符。问是否能将s修改为t。

B. or

构造一个长度为\(n\)的非负整数序列\(\{x_i\}\),满足\(m\)个条件,第\(i\)个条件为\(x[l_i]|x[l_{i+1}]|…|x[r_i]=p_i\)
输入 第一行两个整数\(n,m\)。接下来m行每行三个整数\(l_i,r_i,p_i\)
输出 若存在,第一行输出Yes,第二行输出\(n\)个整数表示\(x[1]…x[n]\),否则输出一行No。本题使用Special Judge。
数据范围 \(n,m≤10^5, 1≤l_i≤r_i≤n, 0≤p_i≤2^{30}\)

#include <cstdio>
using namespace std;
struct TreeNode {
  int lb, rb, mid;
  int val;
  TreeNode *lc, *rc;
  TreeNode(int new_lb, int new_rb) :
    lb(new_lb), rb(new_rb), mid(new_lb + new_rb >> 1), 
    val(0x7fffffff), lc(NULL), rc(NULL) {}
};
int l[100000], r[100000], p[100000];
TreeNode* build_tree(int lb, int rb) {
  TreeNode *new_node = new TreeNode(lb, rb);
  if (lb < rb) {
    new_node->lc = build_tree(lb, new_node->mid);
    new_node->rc = build_tree(new_node->mid + 1, rb);
  }
  return new_node;
}
int lb, rb, x;
void modify(TreeNode *node) {
  if (lb <= node->lb && node->rb <= rb) {
    node->val &= x;
    return;
  }
  if (lb <= node->mid)
    modify(node->lc);
  if (node->mid < rb)
    modify(node->rc);
}
void sink(TreeNode *node) {
  if (node->lb == node->rb)
    return;
  (node->lc)->val &= node->val;
  sink(node->lc);
  (node->rc)->val &= node->val;
  sink(node->rc);
  node->val = (node->lc)->val | (node->rc)->val;
}
int query(TreeNode *node) {
  if (lb <= node->lb && node->rb <= rb)
    return node->val;
  int ans = 0;
  if (lb <= node->mid)
    ans |= query(node->lc);
  if (node->mid < rb)
    ans |= query(node->rc);
  return ans;
}
//↓用指针写的恶果↓ 
int leaf_value(TreeNode *node) {
  if (node->lb == node->rb)
    return node->val;
  return leaf_value(lb <= node->mid ? node->lc : node->rc);
}
int main() {
  freopen("or.in", "r", stdin);
  freopen("or.out", "w", stdout);
  int n, m;
  scanf("%d%d", &n, &m);
  TreeNode *root = build_tree(1, n);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", l + i, r + i, p + i);
    lb = l[i]; rb = r[i]; x = p[i];
    modify(root);
  }
  sink(root);
  for (int i = 0; i < m; i++) {
    lb = l[i]; rb = r[i];
    if (query(root) != p[i]) {
      printf("No\n");
      return 0;
    }
  }
  printf("Yes\n");
  for (int i = 1; i <= n; i++) {
    lb = i;
    printf("%d ", leaf_value(root));
  }
  return 0;
}
posted @ 2017-10-18 21:53  Planet6174  阅读(263)  评论(0编辑  收藏  举报