algorithm - HackerEarth challenge Program - Help Captain Gordon - which corner case am I missing? -


this hackerearth programming challenge question. here's link

here's problem

a large network of criminals working under joker destroy , demolish gotham city. captain gordon quite tensed these criminals can gotham city. fortunately, batman comes rescue , of batman, able rescue n hostages trapped criminals working under joker. makes them stand in line , each of n hostage given id between 1 n (inclusive) , no 2 hostages have same id. newly hired programmer assigned task of keeping track of health status of hostages captain gordon heals them giving them aspirin each time. initially,health of each hostage 0. performs following action -

• gives aspirin of value v hostages having id between x , y (both inclusive) increaces present health v.

• asks give him sum of health of hostages having id between x , y (both inclusive) can write program keeps track of gordon’s action?

input 1st line contains n-no. of hostages 2nd line contains m-no. of actions performed captain gordon. next m lines contain actions in form of either “ 1 x y v “ denotes action 1 or “ 2 x y “ denotes action 2.

output each action captain gordon does, if action 1 carried out, output nothing if action 2 carried out, program must output result gordon asks for.

constraints

1) n<=10^5

2) m<=10^5

3) 1<=x < y<=n

4) 0<=v<=100

sample input 5 6 2 1 5 1 3 4 5 2 2 4 1 1 3 10 1 1 5 5 2 1 5  sample output 0 10 65 

there sample input , sample output. before submitting answer can compile code , test on there. code passed it. can submit answer. after submitting answer code tested various test cases.

out of 10 tests, code passed 6 tests , 4 partially. , problem can't see tests there!!

can finds whats wrong code or corner cases missing. here's code

#include <stdio.h>  int main() {      int n, m, i, j, x, y, v, sum, actn, arr[100001] = {0};      scanf("%d%d", &n, &m);      (i = 1; <= m; i++) {         scanf("%d", &actn);          if (actn == 1) {             scanf("%d%d%d", &x, &y, &v);             (j = x; j <= y; j++)                 arr[j] += v;         } else {             scanf("%d%d", &x, &y);             sum = 0;             (j = x; j <= y; j++)                 sum += arr[j];             printf("%d\n", sum);         }     }      return 0; } 

thanks in advance

these type of questions, when input range 10^5 can done using segment trees.

the idea maintain sums of intermediate range values in tree structure , when range queried, adding 2 or 3 sub ranges gives answer.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -