zoukankan      html  css  js  c++  java
  • HDU 多校对抗 F Naive Operations

    Naive Operations

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)
    Total Submission(s): 2691    Accepted Submission(s): 1183


    Problem Description
    In a galaxy far, far away, there are two integer sequence a and b of length n.
    b is a static permutation of 1 to n. Initially a is filled with zeroes.
    There are two kind of operations:
    1. add l r: add one for al,al+1...ar
    2. query l r: query ri=lai/bi
     
    Input
    There are multiple test cases, please read till the end of input file.
    For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
    In the second line, n integers separated by spaces, representing permutation b.
    In the following q lines, each line is either in the form 'add l r' or 'query l r', representing an operation.
    1n,q1000001lrn, there're no more than 5 test cases.
     
    Output
    Output the answer for each 'query', each one line.
     
    Sample Input
    5 12 1 5 2 4 3 add 1 4 query 1 4 add 2 5 query 2 5 add 3 5 query 1 5 add 2 4 query 1 4 add 2 5 query 2 5 add 2 2 query 1 5
     
    Sample Output
    1 1 2 4 4 6
     

    题意:初始时有一段长度为n的数组a为0,长度为n的数组b,给你数组b

      有操作add,把区间[l,r]内每一个ai+1,query,查询操作。 区间 a[i]/b[i]向下取整的和。

    题解:

    我们每次区间加一,变成把每个值减一,每次减到0的时候ai/bi的值就会+1,用cnt记录,再把值重新更新为bi,查询的时候查询+1 的总和。

    用线段树保留最小值,当出现最小值为0的时候把cnt++,值更新为b[r],因为每次只会加+1所以总数不会太大

    zzq的做法

    考虑维护 的这样的最小的 ,每次 加一的时候 就减
    一,一旦 变成 了那么就需要把 加一,这样两个线段树维护一下就行了。
    注意到 由于 是排列是 的,那么复杂度就是 。

    代码如下:

    #include <map>
    #include <set>
    #include <cmath>
    #include <ctime>
    #include <stack>
    #include <queue>
    #include <cstdio>
    #include <cctype>
    #include <bitset>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #define fuck(x) cout<<"["<<x<<"]";
    #define FIN freopen("input.txt","r",stdin);
    #define FOUT freopen("output.txt","w+",stdout);
    //#pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    const int maxn = 1e5+5;
    const int INF = 0x3f3f3f3f;
    const long long mod = 1e9+7;
    const double eps = 1e-8;
    int b[4*maxn];
    int n,q;
    int dat[4*maxn];
    int lazy[4*maxn];
    int res;
    int cnt[maxn*4];
    void init(int l,int r,int k){
        int chl=k<<1|1,chr=(k+1)<<1,mid=(l+r)/2;
        if(r-l==1){
            lazy[k]=cnt[k]=0;
            dat[k]=b[r];
            return ;
        }else{
            lazy[k]=cnt[k]=0;
            init(l,mid,chl);
            init(mid,r,chr);
            dat[k]=min(dat[chl],dat[chr]);
        }
    }
    int sum(int a,int c,int l,int r,int k){
        int chl=k<<1|1,chr=(k+1)<<1,mid=(l+r)/2;
        if(c<=l||a>=r){
            return 0;
        }else if(a<=l&&r<=c){
            return cnt[k];
        }else {
            lazy[chl]+=lazy[k];
            lazy[chr]+=lazy[k];
            lazy[k]=0;
            dat[k]=min(dat[chl]+lazy[chl],dat[chr]+lazy[chr]);
            return sum(a,c,l,mid,chl)+sum(a,c,mid,r,chr);
        }
    }
    void updata(int a,int c,int l ,int r ,  int k){
        int chl=k<<1|1,chr=(k+1)<<1,mid=(l+r)/2;
        if(c<=l||a>=r){
            return;
        }else if(a<=l&&r<=c){
            if(lazy[k]+dat[k]-1<=0){
                if(r-l==1){
                    cnt[k]++;
                    dat[k]=b[r];
                    lazy[k]=0;
                    return;
                }
                lazy[chl]+=lazy[k];
                lazy[chr]+=lazy[k];
                lazy[k]=0;
                updata(a,c,l,mid,chl);
                updata(a,c,mid,r,chr);
                if(r-l!=1){
                    cnt[k]=cnt[chl]+cnt[chr];
                    dat[k]=min(dat[chl]+lazy[chl],dat[chr]+lazy[chr]);
                }
                return;
            }
            lazy[k]--;
        }else{
            lazy[chl]+=lazy[k];
            lazy[chr]+=lazy[k];
            updata(a,c,l,mid,chl);
            updata(a,c,mid,r,chr);
            lazy[k]=0;
            dat[k]=min(dat[chl]+lazy[chl],dat[chr]+lazy[chr]);
            if(r-l!=1) cnt[k]=cnt[chl]+cnt[chr];
        }
    }
    char ch[10];
    int l,r;
    int main(){
    #ifndef ONLINE_JUDGE
        FIN
    #endif
        while(scanf("%d%d",&n,&q) !=EOF){
            for(int i=1;i<=n;i++){
                scanf("%d",&b[i]);
            }
            init(0,n,0);
            while(q--){
                scanf("%s %d %d",ch,&l,&r);
                if(ch[0]=='a'){
                    updata(l-1,r,0,n,0);
                }else{
                    printf("%d
    ",sum(l-1,r,0,n,0));
                }
            }
        }
        return 0;
    }
    View Code
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<stdlib.h>
    #include<algorithm>
    #define L long long
    using namespace std;
    const int q=998244353;
    int n,m,t,c[3010][3010],f[3010][3010],x[9000010],a[3010],b[3010],p;
    int main()
    {
        int i,j,k,l;
        for(i=0;i<=3000;i++)
          {
           c[i][0]=1;
           for(j=1;j<=i;j++)
             c[i][j]=(c[i-1][j]+c[i-1][j-1])%q;
          }
        x[0]=1;
        for(i=1;i<=9000000;i++)
          x[i]=(x[i-1]<<1)%q;
        while(scanf("%d%d",&n,&m)!=EOF)
          {
           scanf("%d%d",&i,&j);
           a[i]=1;
           for(k=i+1;k<=n;k++)
             {
              a[k]=1;
              for(l=i;l<k;l++)
                a[k]=(a[k]-(L)a[l]*c[k][l])%q;
             }
           b[j]=1;
           for(k=j+1;k<=m;k++)
             {
              b[k]=1;
              for(l=j;l<k;l++)
                b[k]=(b[k]-(L)b[l]*c[k][l])%q;
             }
           for(k=i;k<=n;k++)
             for(l=j;l<=m;l++)
               f[k][l]=(L)c[n][k]*c[m][l]%q*x[(n-k)*(m-l)]%q;
           p=0;
           for(k=i;k<=n;k++)
             for(l=j;l<=m;l++)
               p=(p+(L)f[k][l]*a[k]%q*b[l])%q;
           p=(p+q)%q;
           printf("%d
    ",p);
          }
        return 0;
    }
    View Code
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    Day 15 模块
    Day 14 三元运算符,列表推导式,内置函数
    Day 13 可迭代对象,迭代器对象,for循环迭代,生成器对象,枚举对象
    Day 12 开放封闭原则,装饰器初识
    Day 11 函数对象,函数嵌套,作用域,闭包
    Day 10 函数的形参,实参
    Day 09 函数基础
    Day 08 文件操作模式,文件复制,游标
    HTTP协议
    11,.JS-DOM价绍
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9398113.html
Copyright © 2011-2022 走看看