zoukankan      html  css  js  c++  java
  • Naive Operations

    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
     
    Source
     

     线段树维护最小值,树状数组维护每点贡献。

    我这个线段树写的超级暴力,居然过了....

    Recommend
    chendu   |   We have carefully selected several similar problems for you:  6318 6317 6316 6315 6314 
    #include <iostream>
    #include <bits/stdc++.h>
    #define maxn 400005
    using namespace std;
    typedef long long ll;
    ll tree[maxn]={0};
    ll lazy[maxn]={0};
    ll b[100005]={0};
    ll c[100005];
    int n;
    ll lowbit(ll x)
    {
        return x&-x;
    }
    void update(ll x,ll val)
    {
        for(int i=x;i<=n;i+=lowbit(i))
            c[i]+=val;
    }
    ll sum(ll x)
    {
        ll res=0;
        for(int i=x;i>0;i-=lowbit(i))
            res+=c[i];
        return res;
    }
    void pushup(int rt)
    {
        tree[rt]=min(tree[rt*2],tree[rt*2+1]);
    }
    void buildtree(int l,int r,int rt)
    {
        if(l==r)
        {
            tree[rt]=b[l];
            return;
        }
        int mid=(l+r)/2;
        buildtree(l,mid,rt*2);
        buildtree(mid+1,r,rt*2+1);
        pushup(rt);
    }
    void pushdown(int rt)
    {
        if(lazy[rt])
        {
            lazy[rt*2]+=lazy[rt];
            lazy[rt*2+1]+=lazy[rt];
            tree[rt*2]+=lazy[rt];
            tree[rt*2+1]+=lazy[rt];
            lazy[rt]=0;
        }
    }
    void add(int l,int r,int L,int R,int rt,int val)
    {
        if(L<=l&&R>=r)
        {
            lazy[rt]+=val;
            tree[rt]+=val;
            return;
        }
        if(lazy[rt]) 
            pushdown(rt);
        int mid=(l+r)/2;
        if(L<=mid) 
            add(l,mid,L,R,rt*2,val);
        if(R>mid) 
        add(mid+1,r,L,R,rt*2+1,val);
        pushup(rt);
    }
    ll query(int l,int r,int L,int R,int rt)
    {
        if(L<=l&&R>=r)
            return tree[rt];
        int mid=(l+r)/2;
        pushdown(rt);
        ll ret=1e9;
        if(L<=mid) 
            ret=query(l,mid,L,R,rt*2);
        if(R>mid) 
        ret=min(ret,query(mid+1,r,L,R,rt*2+1));
        return ret;
    }
    int get_min(int l,int r,int L,int R,int rt)
    {
        if(l==r&&l>=L&&l<=R)
            return l;
        int mid=(l+r)/2;
        int ans;
        if(query(1,n,l,mid,1)<query(1,n,mid+1,r,1)) 
            ans=get_min(l,mid,L,R,rt*2);
        else 
            ans=get_min(mid+1,r,L,R,rt*2+1);
        return ans;
    }
    int main()
    {
        int q;
        while(~scanf("%d%d",&n,&q))
        {
            memset(tree,0,sizeof(tree));
            memset(lazy,0,sizeof(lazy));
            memset(c,0,sizeof(c));
            for(int i=1;i<=n;i++)
                scanf("%d",&b[i]);
            buildtree(1,n,1);
            char s[50];
            int l,r;
            while(q--)
            {
               scanf("%s %d %d",s,&l,&r);
               if(s[0]=='a')
               {
                   add(1,n,l,r,1,-1);
                   while(query(1,n,l,r,1)==0)
                   {
                       int id=get_min(1,n,l,r,1);
                       add(1,n,id,id,1,b[id]);
                       update(id,1);
                   }
               }
               else
                   printf("%lld
    ",sum(r)-sum(l-1));
           }
        }
        return 0;
    }
    

      

  • 相关阅读:
    stringify 字符串转化成json方法
    leetcode每日刷题计划-简单篇day22
    leetcode每日刷题计划-简单篇day21
    leetcode每日刷题计划-简单篇day20
    leetcode每日刷题计划-简单篇day19
    leetcode每日刷题计划-简单篇day18
    leetcode每日刷题计划-简单篇day17
    leetcode每日刷题计划-简单篇day16
    leetcode每日刷题计划-简单篇day15
    leetcode每日刷题计划-简单篇day14
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/9370226.html
Copyright © 2011-2022 走看看