zoukankan      html  css  js  c++  java
  • usaco2008 nov 区间异或求和

    Problem 11: Switching Lights [LongFan, 2008]
    
    Farmer John tries to keep the cows sharp by letting them play with
    intellectual toys. One of the larger toys is the lights in the barn.
    Each of the N (2 <= N <= 500) cow stalls conveniently numbered
    1..N has a colorful light above it.
    
    At the beginning of the evening, all the lights are off. The cows
    control the lights with a set of N pushbutton switches that toggle
    the lights; pushing switch i changes the state of light i from off
    to on or from on to off.
    
    The cows read and execute a list of M (1 <= M <= 2,000) operations
    expressed as one of two integers (0 <= operation <= 1).
    
    The first operation (denoted by a 0 command) includes two subsequent
    integers S_i and E_i (1 <= S_i <= E_i <= N) that indicate a starting
    switch and ending switch. They execute the operation by pushing
    each pushbutton from S_i through E_i inclusive exactly once.
    
    The second operation (denoted by a 1 command) asks the cows to count
    how many lights are on in the range given by two integers S_i and
    E_i (1 <= S_i <= E_i <= N) which specify the inclusive range in
    which the cows should count the number of lights that are on.
    
    Help FJ ensure the cows are getting the correct answer by processing
    the list and producing the proper counts.
    
    PROBLEM NAME: swtch
    
    INPUT FORMAT:
    
    * Line 1: Two space-separated integers: N and M
    
    * Lines 2..M+1: Each line represents an operation with three
            space-separated integers: operation, S_i, and E_i
    
    SAMPLE INPUT (file swtch.in):
    
    4 5
    0 1 2
    0 2 4
    1 2 3
    0 2 4
    1 1 4
    
    INPUT DETAILS:
    
    Four lights; five commands. Here is the sequence that should
    be processed:
            Lights
                1 2 3 4
      Init:     O O O O   O = off  * = on
      0 1 2 ->  * * O O  toggle lights 1 and 2
      0 2 4 ->  * O * *
      1 2 3 ->  1        count the number of lit lights in range 2..3
      0 2 4 ->  * * O O  toggle lights 2, 3, and 4
      1 1 4 ->  2        count the number of lit lights in the range 1..4
    
    OUTPUT FORMAT:
    
    * Lines 1..number of queries: For each output query, print the count
            as an integer by itself on a single line.
    
    SAMPLE OUTPUT (file swtch.out):
    
    1
    2
    就是一开始所有的灯是灭着的,然后....

    可以用线段树解决

    /* ***********************************************
    Author        :guanjun
    Created Time  :2015/10/4 15:30:49
    File Name     :1.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 100000+10
    #define cle(a) memset(a,0,sizeof(a))
    #define ls i<<1
    #define rs i<<1|1
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    struct node{
        int l,r,c;
        int sum;
        int dist(){
            return r-l+1;
        }
    }nod[maxn*4];
    void push_up(int i){
        nod[i].sum=nod[ls].sum+nod[rs].sum;
    }
    void push_down(int i){
        if(nod[i].c){
            nod[ls].c^=1;
            nod[rs].c^=1;
            nod[ls].sum=nod[ls].dist()-nod[ls].sum;
            nod[rs].sum=nod[rs].dist()-nod[rs].sum;
            nod[i].c=0;
        }
    }
    void build(int i,int l,int r){
        nod[i].l=l;
        nod[i].r=r;
        nod[i].c=nod[i].sum=0;
        if(l==r){
            return ;
        }
        int mid=(l+r)/2;
        build(ls,l,mid);
        build(rs,mid+1,r);
        push_up(i);
    }
    void update(int i,int l,int r){
        if(nod[i].l==l&&nod[i].r==r){
            nod[i].c^=1;
            nod[i].sum=nod[i].dist()-nod[i].sum;
            return ;
        }
        push_down(i);
        int mid=(nod[i].l+nod[i].r)/2;
        if(r<=mid)update(ls,l,r);
        else if(l>mid)update(rs,l,r);
        else {
            update(ls,l,mid);
            update(rs,mid+1,r);
        }
        push_up(i);
    }
    int query(int i,int l,int r){
        if(nod[i].l==l&&nod[i].r==r){
            return nod[i].sum;
        }
        push_down(i);
        int mid=(nod[i].l+nod[i].r)/2;
        //int sum=0;
        if(r<=mid)return query(ls,l,r);
        else if(l>mid)return query(rs,l,r);
        else return query(ls,l,mid)+query(rs,mid+1,r);
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int n,m,po,x,y;
        while(cin>>n>>m){
            build(1,1,n);
            for(int j=1;j<=m;j++){
                scanf("%d%d%d",&po,&x,&y);
                if(po==0)update(1,x,y);
                else printf("%d
    ",query(1,x,y));
            }
        }
        return 0;
    }

     数据 http://contest.usaco.org/TESTDATA/NOV08_1.htm

  • 相关阅读:
    巨蟒django之权限9:前端展示修改删除合并&&权限展示
    巨蟒django之权限8:排序&&菜单展开权限归属
    巨蟒django之权限7:动态生成一级&&二级菜单
    巨蟒django之权限6: 权限控制表设计&&登录权限
    巨蟒django之CRM5 学习记录&&课程记录&&班级管理&&私户的数量上限
    巨蟒django之CRM4 一些小功能
    巨蟒django之CRM3 添加和编辑客户&&公户和私户的展示和转换
    巨蟒django之CRM2 展示客户列表&&分页
    巨蟒django之CRM1 需求分析&&表结构设计&&注册登录验证
    sysfs接口函数到建立_DEVICE_ATTR
  • 原文地址:https://www.cnblogs.com/pk28/p/4854731.html
Copyright © 2011-2022 走看看