Cod sursa(job #1738334)

Utilizator KonoplyankaKonoplyanka Konoplyanka Data 6 august 2016 14:16:31
Problema SequenceQuery Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.44 kb
#include <fstream>

using namespace std;
class InputReader {
    public:
        InputReader() {}
        InputReader(const char *file_name) {
            input_file = fopen(file_name, "r");
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
        inline InputReader &operator >>(int &n) {
            while((buffer[cursor] < '0' || buffer[cursor] > '9')&& buffer[cursor]!='-') {
                advance();
            }
            semn=1;
            if(buffer[cursor]=='-')
            {
                semn=-1;
                advance();
            }
            n = 0;
            while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
                n = n * 10 + buffer[cursor] - '0';
                advance();
            }
            n*=semn;
            return *this;
        }
    private:
        FILE *input_file;
        static const int SIZE = 1 << 20;
        int cursor,semn;
        char buffer[SIZE];
        inline void advance() {
            ++ cursor;
            if(cursor == SIZE) {
                cursor = 0;
                fread(buffer, SIZE, 1, input_file);
            }
        }
}f("sequencequery.in");
ofstream g("sequencequery.out");
int a,b,n,m,x;
long long sol,S;
inline long long maxi(long long a,long long b)
{
    return (a>b?a:b);
}
struct aint
{
    int best;
    int sum;
    int st;
    int dr;
    inline void init(int val)
    {
        best=sum=st=dr=val;
    }
    inline void up(aint a,aint b)
    {
        sum=a.sum+b.sum;
        best=maxi(maxi(a.best,b.best),a.dr+b.st);
        st=maxi(a.st,a.sum+b.st);
        dr=maxi(b.dr,a.dr+b.sum);
    }
}AI[1<<18];
inline void build(int nod,int st,int dr)
{
    if(st==dr)
    {
        f>>x;
        AI[nod].init(x);
        return ;
    }
    int mid=(st+dr)>>1;
    build(nod<<1,st,mid);
    build((nod<<1)+1,mid+1,dr);
    AI[nod].up(AI[nod<<1],AI[(nod<<1)+1]);
}
inline void query(int nod,int st,int dr)
{
    if(a<=st&&dr<=b)
    {
        if(S<0) S=0;
        sol=maxi(sol,maxi(AI[nod].best,S+AI[nod].st));
        S=maxi(S+AI[nod].sum,AI[nod].dr);
        return ;
    }
    int mid=(st+dr)>>1;
    if(a<=mid) query(nod<<1,st,mid);
    if(b>mid) query((nod<<1)+1,mid+1,dr);
}
int main()
{
    f>>n>>m;
    build(1,1,n);
    while(m--)
    {
        f>>a>>b;
        sol=-(1<<20);S=0;
        query(1,1,n);
        g<<sol<<'\n';
    }
    return 0;
}