Advertisement
a53

B_o_g_d_a_n

a53
Jun 13th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. #include <fstream>
  2. #define Nmax 300000
  3. #define BUF 16384
  4. using namespace std;
  5. class InParser {
  6. private:
  7. FILE *fin;
  8. char *buff;
  9. int sp;
  10.  
  11. char read_ch() {
  12. ++sp;
  13. if (sp == BUF) {
  14. sp = 0;
  15. fread(buff, 1, BUF, fin);
  16. }
  17. return buff[sp];
  18. }
  19.  
  20. public:
  21. InParser(const char* nume) {
  22. fin = fopen(nume, "r");
  23. buff = new char[BUF]();
  24. sp = BUF-1;
  25. }
  26.  
  27. InParser& operator >> (int &n) {
  28. char c;
  29. while (!isdigit(c = read_ch()) && c != '-');
  30. int sgn = 1;
  31. if (c == '-') {
  32. n = 0;
  33. sgn = -1;
  34. } else {
  35. n = c - '0';
  36. }
  37. while (isdigit(c = read_ch())) {
  38. n = 10 * n + c - '0';
  39. }
  40. n *= sgn;
  41. return *this;
  42. }
  43.  
  44. InParser& operator >> (long long &n) {
  45. char c;
  46. n = 0;
  47. while (!isdigit(c = read_ch()) && c != '-');
  48. long long sgn = 1;
  49. if (c == '-') {
  50. n = 0;
  51. sgn = -1;
  52. } else {
  53. n = c - '0';
  54. }
  55. while (isdigit(c = read_ch())) {
  56. n = 10 * n + c - '0';
  57. }
  58. n *= sgn;
  59. return *this;
  60. }
  61. };
  62.  
  63. using namespace std;
  64.  
  65. class OutParser {
  66. private:
  67. FILE *fout;
  68. char *buff;
  69. int sp;
  70.  
  71. void write_ch(char ch) {
  72. if (sp == 50000) {
  73. fwrite(buff, 1, 50000, fout);
  74. sp = 0;
  75. buff[sp++] = ch;
  76. } else {
  77. buff[sp++] = ch;
  78. }
  79. }
  80.  
  81.  
  82. public:
  83. OutParser(const char* name) {
  84. fout = fopen(name, "w");
  85. buff = new char[50000]();
  86. sp = 0;
  87. }
  88. ~OutParser() {
  89. fwrite(buff, 1, sp, fout);
  90. fclose(fout);
  91. }
  92.  
  93. OutParser& operator << (int vu32) {
  94. if (vu32 <= 9) {
  95. write_ch(vu32 + '0');
  96. } else {
  97. (*this) << (vu32 / 10);
  98. write_ch(vu32 % 10 + '0');
  99. }
  100. return *this;
  101. }
  102.  
  103. OutParser& operator << (long long vu64) {
  104. if (vu64 <= 9) {
  105. write_ch(vu64 + '0');
  106. } else {
  107. (*this) << (vu64 / 10);
  108. write_ch(vu64 % 10 + '0');
  109. }
  110. return *this;
  111. }
  112.  
  113. OutParser& operator << (char ch) {
  114. write_ch(ch);
  115. return *this;
  116. }
  117. OutParser& operator << (const char *ch) {
  118. while (*ch) {
  119. write_ch(*ch);
  120. ++ch;
  121. }
  122. return *this;
  123. }
  124. };
  125.  
  126. InParser f("bogdan.in");
  127. OutParser g("bogdan.out");
  128.  
  129. bool Corect;
  130. int n,v[Nmax],pos,valoare,a,b,Maxim;
  131. struct arbore_intervale
  132. {
  133. int val;
  134. bool cresc;
  135. }arb[Nmax];
  136.  
  137. void creare(int nod,int st,int dr)
  138. {
  139. if (st==dr)
  140. {
  141. f>>arb[nod].val;
  142. v[st]=arb[nod].val;
  143. arb[nod].cresc=true;
  144. }
  145. else
  146. {
  147. int mij=(st+dr)/2;
  148. creare(2*nod,st,mij);
  149. creare(2*nod+1,mij+1,dr);
  150. if(v[mij]<=v[mij+1]&&arb[2*nod].cresc==1&&arb[2*nod+1].cresc==1)
  151. {
  152. arb[nod].cresc=true;
  153. }
  154. else
  155. {
  156. arb[nod].cresc=false;
  157. }
  158. }
  159. }
  160.  
  161. void citire()
  162. {
  163. f>>n;
  164. creare(1,1,n);
  165. }
  166.  
  167. void adauga(int nod,int st,int dr)
  168. {
  169. if(st==dr)
  170. {
  171. arb[nod].val=valoare;
  172. }
  173. else
  174. {
  175. int mij=(st+dr)/2;
  176. if(pos<=mij)
  177. {
  178. adauga(2*nod,st,mij);
  179. }
  180. else
  181. {
  182. adauga(2*nod+1,mij+1,dr);
  183. }
  184. if(arb[2*nod].cresc==true&&arb[2*nod+1].cresc==true&&v[mij]<=v[mij+1])
  185. {
  186. arb[nod].cresc=1;
  187. }
  188. else
  189. {
  190. arb[nod].cresc=0;
  191. }
  192. }
  193. }
  194.  
  195. void verificare(int nod,int st,int dr)
  196. {
  197. if(a<=st&&dr<=b)
  198. {
  199. if(arb[nod].cresc==0||(Maxim>v[st]))
  200. {
  201. Corect=false;
  202. }
  203. else
  204. {
  205. Maxim=v[dr];
  206. }
  207. }
  208. else
  209. if(Corect==true)
  210. {
  211. int mij=(st+dr)/2;
  212. if(a<=mij)
  213. {
  214. verificare(2*nod,st,mij);
  215. }
  216. if(mij+1<=b&&Corect)
  217. {
  218. verificare(2*nod+1,mij+1,dr);
  219. }
  220. }
  221. }
  222.  
  223. void rez()
  224. {
  225. int Numar_Teste,Tip_Test;
  226. f>>Numar_Teste;
  227. for(int i=1;i<=Numar_Teste;i++)
  228. {
  229. f>>Tip_Test;
  230. if(Tip_Test==1)
  231. {
  232. f>>pos>>valoare;
  233. v[pos]=valoare;
  234. adauga(1,1,n);
  235. }
  236. else
  237. {
  238. f>>a>>b;
  239. Corect=true;
  240. Maxim=v[a];
  241. verificare(1,1,n);
  242. if(Corect==true)
  243. g<<"DA"<<'\n';
  244. else
  245. g<<"NU"<<'\n';
  246. }
  247. }
  248. }
  249.  
  250. int main()
  251. {
  252. citire();
  253. rez();
  254. return 0;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement