fork download
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5.  
  6.  
  7. class SegmentTree {
  8.  
  9. int sz;
  10. vector<int> t;
  11.  
  12. int query(int v, int vl, int vr, int l, int r) const {
  13. if (r < vl || vr < l)
  14. return 0;
  15. if (l <= vl && vr <= r)
  16. return t[v];
  17. int vm = vl + (vr - vl) / 2;
  18. int ql = query(2 * v + 1, vl, vm, l, r);
  19. int qr = query(2 * v + 2, vm + 1, vr, l, r);
  20. return ql + qr;
  21. }
  22.  
  23. void modify(int v, int vl, int vr, int pos, int val) {
  24. if (vl == vr) {
  25. t[v] += val;
  26. return;
  27. }
  28. int vm = vl + (vr - vl) / 2;
  29. if (pos <= vm)
  30. modify(2 * v + 1, vl, vm, pos, val);
  31. else
  32. modify(2 * v + 2, vm + 1, vr, pos, val);
  33. t[v] = t[2 * v + 1] + t[2 * v + 2];
  34. }
  35.  
  36. public:
  37.  
  38. SegmentTree(const vector<int> &a) {
  39. sz = a.size();
  40. t.resize(4 * sz);
  41. }
  42.  
  43. int query(int l, int r) const {
  44. return query(0, 0, sz - 1, l, r);
  45. }
  46.  
  47. void modify(int pos, int val) {
  48. modify(0, 0, sz - 1, pos, val);
  49. }
  50.  
  51. };
  52.  
  53.  
  54. class Event {
  55.  
  56. int type, index, xl, xr, y;
  57.  
  58. public:
  59.  
  60. Event(int type, int index, int xl, int xr, int y) : type(type), index(index), xl(xl), xr(xr), y(y) {}
  61.  
  62. bool operator < (const Event &that) const {
  63. return y > that.y || y == that.y && type < that.type;
  64. }
  65.  
  66. void process(SegmentTree &st, vector<int> &answers) const {
  67. if (type == 1)
  68. answers[index] = st.query(xl, xr);
  69. if (type == 2)
  70. st.modify(xl, 1);
  71. }
  72.  
  73. };
  74.  
  75.  
  76. int main() {
  77. vector<Event> scanLine;
  78.  
  79. int aSize;
  80. scanf("%d", &aSize);
  81.  
  82. vector<int> a(aSize);
  83. for (int i = 0; i < aSize; i++) {
  84. scanf("%d", &a[i]);
  85. scanLine.push_back(Event(2, -1, i, -1, a[i]));
  86. }
  87.  
  88. SegmentTree st(a);
  89.  
  90. int queriesCount;
  91. scanf("%d", &queriesCount);
  92.  
  93. for (int i = 0; i < queriesCount; i++) {
  94. int l, r, k;
  95. scanf("%d%d%d", &l, &r, &k);
  96. scanLine.push_back(Event(1, i, l, r, k));
  97. }
  98.  
  99. vector<int> answers(queriesCount);
  100. sort(scanLine.begin(), scanLine.end());
  101. for (int i = 0; i < scanLine.size(); i++)
  102. scanLine[i].process(st, answers);
  103.  
  104. for (int i = 0; i < queriesCount; i++)
  105. printf("%d\n", answers[i]);
  106. }
Success #stdin #stdout 0s 3488KB
stdin
5
5 1 2 3 4
3
2 4 1
4 4 4
1 5 2 
stdout
3
0
2