Cod sursa(job #852366)

Utilizator blk.irineluIrina Ursateanu blk.irinelu Data 11 ianuarie 2013 08:29:54
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include<fstream>
#include<cmath>
#define MAX_N 15000
#define MAX_M 100000

using namespace std;

ifstream in("datorii.in");
ofstream out("datorii.out");

//Smenul lui Batog
int main()
{
    int N,M,i,SqrtN,V[MAX_N],B[1500];
    in>>N>>M;

    SqrtN=(int)sqrt(N);

    for(i=0;i<N;i++)
     {
        in>>V[i];
        B[i/SqrtN]+=V[i];
    }

    for(i=1;i<=M;i++)
    {

        int op;//0-achitare, 1-interogare
        int P,Q;
        in>>op>>P>>Q;

        if(op==1)
        {
            P--;
            Q--;

            int aux1=P/SqrtN ;
            int aux2=Q/SqrtN ;

            int suma=0,j=0;
            if(aux1==aux2)
                for(j=P;j<=Q;j++) suma+=V[j] ;
            else
            {
                for(j=P;j<(aux1+1)*SqrtN;j++) suma+=V[j] ;
                for(j=aux1+1;j<aux2;j++) suma+=B[j] ;
                for(j=aux2*SqrtN;j<=Q;j++) suma+=V[j] ;
            }

            out<<suma<<"\n";

        }
        else
        {
            P--;
            int aux=P/SqrtN;
            V[P]-=Q;
            B[aux]-=Q;
        }
    }

    return 0 ;
}