zoukankan      html  css  js  c++  java
  • P3870 [TJOI2009]开关

    链接:Miku

    --------------------------------

    凡天下之事,但知其一不知其二者多矣,可据理臆断欤?

    ------------------------------

    我就是但知其一呜呜呜

    -----------------------------

    %%%并感谢yyq大佬

    --------------------------------

    很显然可以用线段树操作

    --------------------------------

    这次的lazy指的是这个区间需不需要被反转,然后显然,一个区间反转后亮的灯泡的值就是区间灯泡总量-原来亮的值

    -----------------------------------

    所以写出来了个这个

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int n,m;
    long long sum[400005], lazy[400005];
    int f,x,y;
    long long k;
    void pushdown(int x, int L, int R){
        if (lazy[x] != 0){
            int mid = (L + R) >> 1;
            lazy[x << 1] =1;
            lazy[x << 1 | 1] = 1;
            sum[x << 1] =  (mid - L + 1)-sum[x<<1];
            sum[x << 1 | 1] =  (R - mid)-sum[x<<1|1];
            lazy[x] = 0;
        }
        return;
    }
    void pushup(int x){
        sum[x] = sum[x << 1] + sum[x << 1 | 1];
        return;
    }
    void update(int x, int l, int r, int L, int R){
        if (L <= l && r <= R){
            lazy[x] = 1;
            sum[x] = r - l + 1 - sum[x];
            return;
        }
        int mid = (l + r) >> 1;
        pushdown(x, l, r);
        if (L <= mid) update(x << 1, l, mid, L, R);
        if (R > mid) update(x << 1 | 1, mid + 1, r, L, R);
        pushup(x);
    }
    long long query(int x, int l, int r, int L, int R){
        if (L <= l && r <= R){
            return sum[x];
        }
        int mid = (l + r) >> 1;
        pushdown(x, l, r);
        long long ans = 0;
        if (L <= mid) ans += query(x << 1, l, mid, L, R);
        if (R > mid) ans += query(x << 1 | 1, mid + 1, r, L, R);
        return ans;
    }
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;++i){
            scanf("%d",&f);
            if(f==0){
                scanf("%d%d",&x,&y);
                update(1, 1, n, x, y);
            }
            else{
                scanf("%d%d",&x,&y);
                printf("%lld
    ", query(1, 1, n, x, y));
            }
        }
        return 0;
    }
    wa

    --------------------------

    它过样例了,提交,10分

    -----------------------------

    ??????????????

    ----------------------------

    问题出在lazy上

    但是如果一个区间被反转了偶数次,其实就相当于没动过,所以我们在处理懒标记的时候需要特判

    ----------------------------------

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int n,m;
    long long sum[400005], lazy[400005];
    int f,x,y;
    long long k;
    void pushdown(int x, int L, int R){
        if (lazy[x] != 0){
            int mid = (L + R) >> 1;
            lazy[x << 1] =(lazy[x<<1]+1)%2;
            lazy[x << 1 | 1] = (lazy[x<<1|1]+1)%2;
            sum[x << 1] =  (mid - L + 1)-sum[x<<1];
            sum[x << 1 | 1] =  (R - mid)-sum[x<<1|1];
            lazy[x] = 0;
        }
        return;
    }
    void pushup(int x){
        sum[x] = sum[x << 1] + sum[x << 1 | 1];
        return;
    }
    void update(int x, int l, int r, int L, int R){
        if (L <= l && r <= R){
            lazy[x] = (lazy[x]+1)%2;
            sum[x] = r - l + 1 - sum[x];
            return;
        }
        int mid = (l + r) >> 1;
        pushdown(x, l, r);
        if (L <= mid) update(x << 1, l, mid, L, R);
        if (R > mid) update(x << 1 | 1, mid + 1, r, L, R);
        pushup(x);
    }
    long long query(int x, int l, int r, int L, int R){
        if (L <= l && r <= R){
            return sum[x];
        }
        int mid = (l + r) >> 1;
        pushdown(x, l, r);
        long long ans = 0;
        if (L <= mid) ans += query(x << 1, l, mid, L, R);
        if (R > mid) ans += query(x << 1 | 1, mid + 1, r, L, R);
        return ans;
    }
    int main(){
        scanf("%d% d",&n,&m);
        for(int i=1;i<=m;++i){
            scanf("%d",&f);
            if(f==0){
                scanf("%d%d",&x,&y);
                update(1, 1, n, x, y);
            }
            else{
                scanf("%d%d",&x,&y);
                printf("%lld
    ", query(1, 1, n, x, y));
            }
        }
        return 0;
    }
    Ac'
  • 相关阅读:
    SpringBoot之采用AOP统一打印日志信息
    SpringBoot之SpringBoot整合log4j
    SpringBoot之SpringBoot整合logback
    SpringBoot之SpringBoot整合多环境不同配置文件
    SpringBoot之SpringBoot整合devtools热部署
    [bug] Flask:jinja2.exceptions.UndefinedError: 'None' has no attribute 'id'
    [bug] Python:“TabError: inconsistent use of tabs and spaces in indentation”
    [bug] MySQL 无法删除表
    [bug] Flask css 不更新
    [bug] flink on yarn 启动失败
  • 原文地址:https://www.cnblogs.com/For-Miku/p/12358774.html
Copyright © 2011-2022 走看看