zoukankan      html  css  js  c++  java
  • 2015上海网络赛 HDU 5475 An easy problem 线段树

    题意就不说了

    思路:线段树,维护区间乘积。2操作就将要除的点更新为1.

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<cmath>
      5 #include<algorithm>
      6 #include<queue>
      7 #include<vector>
      8 #include<set>
      9 #include<string>
     10 #define inf 0x3f3f3f3f
     11 #define LL long long
     12 #define mid(a,b) ((a+b)>>1)
     13 #define ll long long
     14 #define maxn 110000
     15 #define IN freopen("in.txt","r",stdin);
     16 using namespace std;
     17 
     18 LL mod;
     19 LL num[maxn];
     20 struct Tree
     21 {
     22     int left,right;
     23     LL pro;    /*pro为区间和,可改为最大值最小值等*/
     24 }tree[maxn<<2]; /*四倍大小*/
     25 
     26 
     27 /*递归建树*/
     28 void build(int i,int left,int right)
     29 {
     30     tree[i].left=left;
     31     tree[i].right=right;
     32 
     33     if(left==right){
     34         tree[i].pro=num[left]%mod;
     35         return ;
     36     }
     37 
     38     int mid=mid(left,right);
     39 
     40     build(i<<1,left,mid);
     41     build(i<<1|1,mid+1,right);
     42 
     43     tree[i].pro=(tree[i<<1].pro*tree[i<<1|1].pro)%mod;
     44 }
     45 
     46 /*单点修改,d为改变量,两函数可共存*/
     47 void update(int i,int x,LL d)
     48 {
     49     if(tree[i].left==tree[i].right){
     50         tree[i].pro=d;
     51         return;
     52     }
     53 
     54     int mid=mid(tree[i].left,tree[i].right);
     55 
     56     if(x<=mid) update(i<<1,x,d);
     57     else update(i<<1|1,x,d);
     58 
     59     tree[i].pro=(tree[i<<1].pro*tree[i<<1|1].pro)%mod;
     60 }
     61 
     62 /*区间结果查询*/
     63 LL query(int i,int left,int right)
     64 {
     65     if(tree[i].left==left&&tree[i].right==right)
     66         return tree[i].pro%mod;
     67 
     68     int mid=mid(tree[i].left,tree[i].right);
     69 
     70     if(right<=mid) return query(i<<1,left,right);
     71     else if(left>mid) return query(i<<1|1,left,right);
     72     else return (query(i<<1,left,mid)*query(i<<1|1,mid+1,right))%mod;
     73 }
     74 
     75 int main(int argc, char const *argv[])
     76 {
     77     //IN;
     78 
     79     int t,ca=1;scanf("%d",&t);
     80     while(t--)
     81     {
     82         printf("Case #%d:
    ",ca++);
     83         int n;scanf("%d %lld",&n,&mod);
     84         //fill(num,num+maxn,1);
     85         int type[maxn];LL val[maxn];
     86         for(int i=1;i<=n;i++){
     87             scanf("%d %lld",&type[i],&val[i]);
     88             if(type[i]==1) num[i]=val[i];
     89             if(type[i]==2) num[i]=1;
     90         }
     91         build(1,1,n);
     92         for(int i=1;i<=n;i++){
     93             if(type[i]==1){
     94                 LL ans=query(1,1,i);
     95                 printf("%lld
    ",ans%mod);
     96             }
     97             else if(type[i]==2){
     98                 update(1,val[i],1LL);
     99                 LL ans=query(1,1,i);
    100                 printf("%lld
    ",ans%mod);
    101             }
    102         }
    103 
    104     }
    105 
    106     return 0;
    107 }
  • 相关阅读:
    Sublime Text 3 破解 + 注册 + 汉化 + 教程 + 快捷键大全 + 中文乱码 +编译JAVA
    cocos2dx 翻牌效果
    php中将SimpleXMLElement Object转化为普通数组
    VS2013+lua5.3.1 环境配置
    Jquery相册 fancybox-1.3.4
    JS~字符串长度判断,超出进行自动截取(支持中文)
    JS /jquery 时间戳与日期转换
    php验证身份证号码正确性
    JS身份证验证
    逆向编程一,PE结构拉伸内存
  • 原文地址:https://www.cnblogs.com/macinchang/p/4841024.html
Copyright © 2011-2022 走看看