Submission #402506

#TimeUsernameProblemLanguageResultExecution timeMemory
402506NintsiChkhaidzePoklon (COCI17_poklon)C++14
140 / 140
926 ms43424 KiB
#include <bits/stdc++.h>
#define ll long long
#define s second
#define f first
#define pb push_back
#define left (node<<1),l,(l+r)>>1
#define right ((node<<1)|1),((l+r)>>1)+1,r
using namespace std;
const int N = 500005;
pair<int,int> b[N];
vector <pair<int,int>> v[N];
int a[N],ind[4][N],sgt[4*N],ans[N];
void upd(int node,int l,int r,int ind,int val){
    if (l == r){
        sgt[node] = val;
        return;
    }
    if (ind > ((l+r)>>1)) upd(right,ind,val);
    else upd(left,ind,val);
    sgt[node] = sgt[(node<<1)] + sgt[((node<<1)|1)];
}
ll get(int node,int l,int r,int L,int R){
    if (r < L || R < l) return 0;
    if (L <= l && r <= R) return sgt[node];
    ll x = get(left,L,R),y = get(right,L,R);
    return x+y;
}
int main (){
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
    
    int n,m;
    cin>>n>>m;
    
    for (int i=1;i<=n;i++)
        cin>>b[i].f,b[i].s=i;

    sort(b+1,b+n+1);
    a[b[1].s] = 1;
    for (int i=2;i<=n;i++){
        a[b[i].s] = a[b[i - 1].s];
        if (b[i].f > b[i-1].f) a[b[i].s]++;
    }
    
    for (int i=1;i<=m;i++){
        int l,r;
        cin>>l>>r;
        v[l].pb({r,i});
    }
    
    for (int i=n;i>=1;i--){
        if (ind[3][a[i]]) upd(1,1,n,ind[3][a[i]],0);
        if (ind[2][a[i]]) upd(1,1,n,ind[2][a[i]],-1);
        if (ind[1][a[i]]) upd(1,1,n,ind[1][a[i]],1);
        
        for (int j=0;j<v[i].size();j++){
            int r = v[i][j].f,ind = v[i][j].s;
            ans[ind] = get(1,1,n,i,r);
        }
        
        ind[3][a[i]] = ind[2][a[i]];
        ind[2][a[i]] = ind[1][a[i]];
        ind[1][a[i]] = i;
    }
    
    for (int i=1;i<=m;i++)
        cout<<ans[i]<<"\n";
}

Compilation message (stderr)

poklon.cpp: In function 'int main()':
poklon.cpp:55:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |         for (int j=0;j<v[i].size();j++){
      |                      ~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...