码迷,mamicode.com
首页 > 其他好文 > 详细

lightoj1080 线段树

时间:2014-09-21 00:02:49      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   div   art   

bubuko.com,布布扣
  1 //Accepted    6628 KB    520 ms
  2 //I a b   把a到b区间的二进制位去反,转化成a到b区间的数全部加1
  3 //Q a     判断第a位的奇偶
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <iostream>
  7 #include <queue>
  8 #include <cmath>
  9 #include <algorithm>
 10 using namespace std;
 11 /**
 12   * This is a documentation comment block
 13   * 如果有一天你坚持不下去了,就想想你为什么走到这儿!
 14   * @authr songt
 15   */
 16 const int imax_n = 100005;
 17 struct node
 18 {
 19     int l,r;
 20     int add;
 21     int t;
 22 }f[imax_n*3];
 23 string s;
 24 void build(int t,int l,int r)
 25 {
 26     f[t].l=l;
 27     f[t].r=r;
 28     f[t].add=0;
 29     if (l==r)
 30     {
 31         f[t].t=s[l-1]-0;
 32         return ;
 33     }
 34     int mid=(l+r)/2;
 35     build(2*t,l,mid);
 36     build(2*t+1,mid+1,r);
 37     f[t].t=f[2*t].t+f[2*t+1].t;
 38 }
 39 void update(int t,int l,int r,int c)
 40 {
 41     if (f[t].l==l && f[t].r==r)
 42     {
 43         f[t].add+=c;
 44         return ;
 45     }
 46     f[t].t+=(r-l+1)*c;
 47     int mid=(f[t].l+f[t].r)/2;
 48     if (r<=mid) update(2*t,l,r,c);
 49     else
 50     {
 51         if (l>mid) update(2*t+1,l,r,c);
 52         else
 53         {
 54             update(2*t,l,mid,c);
 55             update(2*t+1,mid+1,r,c);
 56         }
 57     }
 58 }
 59 int query(int t,int l,int r)
 60 {
 61     if (f[t].l==l && f[t].r==r)
 62     {
 63         return f[t].t+f[t].add*(r-l+1);
 64     }
 65     int mid=(f[t].l+f[t].r)/2;
 66     f[t].t+=(r-l+1)*f[t].add;
 67     f[2*t].add+=f[t].add;
 68     f[2*t+1].add+=f[t].add;
 69     f[t].add=0;
 70     if (r<=mid) return query(2*t,l,r);
 71     else
 72     {
 73         if (l>mid) return query(2*t+1,l,r);
 74         else
 75         {
 76             return query(2*t,l,mid)+query(2*t+1,mid+1,r);
 77         }
 78     }
 79 }
 80 char sq[5];
 81 int Q;
 82 int x,y;
 83 void slove()
 84 {
 85     int len=s.length();
 86     build(1,1,len);
 87     scanf("%d",&Q);
 88     while (Q--)
 89     {
 90         scanf("%s",sq);
 91         if (sq[0]==I)
 92         {
 93             scanf("%d%d",&x,&y);
 94             update(1,x,y,1);
 95         }
 96         else
 97         {
 98             scanf("%d",&x);
 99             int ans=query(1,x,x);
100             printf("%d\n",ans%2);
101         }
102     }
103 }
104 int main()
105 {
106     int T;
107     scanf("%d",&T);
108     int t=0;
109     while (T--)
110     {
111         cin>>s;
112         printf("Case %d:\n",++t);
113         slove();
114     }
115     return 0;
116 }
View Code

 

lightoj1080 线段树

标签:style   blog   http   color   io   os   ar   div   art   

原文地址:http://www.cnblogs.com/djingjing/p/3983680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!