Submissions of online judge(1021)
问题描述
An online judge is a system to test programs in programming contests. The system can compile and execute codes, and test them with pre-constructed data. Submitted code may be run with restrictions, including time limit, memory limit, security restriction and so on. The output of the code will be captured by the system, and compared with the standard output. The system will then return the result.
Always, Online Judge systems also have a status page where a list of these results is shown. For example, http://acm.swust.edu.cn/ is an Online Judge of SWUST. A following list can be found in the system. As it shows, every submission has a Run ID and User ID. The Run ID is an incrementing sequence from 1 to N, with which a User ID is associated.
Mr. Yang wants to know, from Run ID L to R, how many different Users there are?
输入
The first line is an integer T (1 ≤ T ≤ 10), which stands for the number of test cases you need to solve. For each case, the input format will be like this
Line 1: N (1 ≤ N ≤ 100,000) indicating the number of submissions.
Line 2: N integers U1, U2…UN (0 ≤ Ui ≤ 1000,000,000). Ui indicates the User ID of the i-th (Run ID) submission.( For the sake of simplicity, User ID is digital)
Line 3: Q (1 ≤ Q ≤ 100,000), the number of Queries.
Next Q lines: each line contains 2 integers L, R representing a Query (1 ≤ L ≤ R ≤ N). Interval contains the endpoint.
输出
For each Query, print the number of different Users from L to R in one line.
样例输入
2
3
1 1 4
2
1 2
2 3
5
1 1 2 1 3
3
1 5
2 4
3 5
样例输出
1
2
3
2
3
貌似改编自HDU、好题、离线处理
也可以不使用map,用离散化+vis标记
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <map> using namespace std; #define ll long long #define INF 0x3f3f3f3f #define N 100010 struct Query{ int id; int l,r; int ans; }q[N]; int n,m; int a[N]; int cnt[N<<2]; map<int,int> mp; bool cmp1(const Query &q1,const Query &q2){return q1.r<q2.r;} bool cmp2(const Query &q1,const Query &q2){return q1.id<q2.id;} void pushup(int rt) { cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1]; } void update(int l,int r,int rt,int pos,int c) { if(l==r){ cnt[rt]=c; return; } int m=(l+r)>>1; if(pos<=m) update(l,m,rt<<1,pos,c); else update(m+1,r,rt<<1|1,pos,c); pushup(rt); } int query(int l,int r,int rt,int L,int R) { if(l==L && r==R){ return cnt[rt]; } int m=(l+r)>>1; if(R<=m) return query(l,m,rt<<1,L,R); else if(L>m) return query(m+1,r,rt<<1|1,L,R); else return query(l,m,rt<<1,L,m)+query(m+1,r,rt<<1|1,m+1,R); } int main() { int T; int i,j; scanf("%d",&T); while(T--) { scanf("%d",&n); mp.clear(); memset(cnt,0,sizeof(cnt)); for(i=1;i<=n;i++) scanf("%d",&a[i]); scanf("%d",&m); for(i=1;i<=m;i++){ scanf("%d%d",&q[i].l,&q[i].r); q[i].id=i; } sort(q+1,q+m+1,cmp1); i=1,j=1; while(j<=m){ for(;i<=q[j].r;i++){ int t=mp[a[i]]; if(!t){ update(1,n,1,i,1); mp[a[i]]=i; } else{ update(1,n,1,t,0); update(1,n,1,i,1); mp[a[i]]=i; } } while(q[j].r==i-1 && j<=m){ q[j].ans=query(1,n,1,q[j].l,q[j].r); j++; } } sort(q+1,q+m+1,cmp2); for(i=1;i<=m;i++) printf("%d ",q[i].ans); } return 0; }