zoukankan      html  css  js  c++  java
  • hdu 4046 2011北京赛区网络赛G 线段树 ***

    还带这么做的,卧槽,15分钟就被A了的题,居然没搞出来

    若某位是1,则前两个为wb,这位就是w

      1 #include<cstdio>
      2 #include<cstring>
      3 #define lson l,m,rt<<1
      4 #define rson m+1,r,rt<<1|1
      5 using namespace std;
      6 const int maxn=50010;
      7 int n,m,sum[maxn<<2];
      8 char str[maxn];
      9 int num[maxn];
     10 void pushup(int rt)
     11 {
     12     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
     13 }
     14 void build(int l,int r,int rt)
     15 {
     16     if(l==r)
     17     {
     18         sum[rt]=num[l];
     19         return;
     20     }
     21     int m=(l+r)>>1;
     22     build(lson);
     23     build(rson);
     24     pushup(rt);
     25 }
     26 int query(int L,int R,int l,int r,int rt)
     27 {
     28     if(L<=l&&r<=R)
     29     {
     30         return sum[rt];
     31     }
     32     int m=(l+r)>>1;
     33     int ans=0;
     34     if(L<=m) ans+=query(L,R,lson);
     35     if(R>m)  ans+=query(L,R,rson);
     36     return ans;
     37 }
     38 void update(int p,int val,int l,int r,int rt)
     39 {
     40     if(l==r)
     41     {
     42         sum[rt]=val;
     43         return;
     44     }
     45     int m=(l+r)>>1;
     46     if(p<=m) update(p,val,lson);
     47     else      update(p,val,rson);
     48     pushup(rt);
     49 }
     50 int main()
     51 {
     52     int t,T;
     53     scanf("%d",&T);
     54     for(int t=1;t<=T;t++)
     55     {
     56         printf("Case %d:
    ",t);
     57         scanf("%d%d%s",&n,&m,str+1);
     58         memset(num,0,sizeof(num));
     59         for(int i=3;i<=n;i++)
     60             if(str[i-2]=='w'&&str[i-1]=='b'&&str[i]=='w')  num[i]=1;
     61         build(1,n,1);
     62         int k,a,b;
     63         char ch[5];
     64         while (m--)
     65         {
     66             scanf("%d",&k);
     67             if(k==0)
     68             {
     69                 scanf("%d%d",&a,&b);
     70                 a++;b++;   //字符串从1开始,所以下标都加1
     71                 if(b-a<2)  printf("0
    ");
     72                 else       printf("%d
    ",query(a+2,b,1,n,1));
     73             }
     74             else
     75             {
     76                 scanf("%d%s",&a,ch);
     77                 a++;
     78                 if(ch[0]==str[a]) continue;//修改的和以前一样,这不用任何操作
     79                 if(a>=3)
     80                 {
     81                     if(str[a-2]=='w'&&str[a-1]=='b'&&str[a]=='w')
     82                         update(a,0,1,n,1);
     83                     if(str[a-2]=='w'&&str[a-1]=='b'&&str[a]=='b')
     84                         update(a,1,1,n,1);
     85                 }
     86                 if(a>=2&&a+1<=n)
     87                 {
     88                     if(str[a-1]=='w'&&str[a]=='b'&&str[a+1]=='w')
     89                         update(a+1,0,1,n,1);
     90                     if(str[a-1]=='w'&&str[a]=='w'&&str[a+1]=='w')
     91                         update(a+1,1,1,n,1);
     92                 }
     93                 if(a+2<=n)
     94                 {
     95                     if(str[a]=='w'&&str[a+1]=='b'&&str[a+2]=='w')
     96                         update(a+2,0,1,n,1);
     97                     if(str[a]=='b'&&str[a+1]=='b'&&str[a+2]=='w')
     98                         update(a+2,1,1,n,1);
     99                 }
    100                 str[a]=ch[0];
    101             }
    102         }
    103     }
    104     return 0;
    105 }
      1 #include<cstdio>
      2 #include<iostream>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<queue>
      7 #define lson l,mid,rt<<1
      8 #define rson mid+1,r,rt<<1|1
      9 #define root 1,n,1
     10 #define mid ((l+r)>>1)
     11 #define ll long long
     12 #define cl(a) memset(a,0,sizeof(a))
     13 #define ts printf("*****
    ");
     14 using namespace std;
     15 const int MAXN=50010;
     16 int sum[MAXN<<2],lsum[MAXN<<2],rsum[MAXN<<2],lc[MAXN<<2],rc[MAXN<<2];
     17 int n,m,tt;
     18 char s[MAXN];
     19 void pushup(int l,int r,int rt)
     20 {
     21     //printf("%d %d %d %d
    ",l,r,rt,mid);
     22     /*if(mid==3)
     23     {
     24         printf("%s
    ",s+1);
     25         printf("%c %c %c
    ",s[mid-1],s[mid],s[mid+1]);
     26     }*/
     27     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
     28     if(/*(s[mid]=='b'&&s[mid-1]=='w'&&s[mid+1]=='w')||*/(s[mid]=='w'&&s[mid+1]=='b'&&s[mid+2]=='w')&&mid+1<=n)
     29     {
     30         sum[rt]+=1;
     31     }
     32 }
     33 void build(int l,int r,int rt)
     34 {
     35     if(l==r)
     36     {
     37         sum[rt]=0;
     38         return;
     39     }
     40     build(lson);
     41     build(rson);
     42     pushup(l,r,rt);
     43 }
     44 void update(int pos,int val,int l,int r,int rt)
     45 {
     46     if(l==r)
     47     {
     48         return;
     49     }
     50     update(pos,val,lson);
     51     update(pos,val,rson);
     52     pushup(l,r,rt);
     53 }
     54 int query(int L,int R,int l,int r,int rt)
     55 {
     56 
     57     //printf("%d %d %d %d %d
    ",L,R,l,r,rt);
     58     if(L<=l&&r<=R)
     59     {
     60         return sum[rt];
     61     }
     62     if(R<=mid) return query(L,R,lson);
     63     if(L>mid) return query(L,R,rson);
     64 
     65     int ta,tb;
     66     ta=query(L,R,lson);
     67     tb=query(L,R,rson);
     68     int ans;
     69     ans=max(ta,tb);
     70     if(/*(s[mid]=='b'&&s[mid-1]=='w'&&s[mid+1]=='w')||*/(s[mid]=='w'&&s[mid+1]=='b'&&s[mid+2]=='w')&&mid+1<=n) //同上
     71     {
     72         ans+=1;
     73     }
     74     return ans;
     75 }
     76 int main()
     77 {
     78     int i,j,k;
     79     #ifndef ONLINE_JUDGE
     80     freopen("1.in","r",stdin);
     81     #endif
     82     scanf("%d",&tt);
     83     int ca=1;
     84     while(tt--)
     85     {
     86         printf("Case %d:
    ",ca++);
     87         scanf("%d%d",&n,&m);
     88         scanf("%s",s+1);
     89         s[0]='k';
     90         build(root);
     91         while(m--)
     92         {
     93             int op;
     94             scanf("%d",&op);
     95             if(op==0)
     96             {
     97                 int l,r;
     98                 scanf("%d%d",&l,&r);
     99                 //printf("%d %d
    ",l,r);
    100                 l++,r++;
    101                 int w=query(l,r,root);
    102                 printf("%d
    ",w);
    103             }
    104             else
    105             {
    106                 int x,val;
    107                 char c[10];
    108                 scanf("%d%s",&x,c);
    109                 x++;
    110                 if(c[0]!=s[x])  s[x]=c[0],update(x,val,root);
    111 
    112             }
    113         }
    114     }
    115 }
    TLE代码
  • 相关阅读:
    拓端数据tecdat|R语言用主成分PCA、 逻辑回归、决策树、随机森林分析心脏病数据并高维可视化
    拓端数据tecdat|R语言进行数据结构化转换:Box-Cox变换、“凸规则”变换方法
    拓端数据tecdat|R语言信用风险回归模型中交互作用的分析及可视化
    拓端数据tecdat|R语言多重比较示例:Bonferroni校正法和Benjamini & Hochberg法
    拓端数据tecdat|调查数据聚焦护士职业满意度
    拓端数据tecdat|【视频】Python和R使用指数加权*均(EWMA),ARIMA自回归移动*均模型预测时间序列
    拓端数据tecdat|R语言基于Bootstrap的线性回归预测置信区间估计方法
    拓端数据tecdat|R语言贝叶斯线性回归和多元线性回归构建工资预测模型
    拓端数据tecdat|R语言分段线性回归分析预测车辆的制动距离
    java序列化和反序列化
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4711113.html
Copyright © 2011-2022 走看看