Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define tani_nachi_ke ios_base::sync_with_stdio(false); cin.tie(NULL);
- #define tani_nachi_ke ios_base::sync_with_stdio(false); cin.tie(NULL);
- #define M_PI 3.14159265358979323846
- #define data data_
- #define ff first
- #define ss second
- vector<int> make_data(int val)
- {
- vector<int> temp;
- if(val != -1)
- temp.push_back(val);
- return temp;
- }
- int const N = 600009;
- vector<int> tree[4*N];
- int arr[N];
- vector<int> combine(vector<int> const &left,vector<int> const &right)
- {
- vector<int> temp;
- int n = left.size();
- int m = right.size();
- int l = 0,r = 0;
- while(l < n && r < m)
- {
- if(left[l] < right[r])
- {
- temp.push_back(left[l]);
- l++;
- }
- else
- {
- temp.push_back(right[r]);
- r++;
- }
- }
- while(l < n)
- {
- temp.push_back(left[l]);
- l++;
- }
- while(r < m)
- {
- temp.push_back(right[r]);
- r++;
- }
- return temp;
- }
- void build(int id,int l,int r)
- {
- if(l==r)tree[id]=make_data(arr[l]);
- else
- {
- int mid=(l+r)/2;
- build(2*id,l,mid);
- build(2*id+1,mid+1,r);
- tree[id]=combine(tree[2*id],tree[2*id+1]);
- }
- }
- void update(int id,int l,int r,int pos,int val)
- {
- if(l==r)
- {
- tree[id]=make_data(val);
- }
- else
- {
- int mid=(l+r)/2;
- if(pos<=mid)update(2*id,l,mid,pos,val);
- else update(2*id+1,mid+1,r,pos,val);
- tree[id]=combine(tree[2*id],tree[2*id+1]);
- }
- }
- int ans = 0;
- int query(int v, int tl, int tr, int l, int r, int x,int y) {
- if (l > r)
- return 0;
- if (l == tl && r == tr) {
- return upper_bound(tree[v].begin(),tree[v].end(),y) -
- lower_bound(tree[v].begin(),tree[v].end(),x);
- }
- int tm = (tl + tr) / 2;
- return query(v*2, tl, tm, l, min(r, tm), x, y)+
- query(v*2+1, tm+1, tr, max(l, tm+1), r, x, y);
- }
- int main()
- {
- clock_t begin = clock();
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- tani_nachi_ke
- // build(1,1,n);
- //querry(1,1,n,aa,bb);
- //update(1,1,n,pos,new_value); start array indexing from 1
- vector<vector<int>> bin;
- int q;
- cin >> q;
- int n = 0;
- while(q--)
- {
- int id;
- cin >> id;
- if(id == 1)
- {
- cin >> arr[++n];
- }
- else
- {
- vector<int> temp(4);
- temp[0] = id;
- cin >> temp[1] >> temp[2] >> temp[3];
- bin.push_back(temp);
- }
- }
- build(1,1,n);
- for(auto u : bin)
- {
- ans = 0;
- if(u[0] == 2)
- {
- cout << query(1,1,n,u[1],u[2],-1,u[3]-1) << '\n';
- // cout << ans << '\n';
- }
- else if(u[0] == 3)
- {
- cout << query(1,1,n,u[1],u[2],u[3],u[3]) << '\n';
- // cout << ans << '\n';
- }
- else
- {
- cout << query(1,1,n,u[1],u[2],u[3]+1,INT_MAX) << '\n';
- // cout << ans << '\n';
- }
- }
- #ifndef ONLINE_JUDGE
- clock_t end = clock();
- cout<<endl<<double(end - begin) / CLOCKS_PER_SEC*1000<<" ms";
- #endif
- return 0;
- }
Add Comment
Please, Sign In to add comment