|
| 1 | + |
| 2 | +/** Which of the favors of your Lord will you deny ? **/ |
| 3 | + |
| 4 | +#include<bits/stdc++.h> |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +#define LL long long |
| 8 | +#define PII pair<int,int> |
| 9 | +#define PLL pair<LL,LL> |
| 10 | +#define F first |
| 11 | +#define S second |
| 12 | + |
| 13 | +#define ALL(x) (x).begin(), (x).end() |
| 14 | +#define READ freopen("alu.txt", "r", stdin) |
| 15 | +#define WRITE freopen("vorta.txt", "w", stdout) |
| 16 | + |
| 17 | +#ifndef ONLINE_JUDGE |
| 18 | +#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl |
| 19 | +#else |
| 20 | +#define DBG(x) |
| 21 | +#define endl "\n" |
| 22 | +#endif |
| 23 | + |
| 24 | +template<class T1, class T2> |
| 25 | +ostream &operator <<(ostream &os, pair<T1,T2>&p); |
| 26 | +template <class T> |
| 27 | +ostream &operator <<(ostream &os, vector<T>&v); |
| 28 | +template <class T> |
| 29 | +ostream &operator <<(ostream &os, set<T>&v); |
| 30 | + |
| 31 | +inline void optimizeIO() |
| 32 | +{ |
| 33 | + ios_base::sync_with_stdio(false); |
| 34 | + cin.tie(NULL); |
| 35 | +} |
| 36 | + |
| 37 | +const int nmax = 2e5+7; |
| 38 | + |
| 39 | +/** |
| 40 | +
|
| 41 | +Implicit Segment Tree (Point Update , Range Query) |
| 42 | +================================================== |
| 43 | +1 based indexing |
| 44 | +
|
| 45 | +**/ |
| 46 | + |
| 47 | +struct Node |
| 48 | +{ |
| 49 | + int sum; |
| 50 | + Node *left , *right; |
| 51 | + |
| 52 | + Node() /// change here |
| 53 | + { |
| 54 | + sum = 0; |
| 55 | + left = NULL; |
| 56 | + right = NULL; |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +struct ImplicitSegmentTree |
| 61 | +{ |
| 62 | + int n; |
| 63 | + Node *root; |
| 64 | + |
| 65 | + ImplicitSegmentTree(int n) |
| 66 | + { |
| 67 | + this->n = n; |
| 68 | + root = new Node(); |
| 69 | + } |
| 70 | + |
| 71 | + void update(int updex,int val) |
| 72 | + { |
| 73 | + update(root,1,n,updex,val); |
| 74 | + } |
| 75 | + |
| 76 | + void update(Node* v,int start,int end,int updex,int val) |
| 77 | + { |
| 78 | + if(start==end) |
| 79 | + { |
| 80 | + v->sum += val; |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + int mid = (start+end)>>1; |
| 85 | + |
| 86 | + if(v->left==NULL) v->left = new Node(); |
| 87 | + if(v->right==NULL) v->right = new Node(); |
| 88 | + |
| 89 | + if(updex<=mid) update(v->left,start,mid,updex,val); |
| 90 | + else update(v->right,mid+1,end,updex,val); |
| 91 | + |
| 92 | + v->sum = v->left->sum + v->right->sum; |
| 93 | + } |
| 94 | + |
| 95 | + int query(int ql,int qr) |
| 96 | + { |
| 97 | + return query(root,1,n,ql,qr); |
| 98 | + } |
| 99 | + |
| 100 | + int query(Node *v,int start,int end,int ql,int qr) |
| 101 | + { |
| 102 | + if(v==NULL) return 0; |
| 103 | + |
| 104 | + if(end<ql || start>qr) return 0; |
| 105 | + |
| 106 | + if(start>=ql && end<=qr) |
| 107 | + { |
| 108 | + return v->sum; |
| 109 | + } |
| 110 | + |
| 111 | + int mid = (start+end)>>1; |
| 112 | + |
| 113 | +// if(v->left==NULL) v->left = new Node(); |
| 114 | +// if(v->right==NULL) v->right = new Node(); |
| 115 | + |
| 116 | + /// query on both child |
| 117 | + int p1 = query(v->left,start,mid,ql,qr); |
| 118 | + int p2 = query(v->right,mid+1,end,ql,qr); |
| 119 | + |
| 120 | + return p1+p2; |
| 121 | + } |
| 122 | +}; |
| 123 | + |
| 124 | +void solveTC() |
| 125 | +{ |
| 126 | + int n,q; |
| 127 | +// cin>>n>>q; |
| 128 | + scanf("%d %d",&n,&q); |
| 129 | + |
| 130 | + int MX = 1e9+2; |
| 131 | + |
| 132 | + ImplicitSegmentTree s(MX); |
| 133 | + |
| 134 | + vector<int>v(n+1); |
| 135 | + |
| 136 | + for(int i=1;i<=n;i++) scanf("%d",&v[i]); |
| 137 | + |
| 138 | + for(int i=1;i<=n;i++) s.update(v[i],1); |
| 139 | + |
| 140 | + while(q--) |
| 141 | + { |
| 142 | + char ch; |
| 143 | + scanf(" %c ",&ch); |
| 144 | + |
| 145 | + if(ch=='!') |
| 146 | + { |
| 147 | + int pos,new_val; |
| 148 | + scanf("%d %d",&pos,&new_val); |
| 149 | + |
| 150 | + s.update(v[pos],-1); |
| 151 | + v[pos] = new_val; |
| 152 | + s.update(v[pos],1); |
| 153 | + |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + int a,b; |
| 158 | + scanf("%d %d",&a,&b); |
| 159 | + |
| 160 | + int ans = s.query(a,b); |
| 161 | + printf("%d\n",ans); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +int32_t main() |
| 167 | +{ |
| 168 | + optimizeIO(); |
| 169 | + |
| 170 | + int tc = 1; |
| 171 | +// cin>>tc; |
| 172 | + |
| 173 | + while(tc--) |
| 174 | + { |
| 175 | + solveTC(); |
| 176 | + } |
| 177 | + |
| 178 | + return 0; |
| 179 | +} |
| 180 | + |
| 181 | +/** |
| 182 | +
|
| 183 | +**/ |
| 184 | + |
| 185 | +template<class T1, class T2> |
| 186 | +ostream &operator <<(ostream &os, pair<T1,T2>&p) |
| 187 | +{ |
| 188 | + os<<"{"<<p.first<<", "<<p.second<<"} "; |
| 189 | + return os; |
| 190 | +} |
| 191 | +template <class T> |
| 192 | +ostream &operator <<(ostream &os, vector<T>&v) |
| 193 | +{ |
| 194 | + os<<"[ "; |
| 195 | + for(T i:v) |
| 196 | + { |
| 197 | + os<<i<<" " ; |
| 198 | + } |
| 199 | + os<<" ]"; |
| 200 | + return os; |
| 201 | +} |
| 202 | + |
| 203 | +template <class T> |
| 204 | +ostream &operator <<(ostream &os, set<T>&v) |
| 205 | +{ |
| 206 | + os<<"[ "; |
| 207 | + for(T i:v) |
| 208 | + { |
| 209 | + os<<i<<" "; |
| 210 | + } |
| 211 | + os<<" ]"; |
| 212 | + return os; |
| 213 | +} |
| 214 | + |
| 215 | + |
0 commit comments