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

  • 相关阅读:
    Linux系统组成及初识
    Linux基础入门
    计算机和操作系统发展历史
    Swift,Objective-C,C,C++混合编程
    Objective-C数组和字典
    Java生成随机数字和字母组合10位数
    注册和登录
    IDEA的开发
    登录时@RequestBody报的错
    Java过滤特殊字符和表情
  • 原文地址:https://www.cnblogs.com/pk28/p/4854731.html
Copyright © 2011-2022 走看看