src
stringlengths
95
64.6k
complexity
stringclasses
7 values
problem
stringlengths
6
50
from
stringclasses
1 value
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ankur */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { String[] str; long mod = (long) 1e9 + 7; long[][] dp; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); str = new String[n]; dp = new long[n + 2][n + 2]; for (int i = 0; i < dp.length; i++) { Arrays.fill(dp[i], -1); } for (int i = 0; i < n; i++) { str[i] = in.readString(); } if (str[0].charAt(0) == 'f') { out.print(solve(1, 1)); } else { out.print(solve(1, 0)); } } long solve(int n, int horiz) { if (horiz < 0) return 0; if (n >= str.length - 1) { return 1; } if (dp[n][horiz] != -1) { return dp[n][horiz]; } if (str[n].charAt(0) == 'f') { return dp[n][horiz] = solve(n + 1, horiz + 1); } else { long ans1 = solve(n, horiz - 1); //System.out.println(ans1+" "+n+" egsvd"+horiz); ans1 += solve(n + 1, horiz); //System.out.println(ans1+" "+n+" "+horiz); ans1 = ans1 % mod; return dp[n][horiz] = ans1; } } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar; private int snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { //*-*------clare------ if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ZYCSwing */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { private static final int MOD = (int) 1e9 + 7; private static final int N = 5000; public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int[] dp = new int[N]; Arrays.fill(dp, 0); dp[0] = 1; String pre = null, ch; for (int i = 0; i < n; ++i) { ch = in.next(); if (i > 0) { if (pre.equals("s")) { int j = N - 1; while (dp[j] == 0) { --j; } long sum = 0; for (; j >= 0; --j) { sum += dp[j]; sum %= MOD; dp[j] = (int) sum; } } else { for (int k = N - 1; k > 0; --k) { dp[k] = dp[k - 1]; } dp[0] = 0; } } pre = ch; } long sum = 0; for (int i = 0; i < N; ++i) { sum += dp[i]; sum %= MOD; } out.println(sum); } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.util.*; import java.io.*; public class p3sol{ static char[] c; static int[][] dp; static int mod = (int)1e9 + 7; public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); c = new char[n]; for(int i = 0; i < n; i++) c[i] = br.readLine().charAt(0); dp = new int[n + 1][n + 1]; dp[0][0] = 1; for(int i = 0; i < n - 1; i++){ if(c[i] == 's'){ int prev = 0; for(int j = i; j >= 0; j--){ prev += dp[i][j]; prev %= mod; dp[i + 1][j] += prev; dp[i + 1][j] %= mod; } } else{ for(int j = 1; j <= n; j++){ dp[i + 1][j] += dp[i][j - 1]; dp[i + 1][j] %= mod; } } } int ans = 0; for(int i = 0; i < n; i++){ ans += dp[n - 1][i]; ans %= mod; } // print(dp); System.out.println(ans); br.close(); } public static void print(int[][] a){ for(int i = 0; i < a.length; i++){ for(int j = 0; j < a[0].length; j++) System.out.print(a[i][j] + " "); System.out.println(""); } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { int mod = 1000000007; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.readInt(); int[][] dp = new int[n + 1][5002]; char[] a = new char[n]; for (int i = 0; i < n; i++) a[i] = in.readCharacter(); for (int i = 0; i < dp[n].length; i++) dp[n][i] = 1; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < n; j++) { if (a[i] == 's') { if (j > 0) dp[i][j] = dp[i][j - 1]; dp[i][j] = (int) ((dp[i][j] + (long) dp[i + 1][j]) % mod); } else { if (j > 0) dp[i][j] = dp[i][j - 1]; dp[i][j] = (int) ((dp[i][j] + (long) dp[i + 1][j + 1] - (long) dp[i + 1][j] + mod) % mod); } } } out.println(dp[0][0]); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.util.*; public class GFG { static int count=0; public static void main (String[] args) { Scanner ob=new Scanner(System.in); int n; long MOD=(long)(1e9+7); int f=0,s=0; n=ob.nextInt(); long dp[][]=new long[n+2][n+2]; dp[0][1]=1; char ch='s'; char p; for(int i=1;i<=n;i++) { p=ch; ch=ob.next().charAt(0); if(p=='f') { for(int j=1;j<=n;j++) dp[i][j+1]=dp[i-1][j]; } else { for(int j=n;j>0;j--) { dp[i][j]=(dp[i][j+1]+dp[i-1][j])%MOD; } } } long ans=0; for(int i=1;i<=n;i++) { ans=(ans+dp[n][i])%MOD; } System.out.println(ans); } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.awt.*; import java.io.*; import java.math.*; import java.util.*; public class TaskC { public static void main(String[] args) { new TaskC().run(); } void solve(){ int n = in.nextInt(); String s[] = new String[n]; for(int i = 0; i < n; i++) s[i] = in.next(); if(s[n-1].compareTo("f") == 0){ out.println(0); return; } int dp[] = new int[n+2]; int mod = 1000*1000*1000+7; dp[0] = 1; for(int i = n-1; i >= 0; i--){ if(s[i].compareTo("s") == 0){ int ss = 0; for(int j = 0; j <= n; j++){ ss += dp[j]; if(ss>=mod) ss -= mod; dp[j] = ss; } }else{ for(int j = 0; j < n;j++){ dp[j] = dp[j+1]; } dp[n] = 0; } } out.println(dp[0]); } FastScanner in; PrintWriter out; void run() { in = new FastScanner(System.in); out = new PrintWriter(System.out); int tests = 1;//in.nextInt(); while(tests > 0){ solve(); tests--; } out.close(); } class Pair implements Comparable<Pair>{ Integer x, y; public Pair(int x, int y){ this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if(x.compareTo(o.x) == 0) return y.compareTo(o.y); return x.compareTo(o.x); } } class FastScanner { StringTokenizer st; BufferedReader bf; public FastScanner(InputStream is) { bf = new BufferedReader(new InputStreamReader(is)); } public FastScanner(File f){ try{ bf = new BufferedReader(new FileReader(f)); } catch(IOException ex){ ex.printStackTrace(); } } String next(){ while(st == null || !st.hasMoreTokens()){ try{ st = new StringTokenizer(bf.readLine()); } catch(Exception ex){ ex.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } int[] readArray(int n){ int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = nextInt(); return a; } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.*; import java.util.*; import java.io.IOException; import java.io.InputStream; public class Main { public static void main(String[] args) { File file = new File("in.txt"); File fileOut = new File("out.txt"); InputStream inputStream = null; OutputStream outputStream = null; // try {inputStream= new FileInputStream(file);} catch (FileNotFoundException ex){return;}; // try {outputStream= new FileOutputStream(fileOut);} catch (FileNotFoundException ex){return;}; inputStream = System.in; outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } } class Task { private final int mod = 1000000007; public void solve(int testNumber, InputReader in, PrintWriter out) { Integer n = in.nextInt(); List<Character> comm = new ArrayList<>(n); for(int i=0; i<n; i++){ comm.add(in.next().charAt(0)); } long[][] dp = new long[n][n]; dp[0][0] = 1; for(int i=1; i<n; i++){ Character lastComm = comm.get(i-1); if(lastComm.equals('f')){ dp[i][0] = 0; for(int j=1; j<n; j++){ dp[i][j] = dp[i-1][j-1]; } } else{ Long suffixSum = dp[i-1][n-1]; for(int j=n-1; j>=0; j--){ dp[i][j] = suffixSum; if(j>0) { suffixSum += dp[i - 1][j - 1] % mod; } } } } Long finalSum = 0L; for(int i=0; i<n; i++){ finalSum += dp[n-1][i] % mod; } out.println(finalSum % mod); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine(){ try { return reader.readLine(); } catch (IOException e){ throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } class Pair<F, S> { public final F first; public final S second; public Pair(F first, S second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (!(o instanceof Pair)) { return false; } Pair<?, ?> p = (Pair<?, ?>) o; return Objects.equals(p.first, first) && Objects.equals(p.second, second); } @Override public int hashCode() { return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode()); } @Override public String toString() { return "(" + first + ", " + second + ')'; } } class IntPair extends Pair<Integer, Integer>{ public IntPair(Integer first, Integer second){ super(first, second); } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; TaskC.InputReader in = new TaskC.InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.Solve(in, out); out.close(); } static class TaskC { private long mod = 1_000_000_007; private int n; private boolean[] s; public void Solve(InputReader in, PrintWriter out) { n = in.NextInt(); s = new boolean[n]; for (int i = 0; i < n; i++) { String ss = in.Next(); s[i] = ss.charAt(0) == 'f'; } if (s[n - 1]) { out.println(0); return; } long[] dpSum = new long[n + 1], lastDpSum = new long[n + 1]; for (int i = 0; i <= n; i++) { lastDpSum[i] = i + 1; } for (int i = n - 2; i >= 0; i--) { for (int j = 0; j <= i; j++) { if (!s[i]) { dpSum[j] = lastDpSum[j]; } else { dpSum[j] = lastDpSum[j + 1] - lastDpSum[j]; } if (j != 0) { dpSum[j] += dpSum[j - 1]; } dpSum[j] %= mod; while (dpSum[j] < 0) dpSum[j] += mod; } long[] temp = dpSum; dpSum = lastDpSum; lastDpSum = temp; } out.println(lastDpSum[0]); } public static int GetMax(int[] ar) { int max = Integer.MIN_VALUE; for (int a : ar) { max = Math.max(max, a); } return max; } public static int GetMin(int[] ar) { int min = Integer.MAX_VALUE; for (int a : ar) { min = Math.min(min, a); } return min; } public static long GetSum(int[] ar) { long s = 0; for (int a : ar) s += a; return s; } public static int[] GetCount(int[] ar) { return GetCount(ar, GetMax(ar)); } public static int[] GetCount(int[] ar, int maxValue) { int[] dp = new int[maxValue + 1]; for (int a : ar) { dp[a]++; } return dp; } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String Next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f,"); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int NextInt() { return Integer.parseInt(Next()); } public long NextLong() { return Long.parseLong(Next()); } public double NextDouble() { return Double.parseDouble(Next()); } public int[] NextIntArray(int n) { return NextIntArray(n, 0); } public int[] NextIntArray(int n, int offset) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = NextInt() - offset; } return a; } public int[][] NextIntMatrix(int n, int m) { return NextIntMatrix(n, m, 0); } public int[][] NextIntMatrix(int n, int m, int offset) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = NextInt() - offset; } } return a; } } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.PrintStream; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Wolfgang Beyer */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long MOD = 1000000007; long[] current = new long[n + 3]; //long[] sum = new long[n + 3]; current[0] = 1; for (int i = 0; i < n - 1; i++) { String s = in.next(); if (s.equals("f")) { for (int j = i + 1; j > 0; j--) { current[j] = current[j - 1]; current[j] %= MOD; } current[0] = 0; } else { for (int j = i + 1; j >= 0; j--) { //sum[j] = sum[j + 1] + current[j]; current[j] = current[j + 1] + current[j]; current[j] %= MOD; } //for (int j = 0; j <= i + 1; j++) { // current[j] = //} } } long result = 0; for (int i = 0; i <= n; i++) { result += current[i]; result %= MOD; } out.println(result); } } static class InputReader { private static BufferedReader in; private static StringTokenizer tok; public InputReader(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); } public int nextInt() { return Integer.parseInt(next()); } public String next() { try { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter } } catch (IOException ex) { System.err.println("An IOException was caught :" + ex.getMessage()); } return tok.nextToken(); } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; /* */ public class C455 { static int N; static final int mod = 1_000_000_007; static int[][] memo; static int[] list; public static void main(String[] args) { FS scan = new FS(System.in); N = scan.nextInt(); list = new int[N]; for(int i=0;i<N;i++) { list[i] = scan.next().equals("s")?0:1; } if(list[N-1] == 1) { System.out.println(0); return; } memo = new int[N+1][N+2]; Arrays.fill(memo[N], 1); int[] sum = new int[N+2]; for(int i=N-1;i>=0;i--) { sum[0] = memo[i+1][0]; for(int j=1;j<sum.length;j++) { sum[j] = sum[j-1] + memo[i+1][j]; sum[j] %= mod; } for(int j=0;j<=N;j++) { if (list[i]==1 && (i==0 || list[i-1]==1)) memo[i][j] = memo[i+1][j+1]; else if(i==0 || list[i-1] == 1) memo[i][j] = memo[i+1][j]; else if (list[i]==1){ // for(int k=0;k<=j;k++) { // memo[i][j] += memo[i+1][k+1]; // } memo[i][j] = sum[j+1] - sum[0] + mod; memo[i][j] %= mod; } else if (list[i]==0) { memo[i][j] = sum[j]; } } } // for(int i=0;i<=N;i++) { // for(int j=0;j<=N;j++) { // System.out.print(memo[i][j]+" "); // } // System.out.println(); // } // System.out.println(); System.out.println(memo[0][0]); } private static class FS { BufferedReader br; StringTokenizer st; public FS(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next(){ while(st==null||!st.hasMoreElements()){ try{st = new StringTokenizer(br.readLine());} catch(IOException e){e.printStackTrace();} } return st.nextToken(); } int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() { return Double.parseDouble(next());} } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.*; import java.math.*; import java.util.*; public class Main { private static final int MAX = 5000 + 10,mod = 1000000007; private static char [] S; private static int n; private static Integer [] [] dp = new Integer[MAX][MAX]; private static int solve(int pos,int open){ if(pos == n) return (open == 0) ? 1 : 0; if (dp[pos][open] != null) return dp[pos][open]; int res = 0; if (S[pos] == 's') { res = solve(pos + 1,open); if (open > 0) res += solve(pos,open - 1); if (res >= mod) res -= mod; } else { res = solve(pos+1,open + 1); } return dp[pos][open] = res; } public static void main(String[] args) throws Exception{ IO io = new IO(null,null); n = io.getNextInt(); S = new char[n]; for (int i = 0;i < n;i++) S[i] = io.getNext().charAt(0); io.println(solve(0,0)); io.close(); } } class IO{ private BufferedReader br; private StringTokenizer st; private PrintWriter writer; private String inputFile,outputFile; public boolean hasMore() throws IOException{ if(st != null && st.hasMoreTokens()) return true; if(br != null && br.ready()) return true; return false; } public String getNext() throws FileNotFoundException, IOException{ while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String getNextLine() throws FileNotFoundException, IOException{ return br.readLine().trim(); } public int getNextInt() throws FileNotFoundException, IOException{ return Integer.parseInt(getNext()); } public long getNextLong() throws FileNotFoundException, IOException{ return Long.parseLong(getNext()); } public void print(double x,int num_digits) throws IOException{ writer.printf("%." + num_digits + "f" ,x); } public void println(double x,int num_digits) throws IOException{ writer.printf("%." + num_digits + "f\n" ,x); } public void print(Object o) throws IOException{ writer.print(o.toString()); } public void println(Object o) throws IOException{ writer.println(o.toString()); } public IO(String x,String y) throws FileNotFoundException, IOException{ inputFile = x; outputFile = y; if(x != null) br = new BufferedReader(new FileReader(inputFile)); else br = new BufferedReader(new InputStreamReader(System.in)); if(y != null) writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile))); else writer = new PrintWriter(new OutputStreamWriter(System.out)); } protected void close() throws IOException{ br.close(); writer.close(); } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Created by Jarek on 2017-12-30. */ public class PhytonIndenter { private static Scanner scanner = new Scanner(System.in); private static int lineCount; private static String[] commands; public static void main(String args[]) { lineCount = scanner.nextInt(); scanner.nextLine(); commands = new String[lineCount]; for (int i = 0; i < lineCount; i++) { commands[i] = scanner.nextLine(); } resolveWithDP(); } private static void resolveWithDP() { long dp[][] = new long[lineCount][5002]; long mod = 1000000007; dp[0][0] = 1; for (int i = 1; i < lineCount; i++) { if ("f".equalsIgnoreCase(commands[i - 1])) { dp[i][0] = 0; for (int j = 1; j <= i; j++) { dp[i][j] = dp[i - 1][j - 1]; } } else { long sum = 0; for (int j = i - 1; j >= 0; j--) { sum += dp[i - 1][j] % mod; dp[i][j] = sum; } } } long result = 0; for (int i = 0; i < lineCount; i++) { result += dp[lineCount-1][i]%mod; result %= mod; } System.out.println(result%mod); } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.util.*; import java.io.*; /* spar5h */ public class codeforces implements Runnable { final static long mod = (long)1e9 + 7; public void run() { InputReader s = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = s.nextInt(); char[] c = new char[n]; for(int i = 0; i < n; i++) c[i] = s.next().charAt(0); //index + depth/indentation long[][] dp = new long[n][n]; dp[0][0] = 1; for(int i = 0; i < n - 1; i++) { //c[i] = f implies that indentation will always be increased by one in i + 1 if(c[i] == 'f') { for(int j = 1; j < n; j++) dp[i + 1][j] = dp[i][j - 1]; } //c[i] = s implies that value of i + 1 can include any previous value of equal or higher indentation else { dp[i + 1][n - 1] = dp[i][n - 1]; for(int j = n - 2; j >= 0; j--) dp[i + 1][j] = (dp[i + 1][j + 1] + dp[i][j]) % mod; } } long res = 0; for(int i = 0; i < n; i++) res = (res + dp[n - 1][i]) % mod; w.println(res); w.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new codeforces(),"codeforces",1<<26).start(); } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { static final long MODULO = (long) (1e9 + 7); public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); long[][] dp = new long[n + 100][n + 100]; dp[0][0] = 1; for (int i = 0; i < n; ++i) { char current = in.nextCharacter(); if (current == 'f') { for (int j = 0; j < n; ++j) { dp[i + 1][j + 1] += dp[i][j]; dp[i + 1][j + 1] %= MODULO; } } else { long runningSum = 0; for (int j = n; j >= 0; --j) { runningSum += dp[i][j]; runningSum %= MODULO; dp[i + 1][j] += runningSum; dp[i + 1][j] %= MODULO; } } } out.println(dp[n][0]); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public char nextCharacter() { int c = pread(); while (isSpaceChar(c)) c = pread(); return (char) c; } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.util.Collections; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.InputStream; public class C909 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int ar[] = new int[n+1]; int count = 1; ar[0] = 1; for(int i=0;i<n;i++){ char c = in.next().charAt(0); if(c == 'f') count++; else{ for(int j=1;j<count;j++){ ar[j] = (ar[j] + ar[j-1])% (int)(1e9+7); } } } out.println(ar[count-1]); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.*; public class Main { public static void main(String[] args) { solve(System.in,System.out); } static void solve(InputStream inStream, PrintStream printStream) { Scanner in = new Scanner(inStream); int n = in.nextInt(); long[] sums = new long[n]; for (int i = 1; i < n; i++) { sums[i]=0; } sums[0]=1; long mod = 1000000007; for (int i = 0; i < n; i++) { if (in.next().equals("f") ) { for (int j = n-1; j > 0 ; j--) { sums[j]=sums[j-1]; } sums[0]=0; } else { for (int j = n-2; j >= 0 ; j--) { sums[j] += sums[j+1]; if (sums[j]>=mod) { sums[j]-=mod; } } } } // long finalSum = 0; // for (int i = 0; i < n; i++) { // finalSum+=sums[i]; // } printStream.println(sums[0]); } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.util.*; import java.io.*; public class C { static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int n = in.nextInt(); int[] sol = new int[n]; sol[0] = 1; int mod = 1000000007; int maxind = 0; boolean f = true; for (int i = 0; i < n; i++) { if (!f) { //int accum = sol[0]; for (int j = 1; j <= maxind; j++) { sol[j] += sol[j-1]; sol[j] %= mod; } //out.println(Arrays.toString(sol)); } if (in.next().equals("f")) { maxind++; f = true; } else { f = false; } } int ans = 0; for (int i = 0; i <= maxind; i++) { ans += sol[i]; ans %= mod; } out.println(ans); finish(); } public static void finish() { out.close(); in.close(); System.exit(0); } static class InputReader implements Iterator<String>, Closeable { // Fast input reader. Based on Kattio.java from open.kattis.com // but has method names to match Scanner private BufferedReader r; private String line; private StringTokenizer st; private String token; public InputReader(InputStream i) { r = new BufferedReader(new InputStreamReader(i)); } public boolean hasNext() { return peekToken() != null; } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public String next() { return nextToken(); } public String nextLine() { try { line = r.readLine(); } catch (IOException e) { line = null; } token = null; st = null; return line; } public void close() { try { r.close(); } catch (IOException e) { } } private String peekToken() { if (token == null) try { while (st == null || !st.hasMoreTokens()) { line = r.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } private String nextToken() { String ans = peekToken(); token = null; return ans; } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author EndUser */ public class R455D2PC { public static void main(String[] args) { final int MAX = 5000; final int MODULO = 1000000007; Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); int pre = 0; int size = 0; int[] block = new int[MAX]; for (int i = 0; i < n; i++) { String command = in.nextLine(); if (command.startsWith("s")) { block[size++] = pre; pre = 0; } else { pre++; } } if (pre != 0) { System.out.println(0); return; } int[][] result = new int[2][MAX + 1]; int currentMax = 0; int preIndex = 0; result[preIndex][0] = 1; for (int i = 1; i < size; i++) { int currentIndex = preIndex ^ 1; int j = block[i - 1]; for (int k = currentMax; k >= 0; k--) { result[currentIndex][k + j] = (result[currentIndex][k + j + 1] + result[preIndex][k]) % MODULO; } for (int k = j - 1; k >= 0; k--) { result[currentIndex][k] = result[currentIndex][j]; } currentMax += j; preIndex = currentIndex; // for (int k = 0; k <= currentMax; k++) { // System.out.print(result[preIndex][k] + " "); // // } // System.out.println(""); } int sum = 0; for (int i = 0; i <= currentMax; i++) { sum = (sum + result[preIndex][i]) % MODULO; } System.out.println(sum); } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class C { static int [][] memo; static int n; static char [] c; static int mod = (int)1e9+7; static int dp(int ind, int loops){ if(ind == n) return loops == 0?1:0; if(memo[ind][loops] != -1) return memo[ind][loops]; long ans = 0; if(c[ind] == 's'){ ans = (ans + dp(ind+1, loops))%mod; if(loops > 0) ans = (ans + dp(ind, loops-1))%mod; } else{ ans = (ans + dp(ind+1, loops+1))%mod; } return memo[ind][loops] = (int) ans; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); n = sc.nextInt(); memo = new int[n+1][n+1]; for(int [] i:memo) Arrays.fill(i, -1); c = new char[n]; for (int i = 0; i < c.length; i++) { c[i] = sc.next().charAt(0); } out.println(dp(0,0)); out.flush(); out.close(); } static class Scanner { BufferedReader bf; StringTokenizer st; public Scanner(InputStream i) { bf = new BufferedReader(new InputStreamReader(i)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(bf.readLine()); return st.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; import java.util.concurrent.*; public final class py_indent { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static FastScanner sc=new FastScanner(br); static PrintWriter out=new PrintWriter(System.out); static Random rnd=new Random(); static int maxn=(int)(5e3+5); static long mod=(long)(1e9+7); static int add(long a,long b) { long ret=(a+b); if(ret>=mod) { ret%=mod; } return (int)ret; } public static void main(String args[]) throws Exception { int n=sc.nextInt();char[] a=new char[n+1];a[0]='s'; for(int i=1;i<=n;i++) { a[i]=sc.next().charAt(0); } int[][] dp=new int[n+1][maxn],sum=new int[n+1][maxn];dp[0][0]=1; sum[0][0]=1; for(int i=1;i<maxn;i++) { sum[0][i]=add(sum[0][i],sum[0][i-1]); } for(int i=1;i<=n;i++) { if(a[i]=='f') { continue; } int curr=0,idx=0; for(int j=i-1;j>=0;j--) { if(a[j]=='s') { idx=j;break; } else { curr++; } } for(int j=0;j<maxn;j++) { int up=Math.max(0,j-curr); long now=(sum[idx][maxn-1]-(up==0?0:sum[idx][up-1])); now=add(now,mod); dp[i][j]=add(dp[i][j],now); } sum[i][0]=dp[i][0]; for(int j=1;j<maxn;j++) { sum[i][j]=add(dp[i][j],sum[i][j-1]); } } //out.println(dp[2][0]+" "+dp[2][1]+" "+dp[2][2]); out.println(dp[n][0]);out.close(); } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public String next() throws Exception { return nextToken().toString(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
quadratic
909_C. Python Indentation
CODEFORCES
//package round455; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class C { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); int mod = 1000000007; long[] dp = new long[5005]; dp[0] = 1; for(int i = 0;i < n;i++){ char c = nc(); if(c == 's'){ if(i < n-1){ for(int j = 5003;j >= 0;j--){ dp[j] += dp[j+1]; if(dp[j] >= mod)dp[j] -= mod; } } }else{ for(int j = 5003;j >= 0;j--){ dp[j+1] = dp[j]; } dp[0] = 0; } } long ans = 0; for(int i = 0;i < 5005;i++)ans += dp[i]; out.println(ans % mod); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new C().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class C { public static void main(String[] args){ try(Scanner sc = new Scanner(System.in)){ final int N = sc.nextInt(); String[] ins = new String[N]; for(int i = 0; i < N; i++){ ins[i] = sc.next(); } final long MOD = 1000000007; long[] DP = new long[N]; long[] nextDP = new long[N]; DP[0] = 1; for(int i = 1; i < N; i++){ Arrays.fill(nextDP, 0); if("f".equals(ins[i - 1])){ for(int j = 0; j < N - 1; j++){ nextDP[j + 1] += DP[j]; nextDP[j + 1] %= MOD; } }else{ for(int j = N - 1; j >= 0; j--){ nextDP[j] += DP[j]; nextDP[j] %= MOD; if(j < N - 1){ nextDP[j] += nextDP[j + 1]; nextDP[j] %= MOD; } } } { long[] tmp = DP; DP = nextDP; nextDP = tmp; } } long answer = 0; for(int i = 0; i < N; i++){ answer += DP[i]; answer %= MOD; } System.out.println(answer); } } public static class Scanner implements Closeable { private BufferedReader br; private StringTokenizer tok; public Scanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } private void getLine() { try { while (!hasNext()) { tok = new StringTokenizer(br.readLine()); } } catch (IOException e) { /* ignore */ } } private boolean hasNext() { return tok != null && tok.hasMoreTokens(); } public String next() { getLine(); return tok.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public void close() { try { br.close(); } catch (IOException e) { /* ignore */ } } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.util.* ; import java.io.* ; public class PythonIndentation { public static void main(String args[]) { Scanner in = new Scanner(System.in) ; int n = in.nextInt() ; boolean[] lst = new boolean[n] ; for(int i=0;i<n;i++) { lst[i] = (in.next().equals("s"))?false:true ; } System.out.println(dp(lst)) ; } static void arrayPrinter(int[][] dp) { System.out.println(":::") ; for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { System.out.print(dp[i][j]+" ") ; } System.out.println() ; } } static int dp(boolean[] lst) {//false in lst means an "s" (simple statement), and true a "f"(for loop) int[][] dp = new int[2][lst.length] ; dp[0][0] = 1 ; for(int i=1;i<lst.length;i++) { // arrayPrinter(dp) ; for(int j=0;j<lst.length;j++) { if(lst[i-1])//(i-1)st statement is a for loop { if(j==0) dp[i%2][j] = 0 ; else dp[i%2][j] = dp[(i-1)%2][j-1] ; } else//i-1 st statement is a simple statement { if(j==0) { int temp = 0 ; for(int k=0;k<lst.length;k++) temp = (temp+dp[(i-1)%2][k])%1000000007 ; dp[i%2][j] = temp ; } else dp[i%2][j] = (dp[i%2][j-1]-dp[(i-1)%2][j-1])%1000000007 ; } } } int ans = 0 ; for(int i=0;i<lst.length;i++) { ans = (ans + dp[(lst.length-1)%2][i])%1000000007 ; } if(ans<0) ans = ans + 1000000007 ; // arrayPrinter(dp) ; return ans ; } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author phantom11 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { long MOD = (long) 1e9 + 7; long[][] dp; int[] a; public void solve(int testNumber, InputReader in, OutputWriter out) { int N = in.nextInt(); a = new int[N]; dp = new long[N][N]; int i; for (i = 0; i < N; i++) { char c = in.nextChar(); if (c == 'f') { a[i] = 1; } } out.printLine(iterative()); // for(i=0;i<=N;i++) { // Arrays.fill(dp[i], -1); // } // out.printLine(recur(0, 0)); } public long iterative() { int i, j, N = a.length, lastLevel = 0; dp[0][0] = 1; for (i = 1; i < N; i++) { if (a[i - 1] == 1) { lastLevel++; for (j = 1; j <= lastLevel; j++) { dp[i][j] = dp[i - 1][j - 1]; } } else { dp[i][N - 1] = dp[i - 1][N - 1]; for (j = N - 2; j >= 0; j--) { dp[i][j] = (dp[i][j + 1] + dp[i - 1][j]) % MOD; } } } long ans = 0; for (i = 0; i < N; i++) { ans += dp[N - 1][i]; } return ans % MOD; } } static class InputReader { BufferedReader in; StringTokenizer tokenizer = null; public InputReader(InputStream inputStream) { in = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } catch (IOException e) { return null; } } public int nextInt() { return Integer.parseInt(next()); } public char nextChar() { return next().charAt(0); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.util.Scanner; public class Main { public static void main(String[] args){ long MOD = 1000000007; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[][]dp = new long[n][5010]; char[] program = new char[n]; for(int i = 0; i < n; i++){ program[i] = sc.next().charAt(0); } dp[0][0] = 1; long[] acc = new long[5010]; acc[0] = 1; for(int i = 1 ; i < n; i++){ for(int j = 0; j< 5010; j++){ if(program[i-1] == 'f'){ if(j - 1 >= 0){ dp[i][j] = dp[i-1][j-1]; } }else{ dp[i][j] = acc[j]; } } acc[5009] = dp[i][5009]; for(int j = 5008; j >= 0; j--){ acc[j] = (acc[j + 1] + dp[i][j]) % MOD; } } System.out.println(acc[0]); } }
quadratic
909_C. Python Indentation
CODEFORCES
/* * Institute DA-IICT */ import java.io.*; import java.math.*; import java.util.*; public class C { InputStream in; PrintWriter out; void solve() { int n=ni(); long dp[]=new long[n+1]; dp[0]=1; for (int i=0;i<n;) { i++; if (nc()=='f') { //boolean flag=i==1; i++; int k=1; while (nc()!='s') { i++;k++; } //long dp1[]=Arrays.copyOf(dp, n+1); for (int j=n-1;j>=0;j--) { dp[j]=add(dp[j],dp[j+1]); } for (int j=n;j>=k;j--) { dp[j]=dp[j-k]; } for (int j=0;j<k;j++) dp[j]=0; } else { for (int j=n-1;j>=0;j--) { dp[j]=add(dp[j],dp[j+1]); } } //tr(dp); } long sum=0; for (int i=0;i<=n;i++) sum=add(sum,dp[i]); out.println(sum); } class LazyPropagation { long tree[]; long lazy[]; long A[]; public LazyPropagation(int n,long arr[]) { tree=new long[4*n]; lazy=new long[4*n]; A=arr; buildSum(1,1,n); } public LazyPropagation(int n) { tree=new long[4*n]; lazy=new long[4*n]; } void buildSum(int node,int start,int end) { if (start==end) { tree[node]=A[start]; } else { int mid=(start+end)>>1; buildSum(node*2, start, mid); buildSum(node*2+1, mid+1, end); tree[node]=tree[node*2]+tree[2*node+1]; } } void updateRangeSum(int node, int start, int end,int l,int r,long val) { if (lazy[node]!=0) { tree[node]=add(tree[node],mul((end-start+1),lazy[node])); if (start!=end) { lazy[2*node]=add(lazy[2*node],lazy[node]); lazy[node*2+1]=add(lazy[2*node+1],lazy[node]); } lazy[node]=0; } if (start>end||start>r||end<l) return; if (start>=l&&end<=r) { tree[node]=add(tree[node],mul((end-start+1),val)); if (start!=end) { lazy[2*node]=add(lazy[2*node],val); lazy[node*2+1]=add(lazy[2*node+1],val); } return; } int mid=(start+end)>>1; updateRangeSum(node*2, start, mid, l, r, val); updateRangeSum(node*2+1, mid+1, end, l, r, val); tree[node]=add(tree[node*2],tree[node*2+1]); } long queryRangeSum(int node,int start,int end,int l,int r) { if (start>r||end<l||start>end) return 0; if (lazy[node]!=0) { tree[node]=add(tree[node],mul((end-start+1),lazy[node])); if (start!=end) { lazy[2*node]=add(lazy[2*node],lazy[node]); lazy[node*2+1]=add(lazy[2*node+1],lazy[node]); } lazy[node]=0; } if (start>=l&&end<=r) return tree[node]; int mid=(start+end)>>1; return add(queryRangeSum(node*2, start, mid, l, r),queryRangeSum(node*2+1, mid+1, end, l, r)); } } long mod=(long)1e9+7; long add(long a,long b) { long x=(a+b); while(x>=mod) x-=mod; return x; } long sub(long a,long b) { long x=(a-b); while(x<0) x+=mod; return x; } long mul(long a,long b) { a%=mod; b%=mod; long x=(a*b); return x%mod; } void run() throws Exception { String INPUT = "C:/Users/ayubs/Desktop/input.txt"; in = oj ? System.in : new FileInputStream(INPUT); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new C().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = in.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean inSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && inSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(inSpaceChar(b))) { // when nextLine, (inSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(inSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.*; import java.util.*; public class A{ InputStream is; PrintWriter out; String INPUT = ""; public void solve(){ int n=ni(); char[] arr=new char[n]; for(int i=0;i<n;i++){ arr[i]=ns().charAt(0); } long mod=1000000007; long[][] memo=new long[n][n]; memo[0][0]=1L; int k=0; for(int i=1; i<n; i++){ if( (arr[i]=='f' && arr[i-1]=='s') || (arr[i]=='s' && arr[i-1]=='s') ){ long sum=0; for(int j=k; j>=0; j--){ sum=(sum+(memo[i-1][j]%mod))%mod; memo[i][j]=sum; } } else{ k+=1; for(int j=1; j<=k; j++){ memo[i][j] = memo[i-1][j-1] % mod; } } } //print(n, memo); long sum=0; for(int i=0;i<=k;i++){ sum=(sum+(memo[n-1][i])%mod)%mod; } out.println(sum); } void print(int n, long[][] memo){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ out.print(memo[i][j]+" "); } out.println(); } } void run(){ is = new DataInputStream(System.in); out = new PrintWriter(System.out); int t=1;while(t-->0)solve(); out.flush(); } public static void main(String[] args)throws Exception{new A().run();} //Fast I/O code is copied from uwi code. private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte(){ if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns(){ int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n){ char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m){ char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n){ int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni(){ int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl(){ long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } static int i(long x){return (int)Math.round(x);} static class Pair implements Comparable<Pair>{ long fs,sc; Pair(long a,long b){ fs=a;sc=b; } public int compareTo(Pair p){ if(this.fs>p.fs)return 1; else if(this.fs<p.fs)return -1; else{ return i(this.sc-p.sc); } //return i(this.sc-p.sc); } public String toString(){ return "("+fs+","+sc+")"; } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class C { static int N; static char[] a; static int[][] memo; static int[] ind; static final int MOD = (int) 1e9 + 7; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); N = sc.nextInt(); a = new char[N]; for(int i = 0; i < N; i++) a[i] = sc.nextChar(); if(N == 1){out.println(1); out.flush(); return;} memo = new int[N][N + 5]; for(int[] a : memo) Arrays.fill(a, -1); out.println(dp(0, 1)); out.flush(); out.close(); } static int dp(int i, int ind) { if(i == N - 1) return a[i - 1] == 'f'? 1 : ind; if(i == 0) return dp(i + 1, a[i] == 's'? 1 : 2); if(memo[i][ind] != -1) return memo[i][ind]; int ans = 0; if(a[i - 1] == 'f') ans = dp(i + 1, a[i] == 's'? ind : ind + 1); else { if(ind > 1) ans = (ans + dp(i, ind -1)) % MOD; ans = (ans + dp(i + 1, a[i] == 's'? ind : ind + 1)) % MOD; } return memo[i][ind] = ans; } static class Scanner { StringTokenizer st; BufferedReader br; Scanner(InputStream system) {br = new BufferedReader(new InputStreamReader(system));} Scanner(String file) throws FileNotFoundException {br = new BufferedReader(new FileReader(file));} String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine()throws IOException{return br.readLine();} int nextInt() throws IOException {return Integer.parseInt(next());} double nextDouble() throws IOException {return Double.parseDouble(next());} char nextChar()throws IOException{return next().charAt(0);} Long nextLong()throws IOException{return Long.parseLong(next());} boolean ready() throws IOException{return br.ready();} void waitForInput(){for(long i = 0; i < 3e9; i++);} } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; public class C{ static long mod=1000000007; static int n; static long w[][]; static void MainMethod()throws Exception{ n=reader.nextInt(); w=new long[n+2][n+2]; for (int i=0;i<=(n+1);i++) Arrays.fill(w[i], 0); w[1][0]=1; String prev,next; prev=reader.next(); long s[]=new long[n+2]; for (int i=2;i<=n;i++) { next=reader.next(); if (prev.equals("f")) for (int lv=0;lv<=n;lv++) w[i][lv+1]=w[i-1][lv]; else for (int lv=n;lv>=0;lv--){ w[i][lv]=(w[i-1][lv]+w[i][lv+1])%mod; } prev=next; } long res=0; for (int i=0;i<=n;i++) res=(res+w[n][i])%mod; printer.print(res); } public static void main(String[] args)throws Exception{ MainMethod(); printer.close(); } static void halt(){ printer.close(); System.exit(0); } static PrintWriter printer=new PrintWriter(new OutputStreamWriter(System.out)); static class reader{ static BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer token=new StringTokenizer(""); static String readNextLine() throws Exception{ return bReader.readLine(); } static String next() throws Exception{ while (token.hasMoreTokens()==false){ token=new StringTokenizer(bReader.readLine()); } return token.nextToken(); } static int nextInt()throws Exception{ while (token.hasMoreTokens()==false){ token=new StringTokenizer(bReader.readLine()); } return Integer.parseInt(token.nextToken()); } static long nextLong()throws Exception{ while (token.hasMoreTokens()==false){ token=new StringTokenizer(bReader.readLine()); } return Long.parseLong(token.nextToken()); } static double nextDouble()throws Exception{ while (token.hasMoreTokens()==false){ token=new StringTokenizer(bReader.readLine()); } return Double.parseDouble(token.nextToken()); } } static class MyMathCompute{ static long [][] MatrixMultiplyMatrix(long [][] A, long [][] B, long mod) throws Exception{ int n=A.length, m=B[0].length; int p=A[0].length; int i,j,k; if (B.length!=p) throw new Exception("invalid matrix input"); long [][] res=new long [n][m]; for (i=0;i<n;i++) for (j=0;j<m;j++){ if (A[i].length!=p) throw new Exception("invalid matrix input"); res[i][j]=0; for (k=0;k<p;k++) res[i][j]=(res[i][j]+((A[i][k]*B[k][j])% mod))% mod; } return res; } static double [][] MatrixMultiplyMatrix(double [][] A, double [][] B ) throws Exception{ int n=A.length, m=B[0].length; int p=A[0].length; int i,j,k; if (B.length!=p) throw new Exception("invalid matrix input"); double [][] res=new double [n][m]; for (i=0;i<n;i++) for (j=0;j<m;j++){ if (A[i].length!=p) throw new Exception("invalid matrix input"); res[i][j]=0; for (k=0;k<p;k++) res[i][j]=res[i][j]+(A[i][k]*B[k][j]); } return res; } static long [][] MatrixPow(long [][] A,long n, long mod) throws Exception{ if (n==1) return A; long [][] res=MatrixPow(A, n/2, mod); res=MatrixMultiplyMatrix(res, res, mod); if ((n % 2) == 1) res=MatrixMultiplyMatrix(A,res, mod); return res; } static double [][] MatrixPow(double [][] A,long n) throws Exception{ if (n==1) return A; double[][] res=MatrixPow(A, n/2); res=MatrixMultiplyMatrix(res, res); if ((n % 2) == 1) res=MatrixMultiplyMatrix(A,res); return res; } static long pow(long a,long n,long mod){ a= a % mod; if (n==0) return 1; long k=pow(a,n/2,mod); if ((n % 2)==0) return ((k*k)%mod); else return (((k*k) % mod)*a) % mod; } static double pow(double a,long n){ if (n==0) return 1; double k=pow(a,n/2); if ((n % 2)==0) return (k*k); else return (((k*k) )*a) ; } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.PrintWriter; import java.util.*; /** * Created by trung.pham on 28/12/17. */ public class C_Round_455_Div2 { static long[][]dp; static long MOD =(long) 1e9 + 7; public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); char[]data = new char[n]; dp = new long[n][n]; for(long []a : dp){ Arrays.fill(a,-1); } for(int i = 0; i < n; i++){ data[i] = in.next().charAt(0); } out.println(cal(0, 0, data)); out.close(); } static long cal(int index, int nested, char[]data ){ //System.out.println(index + " " + nested); if(index + 1 == data.length){ return 1; } if(dp[index][nested] != -1){ return dp[index][nested]; } long result = 0; boolean isLoop = data[index] == 'f'; if(isLoop){ result = cal(index + 1, nested + 1, data); }else{ result = cal(index + 1, nested, data); if(nested > 0){ result += cal(index, nested - 1, data); result %= MOD; } } // System.out.println(result + " " + index + " " + nested); return dp[index][nested]= result; } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); boolean[] isF = new boolean[n]; for (int i = 0; i < n; i++) { isF[i] = in.readCharacter() == 'f'; } int[][] mem = new int[n + 1][n + 1]; mem[n][0] = 1; for (int idx = n - 1; idx >= 0; idx--) { for (int indentLevel = 0; indentLevel < n; indentLevel++) { mem[idx + 1][indentLevel + 1] += mem[idx + 1][indentLevel]; mem[idx + 1][indentLevel + 1] %= MiscUtils.MOD7; int res = isF[idx] ? mem[idx + 1][indentLevel + 1] - mem[idx + 1][indentLevel] : mem[idx + 1][indentLevel]; mem[idx][indentLevel] = (res + MiscUtils.MOD7) % MiscUtils.MOD7; } } out.printLine(mem[0][0]); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public char readCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class MiscUtils { public static final int MOD7 = (int) (1e9 + 7); } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(int i) { writer.println(i); } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.PrintWriter; import java.util.Scanner; public class _909C { private static final int MOD = 1000000007; private static void solve(Scanner scan, PrintWriter pw) { int n = scan.nextInt(); scan.nextLine(); // dp[i][j] is the number ways the ith statement is indented j times int[][] dp = new int[n][n]; int[][] dpSums = new int[n][n]; dp[0][0] = 1; for(int i = 0; i < n; i++) { dpSums[0][i] = 1; } boolean lastIsSimple = scan.nextLine().equals("s"); for(int i = 1; i < n; i++) { if(lastIsSimple) { dp[i][0] = dpSums[i-1][n-1]; dpSums[i][0] = dp[i][0]; for(int j = 1; j < n; j++) { dp[i][j] = (dpSums[i-1][n-1] - dpSums[i-1][j-1] + MOD) % MOD; dpSums[i][j] = (dp[i][j] + dpSums[i][j-1]) % MOD; } } else { dp[i][0] = 0; dpSums[i][0] = 0; for(int j = 1; j < n; j++) { dp[i][j] = dp[i-1][j-1]; dpSums[i][j] = (dp[i][j] + dpSums[i][j-1]) % MOD; } } lastIsSimple = scan.nextLine().equals("s"); } System.out.println(dpSums[n-1][n-1]); } public static void main(String[] args) { Scanner scan = new Scanner(new BufferedInputStream(System.in, 1024 * 64)); PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out, 1024 * 64)); solve(scan, pw); pw.flush(); } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.util.Scanner; public class C { private static final int MOD = (int) 1e9 + 7; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] DP = new int[n][n + 1]; DP[0][0] = 1; for (int i = 0; i < n - 1; i++) { if (in.next().charAt(0) == 'f') { for (int j = 1; j < n; j++) DP[i+1][j] = DP[i][j-1]; } else { for (int j = n - 1; j >= 0; j--) DP[i+1][j] = (DP[i][j] + DP[i+1][j+1]) % MOD; } } int answer = 0; for (int i = 0; i < n; i++) answer = (answer + DP[n-1][i]) % MOD; System.out.println(answer); } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class C { //dp[firstInLoop][index][indentLevel] private static int[][][] dp; private static boolean[] wasLoop; private static int MOD=1000000007; private static int n; public static void solve(FastScanner fs) { n=fs.nextInt(); dp=new int[2][n][n]; for (int i=0; i<dp.length; i++) for (int j=0; j<dp[i].length; j++) Arrays.fill(dp[i][j], -1); wasLoop=new boolean[n]; for (int i=0; i<n; i++) wasLoop[i]=fs.next().equals("f"); int ans=go(0, 0, 0); System.out.println(ans); } private static int go(int firstInLoop, int index, int indentLevel) { if (index>=n)//base case return 1; if (dp[firstInLoop][index][indentLevel]!=-1) return dp[firstInLoop][index][indentLevel]; //if I am forced, just do it if (firstInLoop==1) return dp[firstInLoop][index][indentLevel]=go(wasLoop[index]?1:0, index+1, indentLevel+(wasLoop[index]?1:0)); //if I am on the end level, I don't have a choice if (indentLevel==0) { return dp[firstInLoop][index][indentLevel]=go(wasLoop[index]?1:0, index+1, wasLoop[index]?1:0); } else { //otherwise, add all the possibilities int total=0; total+=go(firstInLoop, index, indentLevel-1); total+=go(wasLoop[index]?1:0, index+1, indentLevel+(wasLoop[index]?1:0)); total%=MOD; return dp[firstInLoop][index][indentLevel]=total; } } public static void main(String[] args) throws NumberFormatException, IOException { FastScanner scanner = new FastScanner(System.in); solve(scanner); } private static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
quadratic
909_C. Python Indentation
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); boolean[] isF = new boolean[n]; for (int i = 0; i < n; i++) { isF[i] = in.readCharacter() == 'f'; } long[][] mem = new long[n + 1][n + 1]; mem[n][0] = 1; for (int idx = n - 1; idx >= 0; idx--) { for (int indentLevel = 0; indentLevel < n; indentLevel++) { mem[idx + 1][indentLevel + 1] += mem[idx + 1][indentLevel]; long res = isF[idx] ? mem[idx + 1][indentLevel + 1] - mem[idx + 1][indentLevel] : mem[idx + 1][indentLevel]; mem[idx][indentLevel] = res % MiscUtils.MOD7; } } out.printLine(mem[0][0]); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public char readCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class MiscUtils { public static final int MOD7 = (int) (1e9 + 7); } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } } }
quadratic
909_C. Python Indentation
CODEFORCES
//package educational.round35; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class D { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); int[] a = na(n); int[] ft = new int[n+3]; int x = 0; for(int i = n-1;i >= 0;i--){ x ^= sumFenwick(ft, a[i]); addFenwick(ft, a[i], 1); } x &= 1; for(int Q = ni();Q > 0;Q--){ int l = ni(), r = ni(); long u = (r-l+1)*(r-l)/2; x ^= u&1; if(x == 0){ out.println("even"); }else{ out.println("odd"); } } } public static int sumFenwick(int[] ft, int i) { int sum = 0; for (i++; i > 0; i -= i & -i) sum += ft[i]; return sum; } public static void addFenwick(int[] ft, int i, int v) { if (v == 0 || i < 0) return; int n = ft.length; for (i++; i < n; i += i & -i) ft[i] += v; } public static int findGFenwick(int[] ft, int v) { int i = 0; int n = ft.length; for (int b = Integer.highestOneBit(n); b != 0 && i < n; b >>= 1) { if (i + b < n) { int t = i + b; if (v >= ft[t]) { i = t; v -= ft[t]; } } } return v != 0 ? -(i + 1) : i - 1; } public static int valFenwick(int[] ft, int i) { return sumFenwick(ft, i) - sumFenwick(ft, i - 1); } public static int[] restoreFenwick(int[] ft) { int n = ft.length - 1; int[] ret = new int[n]; for (int i = 0; i < n; i++) ret[i] = sumFenwick(ft, i); for (int i = n - 1; i >= 1; i--) ret[i] -= ret[i - 1]; return ret; } public static int before(int[] ft, int x) { int u = sumFenwick(ft, x - 1); if (u == 0) return -1; return findGFenwick(ft, u - 1) + 1; } public static int after(int[] ft, int x) { int u = sumFenwick(ft, x); int f = findGFenwick(ft, u); if (f + 1 >= ft.length - 1) return -1; return f + 1; } public static int[] buildFenwick(int[] a) { int n = a.length; int[] ft = new int[n + 1]; System.arraycopy(a, 0, ft, 1, n); for (int k = 2, h = 1; k <= n; k *= 2, h *= 2) { for (int i = k; i <= n; i += k) { ft[i] += ft[i - h]; } } return ft; } public static int[] buildFenwick(int n, int v) { int[] ft = new int[n + 1]; Arrays.fill(ft, 1, n + 1, v); for (int k = 2, h = 1; k <= n; k *= 2, h *= 2) { for (int i = k; i <= n; i += k) { ft[i] += ft[i - h]; } } return ft; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new D().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
quadratic
911_D. Inversion Counting
CODEFORCES
/** * @author: Mehul Raheja */ import java.util.*; import java.io.*; public class p4 { /* Runtime = O() */ static int N, M, K; static String s; static StringTokenizer st; static int[] d; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //int[] x = {1,2,3,4,5,6,7,8,9,10}; int N = Integer.parseInt(br.readLine()); int[] d = new int[N]; st = new StringTokenizer(br.readLine()); for (int i = 0; i < N; i++) { d[i] = Integer.parseInt(st.nextToken()); } boolean cur = ((inv(d)) % 2) == 1; // System.out.println(cur); int Q = Integer.parseInt(br.readLine()); for (int i = 0; i < Q; i++) { st = new StringTokenizer(br.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); int dif = b - a + 1; if (dif / 2 % 2 == 1) { cur = !cur; } System.out.println((cur) ? "odd" : "even"); } // for (int i = 0; i < 30; i++) { // int[] x = new int[i]; // for (int j = 0; j < i; j++) { // x[j] = j + 1; // } // int[] y = new int[x.length]; // for (int k = 0; k < x.length; k++) { // y[x.length - 1 - k] = x[k]; // } // //// System.out.println(inv(x)); //// System.out.println(inv(y)); // System.out.println(i + " " + ((inv(y) - inv(x))%2 == 1)); // } } static class BIT { int[] tree; int N; public BIT(int N) { this.N = N; tree = new int[N + 1]; } public BIT(int N, int[] d) { this.N = N; tree = new int[N + 1]; for (int i = 1; i < d.length; i++) { update(i, d[i]); } } public int query(int K) { int sum = 0; for (int i = K; i > 0; i -= (i & -i)) { sum += tree[i]; } return sum; } public void update(int K, int val) { for (int i = K; i <= N; i += (i & -i)) { tree[i] += val; } } } public static int[] toRel(int[] d) { pair[] p = new pair[d.length]; for (int i = 0; i < d.length; i++) { p[i] = new pair(d[i], i + 1); } Arrays.sort(p); int[] fin = new int[d.length]; for (int i = 0; i < d.length; i++) { fin[i] = p[i].b; } return fin; } public static int inv(int[] d) { int ans = 0; int N = d.length; int[] x = toRel(d); BIT b = new BIT(N + 1); for (int i = N - 1; i >= 0; i--) { ans += b.query(x[i]); b.update(x[i], 1); } return ans; } } class pair implements Comparable<pair> { int a, b; public pair(int _a, int _b) { this.a = _a; this.b = _b; } @Override public int compareTo(pair t) { return (a == t.a) ? b - t.b : a - t.a; } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.*; import java.text.*; import java.io.*; import java.math.*; public class code5 { InputStream is; PrintWriter out; static long mod=pow(10,9)+7; static int dx[]={0,0,1,-1},dy[]={1,-1,0,0}; String arr[]; long dp[][]; void solve() throws IOException { int n=ni(); int a[]=na(n); int q=ni(); boolean flag=false; for(int i=0;i<n;i++) { for(int j=0;j<i;j++) { if(a[j]>a[i]) flag^=true; } } while(q--!=0) { int l=ni()-1; int r=ni()-1; int num=(r-l+1); int tot=num*(num-1)/2; if(tot%2==1) flag^=true; if(flag) out.println("odd"); else out.println("even"); } } int sum(int i) { int sum=0; while(i!=0) { if((i%10)%2==1) sum+=i%10; i/=10; } return sum; } ArrayList<Integer> al[]; void take(int n,int m) { al=new ArrayList[n]; for(int i=0;i<n;i++) al[i]=new ArrayList<Integer>(); for(int i=0;i<m;i++) { int x=ni()-1; int y=ni()-1; al[x].add(y); al[y].add(x); } } int check(long n) { int count=0; while(n!=0) { if(n%10!=0) break; n/=10; count++; } return count; } public static int count(int x) { int num=0; while(x!=0) { x=x&(x-1); num++; } return num; } static long d, x, y; void extendedEuclid(long A, long B) { if(B == 0) { d = A; x = 1; y = 0; } else { extendedEuclid(B, A%B); long temp = x; x = y; y = temp - (A/B)*y; } } long modInverse(long A,long M) //A and M are coprime { extendedEuclid(A,M); return (x%M+M)%M; //x may be negative } public static void mergeSort(int[] arr, int l ,int r){ if((r-l)>=1){ int mid = (l+r)/2; mergeSort(arr,l,mid); mergeSort(arr,mid+1,r); merge(arr,l,r,mid); } } public static void merge(int arr[], int l, int r, int mid){ int n1 = (mid-l+1), n2 = (r-mid); int left[] = new int[n1]; int right[] = new int[n2]; for(int i =0 ;i<n1;i++) left[i] = arr[l+i]; for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i]; int i =0, j =0, k = l; while(i<n1 && j<n2){ if(left[i]>right[j]){ arr[k++] = right[j++]; } else{ arr[k++] = left[i++]; } } while(i<n1) arr[k++] = left[i++]; while(j<n2) arr[k++] = right[j++]; } public static void mergeSort(long[] arr, int l ,int r){ if((r-l)>=1){ int mid = (l+r)/2; mergeSort(arr,l,mid); mergeSort(arr,mid+1,r); merge(arr,l,r,mid); } } public static void merge(long arr[], int l, int r, int mid){ int n1 = (mid-l+1), n2 = (r-mid); long left[] = new long[n1]; long right[] = new long[n2]; for(int i =0 ;i<n1;i++) left[i] = arr[l+i]; for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i]; int i =0, j =0, k = l; while(i<n1 && j<n2){ if(left[i]>right[j]){ arr[k++] = right[j++]; } else{ arr[k++] = left[i++]; } } while(i<n1) arr[k++] = left[i++]; while(j<n2) arr[k++] = right[j++]; } static class Pair implements Comparable<Pair>{ int x,y,k; int i,dir; long val; Pair (int x,int y){ this.x=x; this.y=y; } public int compareTo(Pair o) { if(this.x!=o.x) return this.x-o.x; return this.y-o.y; } public String toString(){ return x+" "+y+" "+k+" "+i;} public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair)o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode()*31 + new Long(y).hashCode() ; } } public static boolean isPal(String s){ for(int i=0, j=s.length()-1;i<=j;i++,j--){ if(s.charAt(i)!=s.charAt(j)) return false; } return true; } public static String rev(String s){ StringBuilder sb=new StringBuilder(s); sb.reverse(); return sb.toString(); } public static long gcd(long x,long y){ if(y==0) return x; else return gcd(y,x%y); } public static int gcd(int x,int y){ if(y==0) return x; return gcd(y,x%y); } public static long gcdExtended(long a,long b,long[] x){ if(a==0){ x[0]=0; x[1]=1; return b; } long[] y=new long[2]; long gcd=gcdExtended(b%a, a, y); x[0]=y[1]-(b/a)*y[0]; x[1]=y[0]; return gcd; } public static int abs(int a,int b){ return (int)Math.abs(a-b); } public static long abs(long a,long b){ return (long)Math.abs(a-b); } public static int max(int a,int b){ if(a>b) return a; else return b; } public static int min(int a,int b){ if(a>b) return b; else return a; } public static long max(long a,long b){ if(a>b) return a; else return b; } public static long min(long a,long b){ if(a>b) return b; else return a; } public static long pow(long n,long p,long m){ long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; if(result>=m) result%=m; p >>=1; n*=n; if(n>=m) n%=m; } return result; } public static long pow(long n,long p){ long result = 1; if(p==0) return 1; if (p==1) return n; while(p!=0) { if(p%2==1) result *= n; p >>=1; n*=n; } return result; } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } void run() throws Exception { is = System.in; out = new PrintWriter(System.out); solve(); out.flush(); } public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { public void run() { try { new code5().run(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); //new code5().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.*; public class A { public static void main(String args[]) { Scanner sc = new Scanner (System.in); int n = sc.nextInt(); int a[] = new int[n+1]; for(int i=1 ; i<=n ; i++) a[i] = sc.nextInt(); int cnt = 0; for(int i=1 ; i<=n ; i++) { for(int j=i-1 ; j>=1 ; j--) { if(a[i]<a[j]) ++cnt; } } //System.out.println(cnt); int q = sc.nextInt(); cnt = cnt % 2; while(q-->0) { int x = sc.nextInt(); int y = sc.nextInt(); int r = y-x+1; long ok = (r*(r-1))/2; if(ok%2==0) { } else { cnt ^= 1 ; } System.out.println(cnt==0?"even":"odd"); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Main implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(),"Main",1<<26).start(); } public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = sc.nextInt(); int a[] = new int[n]; for(int i = 0; i < n; ++i) a[i] = sc.nextInt(); int cnt = 0; for(int i = 0; i < n; ++i) { for(int j = 0; j < i; ++j) { if(a[i] < a[j]) cnt ^= 1; } } int m = sc.nextInt(); for(int i = 0; i < m; ++i) { int l = sc.nextInt(); int r = sc.nextInt(); long size = (long)(r - l + 1); long subarr = size * (size - 1) / 2; int type = (int)(subarr % 2); if(type == 1) { cnt ^= 1; } if(cnt == 0) w.println("even"); else w.println("odd"); } w.close(); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.*; import java.util.*; public class Problem911D { public static void main(String args[]) throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), i, j; int ar[] = new int[n]; int inv = 0; for(i = 0; i < n; i++) { ar[i] = sc.nextInt(); } for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { if(i > j && ar[i] < ar[j]) { inv = (inv + 1) % 2; } } } int q = sc.nextInt(); for(i = 0; i < q; i++) { int l = sc.nextInt(); int r = sc.nextInt(); int c = ( ((r-l)*(r-l+1))/2 ) % 2; inv = (inv + c) % 2; if(inv == 0) System.out.println("even"); else System.out.println("odd"); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Liavontsi Brechka */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static @SuppressWarnings("Duplicates") class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = in.nextIntArray(n); int m = in.nextInt(); int count = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) count++; } } StringBuilder res = new StringBuilder(); int l, r, temp, c1, c2; while (m-- > 0) { l = in.nextInt() - 1; r = in.nextInt() - 1; c1 = c2 = 0; for (int i = 0; i < (r - l + 1) / 2; i++) { if (a[l + i] > a[r - i]) c1++; else c2++; temp = a[l + i]; a[l + i] = a[r - i]; a[r - i] = temp; } count = count + c1 - c2; res.append(Math.abs(count) % 2 == 1 ? "odd" : "even").append('\n'); } out.print(res); } } static class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public int[] nextIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; ++i) { array[i] = nextInt(); } return array; } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(readLine()); } return tokenizer.nextToken(); } public String readLine() { String line; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class InversionCounting { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br.readLine(); int[] a = Arrays.stream(br.readLine().split(" ")).mapToInt(x->Integer.parseInt(x)).toArray(); boolean evenInv = true; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length-1; j++) { if(a[j]>a[j+1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; evenInv =!evenInv; } } } int q = Integer.parseInt(br.readLine()); for (int i = 0; i < q; i++) { int[] sw = Arrays.stream(br.readLine().split(" ")).mapToInt(x->Integer.parseInt(x)).toArray(); int len = sw[1]-sw[0]; if((len)*(len+1)%4 != 0) { evenInv = !evenInv; } if(evenInv) { System.out.println("even"); }else { System.out.println("odd"); } } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Liavontsi Brechka */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static @SuppressWarnings("Duplicates") class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = in.nextIntArray(n); int m = in.nextInt(); int count = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) count = (count + 1) % 2; } } StringBuilder res = new StringBuilder(); int l, r, temp; while (m-- > 0) { l = in.nextInt() - 1; r = in.nextInt() - 1; for (int i = 0; i < (r - l + 1) / 2; i++) { temp = a[l + i]; a[l + i] = a[r - i]; a[r - i] = temp; } count = count ^ (((r - l + 1) * (r - l) / 2) % 2); res.append(count == 1 ? "odd" : "even").append('\n'); } out.print(res); } } static class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public int[] nextIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; ++i) { array[i] = nextInt(); } return array; } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(readLine()); } return tokenizer.nextToken(); } public String readLine() { String line; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.NoSuchElementException; public class Main { static PrintWriter out; static InputReader ir; static void solve() { int n=ir.nextInt(); int[] a=ir.nextIntArray(n); int m=ir.nextInt(); long ret=mergeSort(a,0,n)%2; int p,q; for(int i=0;i<m;i++){ p=ir.nextInt()-1; q=ir.nextInt()-1; if((q-p)%4==1||(q-p)%4==2) ret^=1; f(ret); } } public static void f(long a){ if(a==0){ out.println("even"); } else out.println("odd"); } public static long mergeSort(int[] a, int left, int right) { long cnt = 0; if (left + 1 < right) { int mid = (left + right) / 2; cnt += mergeSort(a, left, mid); cnt += mergeSort(a, mid, right); cnt += merge(a, left, mid, right); } return cnt; } public static long merge(int[] a, int left, int mid, int right) { long cnt = 0; int n1 = mid - left; int n2 = right - mid; int[] l = new int[n1 + 1]; int[] r = new int[n2 + 1]; for (int i = 0; i < n1; i++) { l[i] = a[left + i]; } for (int i = 0; i < n2; i++) { r[i] = a[mid + i]; } l[n1] = Integer.MAX_VALUE; r[n2] = Integer.MAX_VALUE; for (int i = 0, j = 0, k = left; k < right; k++) { if (l[i] <= r[j]) { a[k] = l[i]; i++; } else { a[k] = r[j]; j++; cnt += n1 - i; } } return cnt; } public static void main(String[] args) throws Exception { ir = new InputReader(System.in); out = new PrintWriter(System.out); solve(); out.flush(); } static class InputReader { private InputStream in; private byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) { this.in = in; this.curbuf = this.lenbuf = 0; } public boolean hasNextByte() { if (curbuf >= lenbuf) { curbuf = 0; try { lenbuf = in.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return false; } return true; } private int readByte() { if (hasNextByte()) return buffer[curbuf++]; else return -1; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private void skip() { while (hasNextByte() && isSpaceChar(buffer[curbuf])) curbuf++; } public boolean hasNext() { skip(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[][] nextCharMap(int n, int m) { char[][] map = new char[n][m]; for (int i = 0; i < n; i++) map[i] = next().toCharArray(); return map; } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import java.util.stream.Stream; public class Main implements Runnable { static final double time = 1e9; static final int MOD = (int) 1e9 + 7; static final long mh = Long.MAX_VALUE; static final Reader in = new Reader(); static final PrintWriter out = new PrintWriter(System.out); StringBuilder answer = new StringBuilder(); long start = System.nanoTime(); public static void main(String[] args) { new Thread(null, new Main(), "persefone", 1 << 28).start(); } @Override public void run() { solve(); printf(); long elapsed = System.nanoTime() - start; // out.println("stamp : " + elapsed / time); // out.println("stamp : " + TimeUnit.SECONDS.convert(elapsed, TimeUnit.NANOSECONDS)); close(); // out.flush(); } void solve() { int n = in.nextInt(); int[] a = in.nextIntArray(n); int invertions = 0; for (int i = 1; i < n; i++) { for (int j = i - 1; j > -1; j--) { if (a[j] > a[i]) invertions++; } } invertions %= 2; for (int q = in.nextInt(); q > 0; q--) { int l = in.nextInt(); int r = in.nextInt(); int k = r - l + 1; k = (k * (k - 1) >> 1) % 2; if (invertions == k) { invertions = 0; add("even", '\n'); } else { invertions = 1; add("odd", '\n'); } } } void printf() { out.print(answer); } void close() { out.close(); } void printf(Stream<?> str) { str.forEach(o -> add(o, " ")); add("\n"); } void printf(Object... obj) { printf(false, obj); } void printfWithDescription(Object... obj) { printf(true, obj); } private void printf(boolean b, Object... obj) { if (obj.length > 1) { for (int i = 0; i < obj.length; i++) { if (b) add(obj[i].getClass().getSimpleName(), " - "); if (obj[i] instanceof Collection<?>) { printf((Collection<?>) obj[i]); } else if (obj[i] instanceof int[][]) { printf((int[][])obj[i]); } else if (obj[i] instanceof long[][]) { printf((long[][])obj[i]); } else if (obj[i] instanceof double[][]) { printf((double[][])obj[i]); } else printf(obj[i]); } return; } if (b) add(obj[0].getClass().getSimpleName(), " - "); printf(obj[0]); } void printf(Object o) { if (o instanceof int[]) printf(Arrays.stream((int[]) o).boxed()); else if (o instanceof char[]) printf(new String((char[]) o)); else if (o instanceof long[]) printf(Arrays.stream((long[]) o).boxed()); else if (o instanceof double[]) printf(Arrays.stream((double[]) o).boxed()); else if (o instanceof boolean[]) { for (boolean b : (boolean[]) o) add(b, " "); add("\n"); } else add(o, "\n"); } void printf(int[]... obj) { for (int i = 0; i < obj.length; i++) printf(obj[i]); } void printf(long[]... obj) { for (int i = 0; i < obj.length; i++) printf(obj[i]); } void printf(double[]... obj) { for (int i = 0; i < obj.length; i++) printf(obj[i]); } void printf(boolean[]... obj) { for (int i = 0; i < obj.length; i++) printf(obj[i]); } void printf(Collection<?> col) { printf(col.stream()); } <T, K> void add(T t, K k) { if (t instanceof Collection<?>) { ((Collection<?>) t).forEach(i -> add(i, " ")); } else if (t instanceof Object[]) { Arrays.stream((Object[]) t).forEach(i -> add(i, " ")); } else add(t); add(k); } <T> void add(T t) { answer.append(t); } @SuppressWarnings("unchecked") <T extends Comparable<? super T>> T min(T... t) { if (t.length == 0) return null; T m = t[0]; for (int i = 1; i < t.length; i++) if (t[i].compareTo(m) < 0) m = t[i]; return m; } @SuppressWarnings("unchecked") <T extends Comparable<? super T>> T max(T... t) { if (t.length == 0) return null; T m = t[0]; for (int i = 1; i < t.length; i++) if (t[i].compareTo(m) > 0) m = t[i]; return m; } int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } // c = gcd(a, b) -> extends gcd method: ax + by = c <----> (b % a)p + q = c; int[] ext_gcd(int a, int b) { if (b == 0) return new int[] {a, 1, 0 }; int[] vals = ext_gcd(b, a % b); int d = vals[0]; // gcd int p = vals[2]; // int q = vals[1] - (a / b) * vals[2]; return new int[] { d, p, q }; } // find any solution of the equation: ax + by = c using extends gcd boolean find_any_solution(int a, int b, int c, int[] root) { int[] vals = ext_gcd(Math.abs(a), Math.abs(b)); if (c % vals[0] != 0) return false; printf(vals); root[0] = c * vals[1] / vals[0]; root[1] = c * vals[2] / vals[0]; if (a < 0) root[0] *= -1; if (b < 0) root[1] *= -1; return true; } int mod(int x) { return x % MOD; } int mod(int x, int y) { return mod(mod(x) + mod(y)); } long mod(long x) { return x % MOD; } long mod (long x, long y) { return mod(mod(x) + mod(y)); } int lw(long[] f, int l, int r, long k) { int R = r, m = 0; while (l <= r) { m = l + r >> 1; if (m == R) return m; if (f[m] >= k) r = m - 1; else l = m + 1; } return l; } int up(long[] f, int l, int r, long k) { int R = r, m = 0; while (l <= r) { m = l + r >> 1; if (m == R) return m; if (f[m] > k) r = m - 1; else l = m + 1; } return l; } int lw(int[] f, int l, int r, int k) { int R = r, m = 0; while (l <= r) { m = l + r >> 1; if (m == R) return m; if (f[m] >= k) r = m - 1; else l = m + 1; } return l; } int up(int[] f, int l, int r, int k) { int R = r, m = 0; while (l <= r) { m = l + r >> 1; if (m == R) return m; if (f[m] > k) r = m - 1; else l = m + 1; } return l; } <K extends Comparable<K>> int lw(List<K> li, int l, int r, K k) { int R = r, m = 0; while (l <= r) { m = l + r >> 1; if (m == R) return m; if (li.get(m).compareTo(k) >= 0) r = m - 1; else l = m + 1; } return l; } <K extends Comparable<K>> int up(List<K> li, int l, int r, K k) { int R = r, m = 0; while (l <= r) { m = l + r >> 1; if (m == R) return m; if (li.get(m).compareTo(k) > 0) r = m - 1; else l = m + 1; } return l; } <K extends Comparable<K>> int bs(List<K> li, int l, int r, K k) { while (l <= r) { int m = l + r >> 1; if (li.get(m) == k) return m; else if (li.get(m).compareTo(k) < 0) l = m + 1; else r = m - 1; } return -1; } long calc(int base, int exponent) { if (exponent == 0) return 1; if (exponent == 1) return base % MOD; long m = calc(base, exponent / 2); if (exponent % 2 == 0) return (m * m) % MOD; return (base * ((m * m) % MOD)) % MOD; } long calc(int base, long exponent) { if (exponent == 0) return 1; if (exponent == 1) return base % MOD; long m = calc(base, exponent / 2); if (exponent % 2 == 0) return (m * m) % MOD; return (base * ((m * m) % MOD)) % MOD; } long calc(long base, long exponent) { if (exponent == 0) return 1; if (exponent == 1) return base % MOD; long m = calc(base, exponent / 2); if (exponent % 2 == 0) return (m * m) % MOD; return (base * (m * m % MOD)) % MOD; } long power(int base, int exponent) { if (exponent == 0) return 1; long m = power(base, exponent / 2); if (exponent % 2 == 0) return m * m; return base * m * m; } void swap(int[] a, int i, int j) { a[i] ^= a[j]; a[j] ^= a[i]; a[i] ^= a[j]; } void swap(long[] a, int i, int j) { long tmp = a[i]; a[i] = a[j]; a[j] = tmp; } static class Pair<K extends Comparable<? super K>, V extends Comparable<? super V>> implements Comparable<Pair<K, V>> { private K k; private V v; Pair() {} Pair(K k, V v) { this.k = k; this.v = v; } K getK() { return k; } V getV() { return v; } void setK(K k) { this.k = k; } void setV(V v) { this.v = v; } void setKV(K k, V v) { this.k = k; this.v = v; } @SuppressWarnings("unchecked") @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || !(o instanceof Pair)) return false; Pair<K, V> p = (Pair<K, V>) o; return k.compareTo(p.k) == 0 && v.compareTo(p.v) == 0; } @Override public int hashCode() { int hash = 31; hash = hash * 89 + k.hashCode(); hash = hash * 89 + v.hashCode(); return hash; } @Override public int compareTo(Pair<K, V> pair) { return k.compareTo(pair.k) == 0 ? v.compareTo(pair.v) : k.compareTo(pair.k); } @Override public Pair<K, V> clone() { return new Pair<K, V>(this.k, this.v); } @Override public String toString() { return String.valueOf(k).concat(" ").concat(String.valueOf(v)).concat("\n"); } } static class Reader { private BufferedReader br; private StringTokenizer st; Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String s = ""; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } } }
quadratic
911_D. Inversion Counting
CODEFORCES
//>>>BaZ<<<// import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { static int dx[] = {-1,1,0,0}; static int dy[] = {0,0,1,-1}; static long MOD = 1000000007; static int INF = Integer.MAX_VALUE/10; static PrintWriter pw; static Reader scan; //static MyFileReader scan; //static MyFileReader1 ss; static int ni() throws IOException{return scan.nextInt();} static long nl() throws IOException{return scan.nextLong();} static double nd() throws IOException{return scan.nextDouble();} static void pl() throws IOException{pw.println();} static void pl(Object o) throws IOException{pw.println(o);} static void p(Object o) throws IOException {pw.print(o+" ");} static void psb(StringBuilder sb) throws IOException {pw.print(sb);} public static void main(String[] args){ new Thread(null,null,"BaZ",99999999) { public void run() { try { solve(); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static void solve() throws IOException { Calendar CAL1 = Calendar.getInstance(); CAL1.setTime(new Date()); scan = new Reader(); //scan = new MyFileReader(); //ss = new MyFileReader1(); pw = new PrintWriter(System.out,true); //pw = new PrintWriter(new File("C://Users/Aman deep/Desktop/output.txt")); StringBuilder sb = new StringBuilder(); int n = ni(); int inv = 0; int arr[] = new int[n]; for(int i=0;i<n;++i) { arr[i] = ni(); for(int j=0;j<i;++j) if(arr[j]>arr[i]) inv = 1-inv; } int q = ni(); while(q-->0) { int l = ni(); int r = ni(); int par = c2(r-l+1); par&=1; if(par!=0) inv = 1-inv; if(inv==0) sb.append("even\n"); else sb.append("odd\n"); } psb(sb); Calendar CAL2 = Calendar.getInstance(); CAL2.setTime(new Date()); double Execution_Time = (double)(CAL2.getTimeInMillis()-CAL1.getTimeInMillis())/1000.000; //System.out.println("Execution time : "+Execution_Time+" seconds"); pw.flush(); pw.close(); } static int c2(int n) { return (n*(n-1))>>1; } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static class MyFileReader //File input template { StringTokenizer st; BufferedReader br; MyFileReader() throws IOException { br = new BufferedReader(new FileReader("C://Users/Aman deep/Desktop/input.txt")); } String nextLine() throws IOException { return br.readLine(); } String next() throws IOException { if(st==null || !st.hasMoreTokens()) st = new StringTokenizer(nextLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } } static class MyFileReader1 //File input template { StringTokenizer st; BufferedReader br; MyFileReader1() throws IOException { br = new BufferedReader(new FileReader("C://Users/Aman deep/Desktop/output.txt")); } String nextLine() throws IOException { return br.readLine(); } String next() throws IOException { if(st==null || !st.hasMoreTokens()) st = new StringTokenizer(nextLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.*; public class maestro{ public static long inversions(long[] arr) { long x = 0; int n = arr.length; for (int i = n - 2; i >= 0; i--) { for (int j = i + 1; j < n; j++) { if (arr[i] > arr[j]) { x++; //temp++; } //if (temp%2==0) inv_a[i][j]=0; //else inv_a[i][j]=1; } } return x; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] arr = new long[n]; for (int i=0;i<n;i++) arr[i] = sc.nextLong(); long m = sc.nextLong(); long x = inversions(arr)%2; for (int i=0;i<m;i++){ int l = sc.nextInt()-1; int r = sc.nextInt()-1; if ((r-l+1)%4>1) x=(x+1)%2; if (x==1) System.out.println("odd"); else System.out.println("even"); } /*int inv=0; int temp=0; long[][] inv_a = new long[n][n]; for (int i=0;i<n;i++){ for (int j=0;j<n;j++){ inv_a[i][j]=-1; } } for (int i=n-2;i>=0;i--){ for (int j=i+1;j<n;j++){ if (arr[i]<arr[j]){ inv++; temp++; } if (temp%2==0) inv_a[i][j]=0; else inv_a[i][j]=1; } temp=0; } if (inv%2==0) inv=0; else inv=1; for (int i=0;i<m;i++){ int l = sc.nextInt()-1; int r = sc.nextInt()-1; long[][] exp = new long[r-l+1][r-l+1]; for (int k=0;k<exp.length;k++){ for (int h=0;h<exp.length;h++){ exp[k][h]=-1; } } for (int j=l;j<=r;j++){ } }*/ } }
quadratic
911_D. Inversion Counting
CODEFORCES
//package educational35; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class ProblemD { public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static StringTokenizer tok = null; public static void main(String args[]) throws IOException { tok = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tok.nextToken()); int tab[] = new int[n]; tok = new StringTokenizer(in.readLine()); for (int i=0; i<n; i++) tab[i] = Integer.parseInt(tok.nextToken()); int inversions = countInversions(tab); boolean isOdd = inversions % 2 == 1; tok = new StringTokenizer(in.readLine()); int k = Integer.parseInt(tok.nextToken()); int start, end, len; for (int i=0; i<k; i++) { tok = new StringTokenizer(in.readLine()); start = Integer.parseInt(tok.nextToken()); end = Integer.parseInt(tok.nextToken()); len = (end - start + 1) % 4; if (len == 2 || len ==3) isOdd = !isOdd; out.println(isOdd ? "odd" : "even"); } out.close(); } private static int countInversions(int tab[]) { int n = tab.length; int auxTab[] = new int[n+1]; return _countInversions(tab, 0, n, auxTab); }; private static int _countInversions(int tab[], int start, int end, int auxTab[]) { //indices from start to end; but values from start+1 to end+1 !! if (start+1 >= end) return 0; int mid = (start + end) / 2; int lowerFound = 0; int higherFound = 0; int count = 0; for (int i=start; i<end; i++){ if (tab[i] < mid+1){ count += higherFound; auxTab[start+lowerFound] = tab[i]; lowerFound++; } else { auxTab[mid + higherFound] = tab[i]; higherFound++; } } for (int i=start; i<end; i++) tab[i] = auxTab[i]; count += _countInversions(tab, start, mid, auxTab); count += _countInversions(tab, mid, end, auxTab); return count; } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.PrintWriter; import java.util.Scanner; public class D { public void solve(Scanner in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n + 1]; for(int i = 1; i <= n; ++i) a[i] = in.nextInt(); int[] rangeInv = new int[n + 1]; BIT bit = new BIT(n + 1); for(int i = 1; i <= n; ++i) { int cur = a[i]; int inv = (int) bit.sum(cur, n); rangeInv[i] = rangeInv[i - 1] + inv; bit.add(cur, 1); } int m = in.nextInt(); int curTotal = rangeInv[n]; for(int qq = 0; qq < m; ++qq) { int l = in.nextInt(); int r = in.nextInt(); int N = r - l + 1; int total = N * (N - 1) / 2; int cur = rangeInv[r] - rangeInv[l - 1]; int newInv = total - cur; curTotal -= cur; curTotal += newInv; if(curTotal % 2 == 0) { out.println("even"); } else { out.println("odd"); } } } public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); new D().solve(in, out); in.close(); out.close(); } class BIT { long[] tree; int n; public BIT(int n) { this.n = n; tree = new long[n + 1]; } public void add(int i, long val) { while(i <= n) { tree[i] += val; i += i & -i; } } public long sum(int to) { long res = 0; for(int i = to; i >= 1; i -= (i & -i)) { res += tree[i]; } return res; } public long sum(int from, int to) { return sum(to) - sum(from - 1); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import javafx.util.Pair; import java.util.*; import static java.lang.Math.floor; import static java.lang.Math.min; public class Main { private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int n = scanInt(); List<Integer> a = scanList(n); int m = scanInt(); List<Integer> left = new ArrayList<>(); List<Integer> right = new ArrayList<>(); for (int i = 0; i < m; i++) { left.add(scanInt()); right.add(scanInt()); } String even = "even"; String odd = "odd"; int inversions = 0; for (int i = 0; i < a.size(); i++) { for (int j = i; j < a.size(); j++) { if (a.get(i) > a.get(j)) { ++inversions; } } } inversions = inversions % 2; for (int i = 0; i < m; i++) { inversions = (inversions + (right.get(i) - left.get(i) + 1) / 2 % 2) % 2; println(inversions % 2 == 0 ? even : odd); } } private static Integer get(int digit, List<Integer> numbers) { Integer toReturn = null; for (Integer number : numbers ) { if (number <= digit) { toReturn = number; break; } } return toReturn; } private static List<Integer> toList(char[] chars) { List<Integer> integers = new ArrayList<>(); for (int i = 0; i < chars.length; i++) { integers.add(Integer.parseInt(String.valueOf(chars[i]))); } return integers; } private static List<Pair<Integer, Integer>> scanPairs(int amount) { List<Pair<Integer, Integer>> list = new ArrayList<>(); for (int i = 0; i < amount; i++) { list.add(new Pair<>(scanInt(), scanInt())); } return list; } private static int[] scanArray(int length) { int array[] = new int[length]; for (int i = 0; i < length; i++) { array[i] = scanner.nextInt(); } return array; } private static List<Integer> scanList(int length) { List<Integer> integers = new ArrayList<>(); for (int i = 0; i < length; i++) { integers.add(scanner.nextInt()); } return integers; } public static String scanString() { return scanner.next(); } private static int scanInt() { return scanner.nextInt(); } private static void println(int n) { System.out.println(n); } private static void print(int n) { System.out.print(n); } private static void println(String s) { System.out.println(s); } private static void print(String s) { System.out.print(s); } private static void print(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class Main { static int bit[]; static int array[]; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); bit = new int[1505]; array = new int[n + 1]; StringTokenizer st = new StringTokenizer(br.readLine()); for(int i = 1;i <= n;i++) array[i] = Integer.parseInt(st.nextToken()); long ans = 0; for(int i = n;i >= 1;i--){ ans += read(array[i]); update(array[i]); } long val = (ans & 1) + 1000_000; int m = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); for(int i = 1;i <= m;i++){ st = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); long temp = (r - l + 1); temp = temp*(temp - 1) / 2; if((temp & 1) == 1)--val; if((val & 1) == 1)sb.append("odd"); else sb.append("even"); sb.append('\n'); } System.out.print(sb); } static int update(int idx){ int sum = 0; while(idx < 1501){ bit[idx] += 1; idx += idx & (-idx); } return sum; } static int read(int idx){ int sum = 0; while(idx > 0){ sum += bit[idx]; idx -= idx & (-idx); } return sum; } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] ar = new int[n]; for (int i = 0; i < n; i++) { ar[i] = in.nextInt(); } long ninv = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (ar[i] > ar[j]) ninv++; } } int m = in.nextInt(); for (int i = 0; i < m; i++) { int l = in.nextInt(); int r = in.nextInt(); int s = (r - l) * (r - l + 1) / 2; ninv += s; if (ninv % 2 == 0) out.println("even"); else out.println("odd"); } } } static class InputReader { StringTokenizer st; BufferedReader br; public InputReader(InputStream is) { BufferedReader br = new BufferedReader(new InputStreamReader(is)); this.br = br; } public String next() { if (st == null || !st.hasMoreTokens()) { String nextLine = null; try { nextLine = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (nextLine == null) return null; st = new StringTokenizer(nextLine); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int[] a = in.nextIntArray(n); int ct = 0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (a[i] > a[j]) ++ct; } } ct &= 1; int Q = in.nextInt(); for (int q = 0; q < Q; ++q) { int l = in.nextInt(); int r = in.nextInt(); int size = (r - l + 1) * (r - l) >> 1; ct ^= size & 1; out.println(ct % 2 == 0 ? "even" : "odd"); } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Reader.init(System.in); int n = Reader.nextInt(); int[] arr = new int[n]; int initial = 0; for (int i = 0; i < n; i++) arr[i] = Reader.nextInt(); for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) if (arr[i] > arr[j]) initial++; } int m = Reader.nextInt(); boolean parity = initial % 2 == 0; // System.out.println(parity ? "even": "odd"); for (int i = 0; i < m; i++) { int l = Reader.nextInt(); int r = Reader.nextInt(); int elems = r - l + 1; boolean change = (elems/2) % 2 == 0; parity = parity == change; System.out.println(parity ? "even": "odd"); } } } /** * Reader class based on the article at "https://www.cpe.ku.ac.th/~jim/java-io.html" * */ class Reader{ private static BufferedReader reader; private static StringTokenizer tokenizer; static void init(InputStream inputStream){ reader = new BufferedReader(new InputStreamReader(inputStream)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { String read; while (!tokenizer.hasMoreTokens()){ read = reader.readLine(); if (read == null || read.equals("")) return "-1"; tokenizer = new StringTokenizer(read); } return tokenizer.nextToken(); } static int nextInt() throws IOException{ return Integer.parseInt(next()); } // static long nextLong() throws IOException{ // return Long.parseLong(next()); // } //Get a whole line. // static String line() throws IOException{ // return reader.readLine(); // } // // static double nextDouble() throws IOException{return Double.parseDouble(next());} }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int[] a = IOUtils.readIntArray(in, n); MiscUtils.decreaseByOne(a); int m = in.readInt(); int parity = inversions(a) % 2; boolean[] lengthToParityFlip = new boolean[n + 1]; for (int length = 1; length < lengthToParityFlip.length; length++) { lengthToParityFlip[length] = (((length * (length - 1) / 2) % 2) == 1); } for (int query = 0; query < m; query++) { int l = in.readInt() - 1, r = in.readInt() - 1; int length = r - l + 1; if (lengthToParityFlip[length]) { parity ^= 1; } out.printLine(parity == 0 ? "even" : "odd"); } } private int inversions(int[] a) { int res = 0; for (int j = 0; j < a.length; j++) { for (int i = j + 1; i < a.length; i++) { if (a[i] < a[j]) { res++; } } } return res; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class MiscUtils { public static void decreaseByOne(int[]... arrays) { for (int[] array : arrays) { for (int i = 0; i < array.length; i++) { array[i]--; } } } } static class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = in.readInt(); } return array; } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.*; import java.util.*; public class utkarsh{ BufferedReader br; PrintWriter out; int game(int s, int mid, int e, int[] a){ int i, j, n, m; n = mid - s + 1; m = e - mid; int b[] = new int[n]; int c[] = new int[m]; for(i = 0; i < n; i++) b[i] = a[s + i]; for(j = 0; j < m; j++) c[j] = a[mid + 1 + j]; i = j = 0; int ans = 0; for(int k = s; k <= e; k++){ if(i == n){ a[k] = c[j++]; }else if(j == m){ a[k] = b[i++]; }else{ if(b[i] < c[j]){ a[k] = b[i++]; }else{ a[k] = c[j++]; ans += n - i; } } } return ans; } int play(int s, int e, int[] a){ if(s >= e) return 0; int m = (s + e) >> 1; return play(s, m, a) + play(m+1, e, a) + game(s, m, e, a); } void solve(){ br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); int i, j, k, l, r, n; n = ni(); int a[] = new int[n]; int d[] = new int[n]; for(i = 0; i < n; i++) { a[i] = ni(); d[i] = a[i]; } int ans = (play(0, n-1, d) & 1); int q = ni(); while(q-- > 0){ l = ni(); r = ni(); ans ^= ((r - l + 1) * (r - l) / 2); //out.println(ans); if((ans & 1) > 0) out.println("odd"); else out.println("even"); } out.flush(); } int ni(){ return Integer.parseInt(ns()); } String ip[]; int len, sz; String ns(){ if(len >= sz){ try{ ip = br.readLine().split(" "); len = 0; sz = ip.length; }catch(IOException e){ throw new InputMismatchException(); } if(sz <= 0) return "-1"; } return ip[len++]; } public static void main(String[] args){ new utkarsh().solve(); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.*; import java.util.*; public class CODEFORCES { @SuppressWarnings("rawtypes") static InputReader in; static PrintWriter out; static void solve() { int n = in.ni(); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = in.ni(); int cnt = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) if (arr[j] > arr[i]) cnt++; } cnt %= 2; int m = in.ni(); while (m-- > 0) { int l = in.ni(), r = in.ni(); int fin = r - l + 1; fin *= (fin - 1); fin >>= 1; if ((fin & 1) == 1) cnt++; cnt %= 2; if ((cnt & 1) == 1) out.println("odd"); else out.println("even"); } } @SuppressWarnings("rawtypes") static void soln() { in = new InputReader(System.in); out = new PrintWriter(System.out); solve(); out.flush(); } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { try { soln(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } // To Get Input // Some Buffer Methods static class InputReader<SpaceCharFilter> { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class D911 { public static long total = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); ArrayList<Integer> temp = new ArrayList<>(); int[] ar = new int[n]; int[] memo = new int[n]; for (int i = 0; i < n; i++) { int t = in.nextInt(); int index = -1*Collections.binarySearch(temp, t)-1; temp.add(index, t); ar[i] = t; memo[i] = i - index; total += memo[i]; } int m = in.nextInt(); for (int i = 0; i < m; i++) { // int a = in.nextInt() - 1, b = in.nextInt() - 1; // query(ar, memo, in.nextInt() - 1, in.nextInt() - 1); total += (-1*(in.nextInt() - 1 - in.nextInt() + 1) + 1) / 2; System.out.println(total%2 == 0 ? "even" : "odd"); } } public static void query(int[] ar, int[] memo, int a, int b) { if (a >= b) { return; } if (ar[a] < ar[b]) { memo[a]++; total++; } else { memo[b]--; total--; } int t = ar[a]; ar[b] = ar[a]; ar[b] = t; t = memo[a]; memo[b] = memo[a]; memo[b] = t; query(ar, memo, a + 1, b - 1); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.*; import java.io.*; public class P911d { private static void solve() { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } int cnt = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) { cnt++; } } } cnt %= 2; int m = nextInt(); for (int i = 0; i < m; i++) { int l = nextInt(); int r = nextInt(); int size = r - l + 1; long sum = ((long)size * (size - 1)) / 2; sum %= 2; cnt += sum; cnt %= 2; System.out.println(cnt == 0 ? "even" : "odd"); } } private static void run() { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } private static StringTokenizer st; private static BufferedReader br; private static PrintWriter out; private static String next() { while (st == null || !st.hasMoreElements()) { String s; try { s = br.readLine(); } catch (IOException e) { return null; } st = new StringTokenizer(s); } return st.nextToken(); } private static int nextInt() { return Integer.parseInt(next()); } private static long nextLong() { return Long.parseLong(next()); } public static void main(String[] args) { run(); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.awt.geom.*; import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; /* br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); */ public class Main { private static BufferedReader br; private static StringTokenizer st; private static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //int qq = 1; int qq = Integer.MAX_VALUE; //int qq = readInt(); for(int casenum = 1; casenum <= qq; casenum++) { int n = readInt(); int[] l = new int[n]; for(int i = 0; i < n; i++) { l[i] = readInt(); } int ret = 0; for(int i = 0; i < n; i++) { for(int j = i+1; j < n; j++) { if(l[i] > l[j]) { ret++; } } } int qqq = readInt(); while(qqq-- > 0) { int a = readInt(); int b = readInt(); int d = b-a; ret ^= d*(d+1)/2; pw.println(ret%2 == 0 ? "even" : "odd"); } } exitImmediately(); } private static void exitImmediately() { pw.close(); System.exit(0); } private static long readLong() throws IOException { return Long.parseLong(nextToken()); } private static double readDouble() throws IOException { return Double.parseDouble(nextToken()); } private static int readInt() throws IOException { return Integer.parseInt(nextToken()); } private static String nextLine() throws IOException { String s = br.readLine(); if(s == null) { exitImmediately(); } st = null; return s; } private static String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine().trim()); } return st.nextToken(); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author phantom11 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, OutputWriter out) { int N = in.nextInt(), i, a[] = new int[N]; int rev[] = new int[N]; for (i = 0; i < N; i++) { a[i] = in.nextInt(); rev[N - i - 1] = a[i]; } long[][] inverse = inversions(a, N); long[][] revInverse = inversions(rev, N); int q = in.nextInt(); long inversions = inverse[0][N - 1] % 2; while (q-- > 0) { int left = in.nextInt() - 1, right = in.nextInt() - 1; int length = right - left + 1; length = length * (length - 1) / 2; if (length % 2 == 1) { inversions = 1 - inversions; } if (inversions % 2 == 0) { out.printLine("even"); } else { out.printLine("odd"); } } //DebugUtils.debug(inverse); } public long[][] inversions(int a[], int N) { int x[][] = new int[N][N]; int i, j; for (i = 0; i < N; i++) { for (j = i + 1; j < N; j++) { if (a[i] > a[j]) { x[i][j] = 1; } } } int colSum[][] = new int[N][N]; for (i = 0; i < N; i++) { colSum[0][i] = x[0][i]; for (j = 1; j < N; j++) { colSum[j][i] = colSum[j - 1][i] + x[j][i]; } } long inverse[][] = new long[N][N]; for (int length = 2; length <= N; length++) { j = length - 1; i = 0; while (j < N) { inverse[i][j] = inverse[i][j - 1] + colSum[i + length - 1][j]; if (i > 0) { inverse[i][j] -= colSum[i - 1][j]; } i++; j++; } } return inverse; } } static class InputReader { BufferedReader in; StringTokenizer tokenizer = null; public InputReader(InputStream inputStream) { in = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } catch (IOException e) { return null; } } public int nextInt() { return Integer.parseInt(next()); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.*; import java.util.*; public class Mainn { public static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public char nextChar() { return next().charAt(0); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArr(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = this.nextInt(); } return arr; } public Integer[] nextIntegerArr(int n) { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = new Integer(this.nextInt()); } return arr; } public int[][] next2DIntArr(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = this.nextInt(); } } return arr; } public int[] nextSortedIntArr(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = this.nextInt(); } Arrays.sort(arr); return arr; } public long[] nextLongArr(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = this.nextLong(); } return arr; } public char[] nextCharArr(int n) { char[] arr = new char[n]; for (int i = 0; i < n; i++) { arr[i] = this.nextChar(); } return arr; } } public static InputReader scn = new InputReader(); public static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { // InputStream inputStream = System.in; // Useful when taking input other than // console eg file handling // check ctor of inputReader // To print in file use this:- out = new PrintWriter("destination of file // including extension"); int n = scn.nextInt(), inv = 0; int[] arr = scn.nextIntArr(n); for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(arr[i] > arr[j]) { inv++; } } } int ans = inv % 2; int m = scn.nextInt(); while(m-- > 0) { int l = scn.nextInt(), r = scn.nextInt(); int change = ((r - l + 1) / 2) % 2; if(change == 1) { ans = 1 - ans; } if(ans == 0) { out.println("even"); } else { out.println("odd"); } } out.close(); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.Scanner; import java.util.StringTokenizer; public class D { public static class BIT { int[] dat; public BIT(int n){ dat = new int[n + 1]; } public void add(int k, int a){ // k : 0-indexed for(int i = k + 1; i < dat.length; i += i & -i){ dat[i] += a; } } public int sum(int s, int t){ // [s, t) if(s > 0) return sum(0, t) - sum(0, s); int ret = 0; for(int i = t; i > 0; i -= i & -i) { ret += dat[i]; } return ret; } } public static void main(String[] args) { try (final Scanner sc = new Scanner(System.in)) { final int N = sc.nextInt(); int[] array = new int[N]; for(int i = 0; i < N; i++){ array[i] = sc.nextInt() - 1; } long inv = 0; BIT bit = new BIT(N); for(int i = 0; i < N; i++){ inv += bit.sum(array[i], N); bit.add(array[i], 1); } //System.out.println(inv); int mod2 = (int)(inv % 2); final int M = sc.nextInt(); for(int i = 0; i < M; i++){ final int l = sc.nextInt() - 1; final int r = sc.nextInt() - 1; final long size = (r - l) + 1; if(size > 1){ //System.out.println(size + " " + ((size * (size - 1) / 2))); if((size * (size - 1) / 2) % 2 == 1){ mod2 = 1 - mod2; } } System.out.println((mod2 == 0) ? "even" : "odd"); } } } public static class Scanner implements Closeable { private BufferedReader br; private StringTokenizer tok; public Scanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } private void getLine() { try { while (!hasNext()) { tok = new StringTokenizer(br.readLine()); } } catch (IOException e) { /* ignore */ } } private boolean hasNext() { return tok != null && tok.hasMoreTokens(); } public String next() { getLine(); return tok.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public void close() { try { br.close(); } catch (IOException e) { /* ignore */ } } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.*; import java.util.*; public class A{ InputStream is; PrintWriter out; String INPUT = ""; public void solve(){ int n=ni(); int ans=0; int[] arr=na(n); for(int i=0;i<n;i++){ for(int j=i+1; j<n; j++){ if(arr[i] > arr[j]){ ans^=1; } } } int m=ni(); while(m-->0){ int a=ni(), b=ni(); int diff=Math.abs(a-b)+1; ans = ans ^ ((((diff-1)*diff)/2)%2); out.println(ans==0 ? "even" : "odd"); } } void print(int n, long[][] memo){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ out.print(memo[i][j]+" "); } out.println(); } } void run(){ is = new DataInputStream(System.in); out = new PrintWriter(System.out); int t=1;while(t-->0)solve(); out.flush(); } public static void main(String[] args)throws Exception{new A().run();} //Fast I/O code is copied from uwi code. private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte(){ if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns(){ int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n){ char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m){ char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n){ int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni(){ int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl(){ long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } static int i(long x){return (int)Math.round(x);} static class Pair implements Comparable<Pair>{ long fs,sc; Pair(long a,long b){ fs=a;sc=b; } public int compareTo(Pair p){ if(this.fs>p.fs)return 1; else if(this.fs<p.fs)return -1; else{ return i(this.sc-p.sc); } //return i(this.sc-p.sc); } public String toString(){ return "("+fs+","+sc+")"; } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int l, r, sum = 0; int[] a = new int[n+5]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (a[i] > a[j]) sum++; int q = in.nextInt(); while (q-- > 0){ l = in.nextInt(); r = in.nextInt(); sum += (r-l+1)/2; if (sum % 2 == 1) out.println("odd"); else out.println("even"); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int l, r, sum = 0; int[] a = new int[n+5]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (a[i] > a[j]) sum++; int q = in.nextInt(); while (q-- > 0){ l = in.nextInt(); r = in.nextInt(); sum += (r-l+1)/2; if (sum % 2 == 1) out.println("odd"); else out.println("even"); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
/** p * @author prakhar28 * */ import java.io.*; import java.util.*; import java.util.Map.Entry; import java.text.*; import java.math.*; import java.util.regex.*; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class Main{ static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[200005]; // line length+1 int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n' || c==' ') { //buf[cnt++]=(byte)c; break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static class FastScanner { private final BufferedReader bufferedReader; private StringTokenizer stringTokenizer; public FastScanner() { bufferedReader = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) { try { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException e) { throw new RuntimeException("Can't read next value", e); } } return stringTokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine(){ String str = ""; try { str = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void closeall() throws IOException{ printWriter.close(); sc.close(); in.close(); } static Scanner sc = new Scanner(System.in); static Reader in = new Reader(); static FastScanner fastScanner = new FastScanner(); static PrintWriter printWriter = new PrintWriter(new BufferedOutputStream(System.out)); static BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); static long INF = (long)(1e18); int V,E; ArrayList<Integer> adj[]; HashMap<Integer,Integer> path; Main(){ } Main(int v,int e){ V=v; E=e; adj=new ArrayList[v]; path = new HashMap<Integer,Integer>(); for(int i=0;i<v;i++) adj[i] = new ArrayList<>(); } void addEdge(int u,int v){ adj[u].add(v); adj[v].add(u); } static long power(long a,long k) { long m = 1000000007; long ans=1; long tmp=a%m; while(k>0) { if(k%2==1) ans=ans*tmp%m; tmp=tmp*tmp%m; k>>=1; } return ans; } static class Pair /*implements Comparable<Pair>*/{ long F,S; Pair(long x,long y){ F = x; S = y; } /*public int compareTo(Pair ob){ return this.num-ob.num; }*/ } static long gcd(long a,long b) { if(a<b) { long t = a; a = b; b = t; } long r = a%b; if(r==0) return b; return gcd(b,r); } static int lower_bound(int[] a,int low,int high,int val) { while(low!=high) { int mid = (low+high)/2; if(a[mid]<val) low=mid+1; else high=mid; } return low; } static int max(int a,int b) { return (int)Math.max(a, b); } static long max(long a,long b) { return (long)Math.max(a, b); } static int min(int a,int b) { if(a<b) return a; return b; } static long mod = 1000000007; public static void main(String args[])throws IOException{ int n = in.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = in.nextInt(); int inv = 0; for(int i=0;i<n;i++) { for(int j=0;j<i;j++) { if(a[j]>a[i]) inv++; } } inv%=2; int q = in.nextInt(); for(int i=0;i<q;i++) { int l = in.nextInt(); int r = in.nextInt(); int num = r-l+1; inv=(inv+num*(num-1)/2)%2; if(inv==0) printWriter.println("even"); else printWriter.println("odd"); } closeall(); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * @author pttrung */ public class D_Edu_Round_35 { public static long MOD = 1000000007; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); int[] data = new int[n]; for (int i = 0; i < n; i++) { data[i] = in.nextInt(); } FT tree = new FT(n + 1); int result = 0; for (int i = n - 1; i >= 0; i--) { tree.update(data[i], 1); result += tree.get(data[i] - 1); result %= 2; } int q = in.nextInt(); int[] tmp = new int[n]; for (int i = 0; i < q; i++) { int l = in.nextInt() - 1; int r = in.nextInt() - 1; FT a = new FT(n + 1); int total = r - l + 1; total = total * (total - 1) / 2; total %= 2; // for (int j = r; j >= l; j--) { // tmp[j] = data[j]; // a.update(data[j], 1); // total -= a.get(data[j] - 1); // // total += 2; // // total %= 2; // } // System.out.println("PRE " + result); // System.out.println(Arrays.toString(tmp)); // for (int j = 0; j < (r - l + 1) / 2; j++) { // data[r - j] = tmp[l + j]; // data[l + j] = tmp[r - j]; // } result += total; result %= 2; // System.out.println("AFTER " + result); // System.out.println(Arrays.toString(data)); if (result % 2 == 0) { out.println("even"); } else { out.println("odd"); } } out.close(); } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return Integer.compare(x, o.x); } } public static class FT { int[] data; FT(int n) { data = new int[n]; } public void update(int index, int value) { while (index < data.length) { data[index] += value; data[index] %= 2; index += (index & (-index)); } } public int get(int index) { int result = 0; while (index > 0) { result += data[index]; result %= 2; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b, long MOD) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2, MOD); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author EndUser */ public class E35PD { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } int count = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) { count++; } } } boolean isEven = (count % 2 == 0); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int m = in.nextInt(); for (int i = 0; i < m; i++) { int l = in.nextInt(); int r = in.nextInt(); int size = (r - l) + 1; int numOfConn = (size - 1) * size / 2; if (numOfConn % 2 == 1) { isEven = !isEven; } if (isEven) { out.write("even"); out.newLine(); } else { out.write("odd"); out.newLine(); } } out.close(); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.*; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int a[]=new int[n]; for(int i=0; i<n; i++) a[i]=in.nextInt(); long no=0; for(int i=0; i<n-1; i++) { for(int j=i+1; j<n; j++) { if(a[i]>a[j]) no++; } } // System.out.println(no); no%=2; int m=in.nextInt(); int te; String te2="odd",te1="even"; for(int i=0; i<m; i++) { te=in.nextInt()-in.nextInt(); te=-te+1; // System.out.println(te); if((te*(te-1)/2)%2==0) { // System.out.println("HOLA"+no); if(no==0) System.out.println("even"); else System.out.println("odd"); } else { no=(no+1)%2; if(no==0) System.out.println("even"); else System.out.println("odd"); } } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class CE35D { public static void main(String[] args) throws NumberFormatException, IOException { //Scanner sc = new Scanner(System.in); BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(sc.readLine()); String[] t = sc.readLine().split(" "); int[] list = new int[n]; for(int x=0; x<n; x++){ list[x] = Integer.parseInt(t[x]); } boolean even = true; int[] indList = new int[n+1]; for(int x=0; x<n; x++){ indList[list[x]] = x; } for(int x=1; x<=n; x++){ int theIndex = indList[x]; int other = list[x-1]; if(theIndex != x-1){ even = !even; list[x-1] = x; list[theIndex] = other; indList[x] = x-1; indList[other] = theIndex; } } //System.out.println(even); int numQ = Integer.parseInt(sc.readLine()); for(int x=0; x<numQ; x++){ String[] dir = sc.readLine().split(" "); int l = Integer.parseInt(dir[0]); int r = Integer.parseInt(dir[1]); int diff = r - l + 1; if(diff%4 > 1){ even = !even; } if(even){ System.out.println("even"); } else{ System.out.println("odd"); } } } } /* int n = Integer.parseInt(sc.nextLine()); String[] t = sc.nextLine().split(" "); int[] list = new int[n]; for(int x=0; x<n; x++){ list[x] = Integer.parseInt(t[x]); } String[] dir = sc.nextLine().split(" "); int a = Integer.parseInt(dir[0]); int b = Integer.parseInt(dir[1]); int c = Integer.parseInt(dir[2]); int d = Integer.parseInt(dir[3]); int e = Integer.parseInt(dir[4]); int n = Integer.parseInt(sc.nextLine()); */
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main { final static int mod = 1_000_000_007; public static void main(String[] args) throws Exception { STDIN scan = new STDIN(); PrintWriter pw = new PrintWriter(System.out); int n = scan.nextInt(); boolean even = true; int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = scan.nextInt(); for(int j = 0; j < i; j++) if(a[i] < a[j]) even = !even; } int q = scan.nextInt(); while(q-- > 0) { int l = scan.nextInt(), r = scan.nextInt(); int len = r - l + 1; int permutations = len * (len - 1) / 2; if(permutations % 2 != 0) even = !even; pw.println(even ? "even" : "odd"); } pw.flush(); } static class STDIN { BufferedReader br; StringTokenizer st; public STDIN() { br = new BufferedReader(new InputStreamReader(System.in)); st = null; } boolean hasNext() throws Exception { if (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.hasMoreTokens(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } double nextDouble() throws Exception { return Double.parseDouble(next()); } String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws Exception { return br.readLine(); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.readInt(); int[] a = in.readIntArray(n); int swap = 0; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (a[i] > a[j]) swap ^= 1; int m = in.readInt(); while (m-- > 0) { int l = in.readInt(); int r = in.readInt(); int s = (r - l + 1); s = s * (s - 1) / 2; swap ^= s; out.println((swap & 1) == 0 ? "even" : "odd"); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] readIntArray(int size) { int[] ans = new int[size]; for (int i = 0; i < size; i++) ans[i] = readInt(); return ans; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
/* * PDPM IIITDM Jabalpur * Asutosh Rana */ import java.util.*; import java.io.*; import java.math.*; public class Main { long MOD = 1000000007; InputReader in; BufferedReader br; PrintWriter out; public static void main(String[] args) throws java.lang.Exception { Main solver = new Main(); solver.in = new InputReader(System.in); solver.br = new BufferedReader(new InputStreamReader(System.in)); solver.out = new PrintWriter(System.out); solver.solve(); solver.out.flush(); solver.out.close(); } public void solve() { int tc = 1;//in.readInt(); for (int cas = 1; cas <= tc; cas++) { int N = in.readInt(); int[] A = new int[N]; in.readInt(A); int res = 0; for(int i=0;i<N;i++){ for(int j=i+1;j<N;j++){ if(A[i]>A[j]) res++; } } res = res % 2; int Q = in.readInt(); while(Q-->0){ int l = in.readInt(); int r = in.readInt(); int add = (r-l+1)/2; res = res + add; res%=2; out.println(res%2==0?"even":"odd"); } } } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public void readInt(int[] A) { for (int i = 0; i < A.length; i++) A[i] = readInt(); } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public void readLong(long[] A) { for (int i = 0; i < A.length; i++) A[i] = readLong(); } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public char[] readCharA() { return readString().toCharArray(); } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
quadratic
911_D. Inversion Counting
CODEFORCES
//~~~~~~~~~~~~~~~~~~~~~~~@@@@@@@@@@@@@@@_____________K_____S_____J__________@@@@@@@@@@@@@@@@@@@@@@@@@@@@~~~~~~~~~~~~~~~~~~~~~~~~~~ //Date:00/00/17 //------------- import java.util.*; import java.io.*; /* [[[[[[[[[[[[[[[ ]]]]]]]]]]]]]]] [:::::::::::::: ::::::::::::::] [:::::::::::::: ::::::::::::::] [::::::[[[[[[[: :]]]]]]]::::::] [:::::[ ]:::::] [:::::[ ]:::::] [:::::[ ]:::::] [:::::[ ]:::::] [:::::[ CODE YOUR LIFE ]:::::] [:::::[ Kripa Shankar jha ]:::::] [:::::[ ]:::::] [:::::[ ]:::::] [:::::[ ]:::::] [:::::[ ]:::::] [::::::[[[[[[[: :]]]]]]]::::::] [:::::::::::::: ::::::::::::::] [:::::::::::::: ::::::::::::::] [[[[[[[[[[[[[[[ ]]]]]]]]]]]]]]] */ public class prob3{ static Parser sc=new Parser(System.in); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static int p[]=new int[100005]; public static void main(String[] args) throws IOException { // use ((((((( sc ............... for input int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } int swap=0; for(int i=0;i<n;i++){ for(int j=0;j<i;j++){ if(arr[i]<arr[j]){ swap++; } } } swap%=2; int m=sc.nextInt(); for(int i=0;i<m;i++){ int a=sc.nextInt(),b=sc.nextInt(); swap+=((b-a)*((b-a)+1))/2; swap%=2; if(swap%2==0){System.out.println("even");} else{System.out.println("odd");} } } public static void union(int a,int b){ int i=find(a); int j=find(b); if(p[i]!=j){ p[i]=j; } } public static int find(int a){ while(p[a]!=a){ a=p[a]; } return a; } //___________________________Fast-Input_Output-------------------******************* static class Parser { final private int BUFFER_SIZE = 1 << 20; // 2^16, a good compromise for some problems private InputStream din; // Underlying input stream private byte[] buffer; // Self-maintained buffer private int bufferPointer; // Current read position in the buffer private int bytesRead; // Effective bytes in the buffer read from the input stream private SpaceCharFilter filter; public Parser(InputStream in) { din = in; buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } /** * Read the next integer from the input stream. * @return The next integer. * @throws IOException */ public int nextInt() throws IOException { int result = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); while (c >= '0' && c <= '9') { result = result * 10 + c - '0'; c = read(); } if (neg) return -result; return result; } public int nextLong() throws IOException { int result = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); while (c >= '0' && c <= '9') { result = result * 10 + c - '0'; c = read(); } if (neg) return -result; return result; } public String nextLine()throws IOException { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)==true); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } /** * Read the next byte of data from the input stream. * @return the next byte of data, or -1 if the end of the stream is reached. * @throws IOException if an I/O error occurs. */ public byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } /** * Read data from the input stream into the buffer * @throws IOException if an I/O error occurs. */ private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.Scanner; public class D2 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int array[] =new int[n]; for(int i=0; i<=n-1; i++) { array[i] = sc.nextInt(); } int m = sc.nextInt(); int result = count(array); for(int i=1; i<=m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); result += (b-a)*(b-a+1)/2; result=result%2; if(result%2==1) System.out.println("odd"); else System.out.println("even"); } } public static int count(int[] arr) { int[] array = arr.clone(); return sort(array,0,array.length-1); } public static int sort(int[] arr, int i, int j) { if(i>=j) return 0; int mid = (i+j)/2; int a = sort(arr,i,mid); int b = sort(arr,mid+1,j); int addition = 0; int r1 = mid+1; int[] tmp = new int[arr.length]; int tIndex = i; int cIndex=i; while(i<=mid&&r1<=j) { if (arr[i] <= arr[r1]) tmp[tIndex++] = arr[i++]; else { tmp[tIndex++] = arr[r1++]; addition+=mid+1-i; } } while (i <=mid) { tmp[tIndex++] = arr[i++]; } while ( r1 <= j ) { tmp[tIndex++] = arr[r1++]; } while(cIndex<=j){ arr[cIndex]=tmp[cIndex]; cIndex++; } return a+b+addition; } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.InputStream; import java.io.PrintStream; import java.util.Scanner; /** */ public class Main911D { public static void main(String[] args) { run(System.in, System.out); } public static void run(InputStream in, PrintStream out) { try (Scanner sc = new Scanner(in)) { int n = sc.nextInt(); int[] t = new int[n]; int inv = 0; for (int i = 0; i < n; i++) { t[i] = sc.nextInt(); for (int j = 0; j < i; j++) { if (t[j] > t[i]) inv++; } } inv = inv % 2; int m = sc.nextInt(); for (int i = 0; i < m; i++) { int a = sc.nextInt(); int b = sc.nextInt(); int s = b - a + 1; inv = (inv + s * (s - 1) / 2) % 2; out.println(inv == 0 ? "even" : "odd"); } } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.Scanner; public class inversion__count { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n=s.nextInt(); int[] a = new int[n+1]; for(int i=1;i<=n;i++){ a[i]=s.nextInt(); } int m=s.nextInt(); int count=0; for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ if(a[i]>a[j]){ count++; } } } if(count%2==0){ count=0; }else{ count=1; } //System.out.println(count); for(int i=0;i<m;i++){ int l=s.nextInt(); int r=s.nextInt(); if(l==r){ if((count&1)==1){ System.out.println("odd"); }else{ System.out.println("even"); } continue; } int d=r-l+1; int segcount = 0; int temp = (d*(d-1))/2; if((temp&1)==1 && (count&1)==1){ count=0; System.out.println("even"); }else if((temp&1)==1 && (count&1)==0){ count=1; System.out.println("odd"); }else{ if((count&1)==1){ System.out.println("odd"); }else{ System.out.println("even"); } } } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.* ; import java.util.* ; import java.text.* ; import java.math.* ; import static java.lang.Math.min ; import static java.lang.Math.max ; public class Codeshefcode{ public static void main(String[] args) throws IOException{ // Solver Machine = new Solver() ; // Machine.Solve() ; // Machine.Finish() ; new Thread(null,new Runnable(){ public void run(){ Solver Machine = new Solver() ; try{ Machine.Solve() ; Machine.Finish() ; }catch(Exception e){ e.printStackTrace() ; System.out.flush() ; System.exit(-1) ; }catch(Error e){ e.printStackTrace() ; System.out.flush() ; System.exit(-1) ; } } },"Solver",1l<<27).start() ; } } class Mod{ static long mod=1000000007 ; static long d(long a,long b){ return (a*MI(b))%mod ; } static long m(long a,long b){ return (a*b)%mod ; } static private long MI(long a){ return pow(a,mod-2) ; } static long pow(long a,long b){ if(b<0) return pow(MI(a),-b) ; long val=a ; long ans=1 ; while(b!=0){ if((b&1)==1) ans = (ans*val)%mod ; val = (val*val)%mod ; b/=2 ; } return ans ; } } class pair implements Comparable<pair>{ int x ; int y ; pair(int x,int y){ this.x=x ; this.y=y ;} public int compareTo(pair p){ return (this.x<p.x ? -1 : (this.x>p.x ? 1 : (this.y<p.y ? -1 : (this.y>p.y ? 1 : 0)))) ; } } class Solver{ Reader ip = new Reader(System.in) ; PrintWriter op = new PrintWriter(System.out) ; public void Solve() throws IOException{ int n = ip.i() ; int a[] = new int[n] ; for(int i=0 ; i<n ; i++) a[i] = ip.i() ; int num=0 ; for(int i=0 ; i<n ; i++) for(int j=(i+1) ; j<n ; j++) if(a[i]>a[j]) num++ ; num%=2 ; int m = ip.i() ; while(m--!=0){ int l = ip.i() ; int r = ip.i() ; int d = (r-l+1) ; int mod = d%4 ; int bit ; if(mod<=1) bit=0 ; else bit=1 ; num+=bit ; num%=2 ; pln(num==1 ? "odd" : "even") ; } } void Finish(){ op.flush(); op.close(); } void p(Object o){ op.print(o) ; } void pln(Object o){ op.println(o) ; } } class mylist extends ArrayList<Integer>{} class myset extends TreeSet<Integer>{} class mystack extends Stack<Integer>{} class mymap extends TreeMap<Long,Integer>{} class Reader { BufferedReader reader; StringTokenizer tokenizer; Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer("") ; } String s() throws IOException { while (!tokenizer.hasMoreTokens()){ tokenizer = new StringTokenizer( reader.readLine()) ; } return tokenizer.nextToken(); } int i() throws IOException { return Integer.parseInt(s()) ; } long l() throws IOException{ return Long.parseLong(s()) ; } double d() throws IOException { return Double.parseDouble(s()) ; } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.*; import java.io.*; public class ProblemD { static int mod = (int) (1e9+7); static InputReader in; static PrintWriter out; static void update(int i, int val, int[] bit){ for(; i < bit.length; i += (i&-i)) bit[i] += val; } static int query(int i, int[] bit){ int ans=0; for(; i>0; i -= (i&-i)) ans += bit[i]; return ans; } static int get(int l, int r, int[] bit){ if(l > r) return 0; return query(r, bit) - query(l - 1, bit); } static void solve() { in = new InputReader(System.in); out = new PrintWriter(System.out); int n = in.nextInt(); int[] arr = new int[n + 1]; int[] bit = new int[n + 2]; for(int i = 1; i <= n; i++){ arr[i] = in.nextInt(); } int cnt = 0; for(int i = n; i > 0; i--){ cnt += query(arr[i], bit); update(arr[i], 1, bit); } cnt %= 2; int q = in.nextInt(); while(q-- > 0){ int l = in.nextInt(); int r = in.nextInt(); int length = r - l + 1; int x = (length * (length - 1)) / 2; x %= 2; cnt ^= x; out.println(cnt == 0 ? "even" : "odd"); } out.close(); } public static void main(String[] args) { new Thread(null ,new Runnable(){ public void run(){ try{ solve(); } catch(Exception e){ e.printStackTrace(); } } },"1",1<<26).start(); } static class Pair implements Comparable<Pair> { long x,y; Pair (long x,long y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return Long.compare(this.x,o.x); //return 0; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair)o; return p.x == x && p.y==y; } return false; } @Override public String toString() { return x + " "+ y ; } /*public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); }*/ } static long add(long a,long b){ long x=(a+b); while(x>=mod) x-=mod; return x; } static long sub(long a,long b){ long x=(a-b); while(x<0) x+=mod; return x; } static long mul(long a,long b){ long x=(a*b); while(x>=mod) x-=mod; return x; } static String rev(String s){ StringBuilder sb=new StringBuilder(s); sb.reverse(); return sb.toString(); } static long gcd(long x,long y) { if(y==0) return x; else return gcd(y,x%y); } static int gcd(int x,int y) { if(y==0) return x; else return gcd(y,x%y); } static long pow(long n,long p,long m) { long result = 1; if(p==0){ return 1; } while(p!=0) { if(p%2==1) result *= n; if(result >= m) result %= m; p >>=1; n*=n; if(n >= m) n%=m; } return result; } static long pow(long n,long p) { long result = 1; if(p==0) return 1; while(p!=0) { if(p%2==1) result *= n; p >>=1; n*=n; } return result; } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
//package codeforces.Educational35; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class D { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int inv = 0; int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) arr[i] = sc.nextInt(); for (int i = 0; i < arr.length; i++) for (int j = i+1; j < arr.length; j++) if(arr[i] > arr[j]) inv++; boolean odd = (inv%2)!=0; int q = sc.nextInt(); for (int i = 0; i < q; i++) { int l = sc.nextInt(); int r = sc.nextInt(); int sz = r-l+1; int tot = (sz*(sz-1))/2; if(tot%2 != 0) odd = !odd; if(odd) pw.println("odd"); else pw.println("even"); } pw.flush(); pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File((s)))); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.*; import java.io.*; import java.math.BigInteger; public class D { FastScanner in; PrintWriter out; boolean systemIO = true; public void solve() { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = in.nextInt(); } int x = 0; for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] > a[j]) { x++; } } } boolean ans = x % 2 == 0; int m = in.nextInt(); for (int i = 0; i < m; i++) { int len = -in.nextInt() + in.nextInt(); len = len * (len + 1) / 2; if (len % 2 == 1) { ans = !ans; } if (ans) { out.println("even"); } else { out.println("odd"); } } } public void run() { try { if (systemIO) { in = new FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new FastScanner(new File("segments.in")); out = new PrintWriter(new File("segments.out")); } solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA public static void main(String[] arg) { new D().run(); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; // import java.awt.Point; public class Main { InputStream is; PrintWriter out; String INPUT = ""; long MOD = 1_000_000_007; int inf = Integer.MAX_VALUE; void solve() { int n = ni(); int[] a = new int[n]; for(int i = 0; i < n; i++){ a[i] = ni(); } long ans = 0; for(int i = 0; i < n; i++){ for(int j = i+1; j < n; j++){ if(a[j]<a[i]) ans++; } } if(ans%2==0) ans = 0; else ans = 1; int m = ni(); for(int i = 0; i < m; i++){ long s = nl(); long g = nl(); long sub = g-s; long res = sub*(sub+1)/2; if(res%2==1) ans = 1 - ans; if(ans==0) out.println("even"); else out.println("odd"); } } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b) && b != ' ')){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class D { public static void solve(FastScanner fs) { int n=fs.nextInt(); int[] a=fs.readArray(n); boolean even=((countInversions(a)&1)==0); int q=fs.nextInt(); for (int i=0; i<q; i++) { int start=fs.nextInt(); int end=fs.nextInt(); int diff=end-start; boolean evenChange=(diff+1)/2%2==0; even^=!evenChange; System.out.println(even?"even":"odd"); } } private static int countInversions(int[] a) { int c=0; for (int i=0; i<a.length; i++) { for (int j=i+1; j<a.length; j++) if (a[j]<a[i]) c++; } return c; } public static void main(String[] args) throws NumberFormatException, IOException { FastScanner scanner = new FastScanner(System.in); solve(scanner); } private static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
quadratic
911_D. Inversion Counting
CODEFORCES
//package Educational35; //import FastScanner.Competitive; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class D { String INPUT = "4\n" + "1 2 4 3\n" + "4\n" + "1 1\n" + "1 4\n" + "1 4\n" + "2 3"; void solve() { int n = i(); int[] a = ia(n); int count = 0 ; for(int i = 0 ; i<n-1 ; i++) { for(int j = i+1; j<n ; j++) { if(a[j]<a[i]) { count++; } } } int q = i(); for(int i = 0 ; i<q ; i++) { int l = i(), r=i(); int mid = (r-l+1)/2; if(mid%2==0) { out.println(count%2==0?"even":"odd"); } else { count++; out.println(count%2==0?"even":"odd"); } } } /////////////////////////////////////// void run() throws Exception{ is = oj ? System.in: new ByteArrayInputStream(INPUT.getBytes()); //is = System.in; out = new PrintWriter(System.out); int t = 1; while(t-->0) solve(); out.flush(); } public static void main(String[] args)throws Exception { new D().run(); } InputStream is; PrintWriter out; private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double d() { return Double.parseDouble(s()); } private char c() { return (char)skip(); } private String s() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] sa(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = sa(m); return map; } private int[] ia(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = i(); return a; } private int i() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long l() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.awt.Checkbox; import java.awt.Point; import java.io.*; import java.math.*; import java.util.*; import java.util.Map.Entry; import javax.print.attribute.SetOfIntegerSyntax; import javax.swing.plaf.FontUIResource; public class CODE2{ private static InputStream stream; private static byte[] buf = new byte[1024]; private static int curChar,MAX; private static int numChars; private static SpaceCharFilter filter; private static PrintWriter pw; private static long count = 0,mod=1000000007; static int BIT[]; private static boolean primer[]; // private static TreeSet<Integer> ts=new TreeSet[200000]; public final static int INF = (int) 1E9; public static void main(String args[]) { InputReader(System.in); pw = new PrintWriter(System.out); new Thread(null ,new Runnable(){ public void run(){ try{ solve(); pw.close(); } catch(Exception e){ e.printStackTrace(); } } },"1",1<<26).start(); } static StringBuilder sb; public static void test(){ sb=new StringBuilder(); int t=nextInt(); while(t-->0){ solve(); } pw.println(sb); } public static long pow(long n, long p,long mod) { if(p==0) return 1; if(p==1) return n%mod; if(p%2==0){ long temp=pow(n, p/2,mod); return (temp*temp)%mod; }else{ long temp=pow(n,p/2,mod); temp=(temp*temp)%mod; return(temp*n)%mod; } } public static long pow(long n, long p) { if(p==0) return 1; if(p==1) return n; if(p%2==0){ long temp=pow(n, p/2); return (temp*temp); }else{ long temp=pow(n,p/2); temp=(temp*temp); return(temp*n); } } public static void Merge(long a[],int p,int r){ if(p<r){ int q = (p+r)/2; Merge(a,p,q); Merge(a,q+1,r); Merge_Array(a,p,q,r); } } public static void Merge_Array(long a[],int p,int q,int r){ long b[] = new long[q-p+1]; long c[] = new long[r-q]; for(int i=0;i<b.length;i++) b[i] = a[p+i]; for(int i=0;i<c.length;i++) c[i] = a[q+i+1]; int i = 0,j = 0; for(int k=p;k<=r;k++){ if(i==b.length){ a[k] = c[j]; j++; } else if(j==c.length){ a[k] = b[i]; i++; } else if(b[i]<c[j]){ a[k] = b[i]; i++; } else{ a[k] = c[j]; j++; } } } public static long gcd(long x, long y) { if (x == 0) return y; else return gcd( y % x,x); } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static LinkedList<Integer> adj[]; static boolean Visited[]; static HashSet<Integer> exc; static long oddsum[]=new long[1000001]; static long co=0,ans=0; static int parent[]; static int size[],color[],res[],k; static ArrayList<Integer> al[]; static long MOD = (long)1e9 + 7; private static void buildgraph(int n){ adj=new LinkedList[n+1]; Visited=new boolean[n+1]; levl=new int[n+1]; for(int i=0;i<=n;i++){ adj[i]=new LinkedList<Integer>(); } } static int[] levl; static int[] eat; // static int n,m; static int price[]; //ind frog crab static boolean check(char c) { if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u' ) return true; else return false; } public static void solve(){ int n= nextInt(); int a[]=new int[n]; a=nextIntArray(n); int invcount=0; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(a[i]>a[j]) invcount++; } } int m=nextInt(); int initial = invcount%2; while(m--!=0) { int l=nextInt(); int r=nextInt(); int diff = r-l+1; int totalpair = (diff*(diff-1))/2; if(((totalpair%2)+initial)%2==1) { pw.println("odd"); initial=1; } else { pw.println("even"); initial=0; } } } static void seive2(int n) { primer=new boolean[n+1]; Arrays.fill(primer,true); primer[0]=false; primer[1]=false; primer[2]=true; for(int i=2;i*i<=n;i++) { if(primer[i]) { for(int j=2*i;j<=n;j=j+i) { primer[j]=false; } } } } /* static void BITupdate(int x,int val) { while(x<=n) { BIT[x]+=val; x+= x & -x; } }*/ /* static void update(int x,long val) { val=val%MOD; while(x<=n) { // System.out.println(x); BIT[x]=(BIT[x]+val)%MOD; x+=(x & -x); } // System.out.println("dfd"); }*/ static int BITsum(int x) { int sum=0; while(x>0) { sum+=BIT[x]; x-= (x & -x); } return sum; } /* static long sum(int x) { long sum=0; while(x>0) { sum=(sum+BIT[x])%MOD; x-=x & -x; } return sum; }*/ static boolean union(int x,int y) { int xr=find(x); int yr=find(y); if(xr==yr) return false; if(size[xr]<size[yr]) { size[yr]+=size[xr]; parent[xr]=yr; } else { size[xr]+=size[yr]; parent[yr]=xr; } return true; } static int find(int x) { if(parent[x]==x) return x; else { parent[x]=find(parent[x]); return parent[x]; } } public static class Edge implements Comparable<Edge> { int u, v,s; public Edge(int u, int v) { this.u = u; this.v = v; //this.s = s; } public int hashCode() { return Objects.hash(); } public int compareTo(Edge other) { return (Integer.compare(u, other.u) != 0 ? (Integer.compare(u, other.u)):(Integer.compare(v, other.v))); // &((Long.compare(s, other.s) != 0 ? (Long.compare(s, other.s)):(Long.compare(u, other.v)!=0?Long.compare(u, other.v):Long.compare(v, other.u)))); //return this.u-other.u; } public String toString() { return this.u + " " + this.v; } } static int col[]; public static boolean isVowel(char c){ if(c=='a' || c=='e'||c=='i' || c=='o' || c=='u') return true; return false; } static int no_vert=0; private static void dfs(int start){ Visited[start]=true; if(al[color[start]].size()>=k) { res[start]=al[color[start]].get(al[color[start]].size()-k); } al[color[start]].add(start); for(int i:adj[start]){ if(!Visited[i]) { dfs(i); } } (al[color[start]]).remove(al[color[start]].size()-1); } public static String reverseString(String s) { StringBuilder sb = new StringBuilder(s); sb.reverse(); return (sb.toString()); } /* private static void BFS(int sou){ Queue<Integer> q=new LinkedList<Integer>(); q.add(sou); Visited[sou]=true; levl[sou]=0; while(!q.isEmpty()){ int top=q.poll(); for(int i:adj[top]){ //pw.println(i+" "+top); if(!Visited[i]) { q.add(i); levl[i]=levl[top]+1; } Visited[i]=true; } } }*/ static int indeg[]; /* private static void kahn(int n){ PriorityQueue<Integer> q=new PriorityQueue<Integer>(); for(int i=1;i<=n;i++){ if(indeg[i]==0){ q.add(i); } } while(!q.isEmpty()){ int top=q.poll(); st.push(top); for(Node i:adj[top]){ indeg[i.to]--; if(indeg[i.to]==0){ q.add(i.to); } } } } static int state=1; static long no_exc=0,no_vert=0; static Stack<Integer> st; static HashSet<Integer> inset; /* private static void topo(int curr){ Visited[curr]=true; inset.add(curr); for(int x:adj[curr]){ if(adj[x].contains(curr) || inset.contains(x)){ state=0; return; } if(state==0) return; } st.push(curr); inset.remove(curr); }*/ static HashSet<Integer> hs; static boolean prime[]; static int spf[]; public static void sieve(int n){ prime=new boolean[n+1]; spf=new int[n+1]; Arrays.fill(spf, 1); Arrays.fill(prime, true); prime[1]=false; spf[2]=2; for(int i=4;i<=n;i+=2){ spf[i]=2; } for(int i=3;i<=n;i+=2){ if(prime[i]){ spf[i]=i; for(int j=2*i;j<=n;j+=i){ prime[j]=false; if(spf[j]==1){ spf[j]=i; } } } } } // To Get Input // Some Buffer Methods public static void sort(long a[]){ Merge(a, 0, a.length-1); } public static void InputReader(InputStream stream1) { stream = stream1; } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private static boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } private static int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } private static int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private static String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } private static int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } private static long[][] next2dArray(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = nextLong(); } } return arr; } private static char[][] nextCharArray(int n,int m){ char [][]c=new char[n][m]; for(int i=0;i<n;i++){ String s=nextLine(); for(int j=0;j<s.length();j++){ c[i][j]=s.charAt(j); } } return c; } private static long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private static void pArray(int[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static void pArray(long[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static void pArray(boolean[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
quadratic
911_D. Inversion Counting
CODEFORCES
// discussed with rainboy import java.io.*; import java.util.*; public class CF911D { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int[] aa = new int[n]; for (int i = 0; i < n; i++) aa[i] = Integer.parseInt(st.nextToken()); boolean odd = false; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (aa[i] > aa[j]) odd = !odd; int m = Integer.parseInt(br.readLine()); while (m-- > 0) { st = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st.nextToken()); int r = Integer.parseInt(st.nextToken()); int k = r - l + 1; if ((long) k * (k - 1) / 2 % 2 != 0) odd = !odd; pw.println(odd ? "odd" : "even"); } pw.close(); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class D911{ void solve() { int n = ni(); int[] a = ia(n); int Q = ni(); String[] ans = {"even", "odd"}; int cur = merge(a, 0, n - 1) % 2; while(Q-->0) { int l = ni(), r = ni(); cur ^= (r - l + 1) / 2 % 2; out.println(ans[cur]); } } int merge(int[] a, int l, int r) { if(l >= r) return 0; int mid = l + r >> 1; int v1 = merge(a, l, mid); int v2 = merge(a, mid + 1, r); int[] rep = new int[r-l+1]; int ptr0 = 0, ptr1 = l, ptr2 = mid + 1; long len = mid-l+1; int ret = 0; while(ptr1<=mid && ptr2<=r) { if(a[ptr1] <= a[ptr2]) { len--; rep[ptr0++] = a[ptr1++]; } else { ret += len; rep[ptr0++] = a[ptr2++]; } } while(ptr1 <= mid) rep[ptr0++] = a[ptr1++]; while(ptr2 <= r) rep[ptr0++] = a[ptr2++]; for(int i=0;i<ptr0;i++) a[l+i] = rep[i]; return v1 + v2 + ret; } public static void main(String[] args){new D911().run();} private byte[] bufferArray = new byte[1024]; private int bufLength = 0; private int bufCurrent = 0; InputStream inputStream; PrintWriter out; public void run() { inputStream = System.in; out = new PrintWriter(System.out); solve(); out.flush(); } int nextByte() { if(bufLength == -1) throw new InputMismatchException(); if(bufCurrent >= bufLength) { bufCurrent = 0; try {bufLength = inputStream.read(bufferArray);} catch(IOException e) { throw new InputMismatchException();} if(bufLength <= 0) return -1; } return bufferArray[bufCurrent++]; } boolean isSpaceChar(int x) {return (x < 33 || x > 126);} boolean isDigit(int x) {return (x >= '0' && x <= '9');} int nextNonSpace() { int x; while((x=nextByte()) != -1 && isSpaceChar(x)); return x; } int ni() { long ans = nl(); if (ans >= Integer.MIN_VALUE && ans <= Integer.MAX_VALUE) return (int)ans; throw new InputMismatchException(); } long nl() { long ans = 0; boolean neg = false; int x = nextNonSpace(); if(x == '-') { neg = true; x = nextByte(); } while(!isSpaceChar(x)) { if(isDigit(x)) { ans = ans * 10 + x -'0'; x = nextByte(); } else throw new InputMismatchException(); } return neg ? -ans : ans; } String ns() { StringBuilder sb = new StringBuilder(); int x = nextNonSpace(); while(!isSpaceChar(x)) { sb.append((char)x); x = nextByte(); } return sb.toString(); } char nc() { return (char)nextNonSpace();} double nd() { return (double)Double.parseDouble(ns()); } char[] ca() { return ns().toCharArray();} char[][] ca(int n) { char[][] ans = new char[n][]; for(int i=0;i<n;i++) ans[i] = ca(); return ans; } int[] ia(int n) { int[] ans = new int[n]; for(int i=0;i<n;i++) ans[i] = ni(); return ans; } void db(Object... o) {System.out.println(Arrays.deepToString(o));} }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; // import java.awt.Point; public class Main { InputStream is; PrintWriter out; String INPUT = ""; long MOD = 1_000_000_007; int inf = Integer.MAX_VALUE; void solve() { int n = ni(); int[] a = new int[n]; for(int i = 0; i < n; i++){ a[i] = ni(); } long ans = 0; for(int i = 0; i < n; i++){ for(int j = i+1; j < n; j++){ if(a[j]<a[i]) ans++; } } if(ans%2==0) ans = 0; else ans = 1; int m = ni(); for(int i = 0; i < m; i++){ long s = nl(); long g = nl(); long sub = g-s; long res = sub*(sub+1)/2; if(res%2==1) ans = 1 - ans; if(ans==0) out.println("even"); else out.println("odd"); } } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b) && b != ' ')){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = s.nextInt(); } int parity = 0; for(int i = 0; i < n; i++) { int count = 0; for(int j = i + 1; j < n; j++) { if(arr[j] < arr[i]) { parity ^= 1; } } } int m = s.nextInt(); for(int i = 0; i < m; i++) { int l = s.nextInt(), r = s.nextInt(); if(((r - l + 1) / 2) % 2 == 1) { parity ^= 1; } System.out.println(parity == 1 ? "odd" : "even"); } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.Scanner; public class d { public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); int[] vals = new int[size]; long[] cum = new long[size]; for(int i=0; i<size; i++){ vals[i] = in.nextInt(); int c = 0; for(int j=0; j<i; j++) if(vals[j] > vals[i]) c++; if(i != 0) cum[i] = cum[i-1]+c; else cum[i] = c; } long tot = cum[size-1]; int q = in.nextInt(); int[] nv = new int[size]; for(int i=0; i<q; i++) { int l = in.nextInt()-1; int r = in.nextInt()-1; int n = (r-l); long add = (n*(n+1))/2 - (cum[r] - cum[l]); tot = tot - (cum[r] - cum[l]) + add; if(tot%2 == 0) System.out.println("even"); else System.out.println("odd"); // for(int j=0; j<=r-l; j++) // nv[l+j] = vals[r-j]; // // for(int j=0; j<=r-l; j++) // vals[l+j] = nv[l+j]; } } } /* 3 1 2 3 2 1 2 2 3 4 1 2 4 3 4 1 1 1 4 1 4 2 3 */
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.*; import java.util.*; public class DD { public static void main(String[] args)throws Throwable { MyScanner sc=new MyScanner(); PrintWriter pw=new PrintWriter(System.out); int n=sc.nextInt(); int [] a=new int [n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); int c=0; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) if(a[i]>a[j]) c^=1; int m=sc.nextInt(); while(m-->0){ int l=sc.nextInt()-1; int r=sc.nextInt()-1; int d=r-l+1; d=d*(d-1)/2; c^=(d%2); pw.println(c==0? "even" : "odd"); } pw.flush(); pw.close(); } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() {while (st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}} return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} String nextLine(){String str = ""; try {str = br.readLine();} catch (IOException e) {e.printStackTrace();} return str;} } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.*; import java.io.*; public class InversionCounting { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = Integer.parseInt(sc.nextLine()); int inversions = 0; int[] data = new int[n]; StringTokenizer st = new StringTokenizer(sc.nextLine()); for(int i = 0; i < n; i ++) { data[i] = Integer.parseInt(st.nextToken()); } for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(data[i] > data[j]) inversions++; } } //false = 0, true = 1; boolean inversiontype = (inversions % 2 == 1); int n2 = Integer.parseInt(sc.nextLine()); for(int i = 0; i < n2; i++) { st = new StringTokenizer(sc.nextLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); int parity = (b-a)*(b - a + 1)/2; if(parity % 2 == 0) { if(inversiontype) pw.println("odd"); else pw.println("even"); } else { inversiontype = !inversiontype; if(inversiontype) pw.println("odd"); else pw.println("even"); } } pw.close(); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.*; public class inversioncounting { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] permutation = new int[n]; for (int i = 0; i < n; i++) { permutation[i] = sc.nextInt(); } int m = sc.nextInt(); int[][] reverse = new int[m][2]; for (int i = 0; i < m; i++) { reverse[i][0] = sc.nextInt(); reverse[i][1] = sc.nextInt(); } int counter = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (permutation[i] > permutation[j]) { counter++; } } } boolean bayus = true; if (counter % 2 == 1) { bayus = false; } for (int i = 0; i < m; i++) { int bobib = reverse[i][1] - reverse[i][0] + 1; int bafry = nChoose2(bobib); if (bafry%2 == 1) { bayus = !bayus; } if (bayus) { System.out.println("even"); } else { System.out.println("odd"); } } } private static int nChoose2 (int n) { return (n * (n-1)) / 2; } }
quadratic
911_D. Inversion Counting
CODEFORCES
//package Educational35; //import FastScanner.Competitive; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class D { String INPUT = "4\n" + "1 2 4 3\n" + "4\n" + "1 1\n" + "1 4\n" + "1 4\n" + "2 3"; void solve() { final int n = i(); final int[] a = ia(n); int count = 0 ; for(int i = 0 ; i<n-1 ; i++) { for(int j = i+1; j<n ; j++) { if(a[j]<a[i]) { count++; } } } final int q = i(); for(int i = 0 ; i<q ; i++) { final int l = i(), r=i(); final int mid = (r-l+1)/2; if(mid%2==0) { out.println(count%2==0?"even":"odd"); } else { count++; out.println(count%2==0?"even":"odd"); } } } /////////////////////////////////////// void run() throws Exception{ is = oj ? System.in: new ByteArrayInputStream(INPUT.getBytes()); //is = System.in; out = new PrintWriter(System.out); int t = 1; while(t-->0) solve(); out.flush(); } public static void main(String[] args)throws Exception { new D().run(); } InputStream is; PrintWriter out; private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double d() { return Double.parseDouble(s()); } private char c() { return (char)skip(); } private String s() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] sa(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = sa(m); return map; } private int[] ia(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = i(); return a; } private int i() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long l() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class P911D { public static void main(String[] args) { FastScanner scan = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int n = scan.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = scan.nextInt(); int inv = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { if (arr[i] > arr[j]) inv++; } } inv &= 1; int[] cumul = new int[n+1]; for (int i = 2; i < cumul.length; i++) { cumul[i] = cumul[i-1] + i-1; } int q = scan.nextInt(); for (int i = 0; i < q; i++) { int a = scan.nextInt()-1; int b = scan.nextInt()-1; inv += cumul[b-a+1]; inv &= 1; if (inv == 0) pw.println("even"); else pw.println("odd"); } pw.flush(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } public String next() { if (st.hasMoreTokens()) return st.nextToken(); try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { String line = ""; try { line = br.readLine(); } catch (Exception e) { e.printStackTrace(); } return line; } } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; public class A{ void solve(){ int n=ni(); int P[]=new int[n+1]; for(int i=1;i<=n;i++) P[i]=ni(); a=new int[n+1]; BIT=new long[n+1]; long cnt=0; long p=0; for(int i=n;i>=1;i--){ p+=querry(P[i]); if(p>=M) p%=M; update(n,P[i],1); } int d=0; if(p%2==0) d=1; int m=ni(); while(m-->0){ int l=ni(),r=ni(); long sz=r-l+1; sz=(sz*(sz-1))/2; if(d==1 && sz%2==0) d=1; else if(d==1 && sz%2!=0) d=0; else if(d==0 && sz%2==0) d=0; else if(d==0 && sz%2!=0) d=1; if(d==1) pw.println("even"); else pw.println("odd"); } } int a[]; long BIT[]; void update(int n,int x,int val){ a[x]=val; for(;x<=n;x+=(x&-x)) BIT[x]+=val; } long querry(int x){ long ans=0; for(;x>0;x-=(x&-x)) ans+=BIT[x]; return ans; } static long d, x, y; static void extendedEuclid(long A, long B) { if(B == 0) { d = A; x = 1; y = 0; } else { extendedEuclid(B, A%B); long temp = x; x = y; y = temp - (A/B)*y; } } static long modInverse(long A, long M) { extendedEuclid(A,M); return (x%M+M)%M; } long M=(long)1e9+7; InputStream is; PrintWriter pw; String INPUT = ""; void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); pw = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); pw.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new A().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); } }
quadratic
911_D. Inversion Counting
CODEFORCES
import java.util.*; import java.io.*; public class P911d { private static void solve() { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } int cnt = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) { cnt++; } } } cnt %= 2; int m = nextInt(); for (int i = 0; i < m; i++) { int l = nextInt(); int r = nextInt(); int size = r - l + 1; int sum = (size * (size - 1)) / 2; sum %= 2; cnt += sum; cnt %= 2; out.println(cnt == 0 ? "even" : "odd"); } } private static void run() { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } private static StringTokenizer st; private static BufferedReader br; private static PrintWriter out; private static String next() { while (st == null || !st.hasMoreElements()) { String s; try { s = br.readLine(); } catch (IOException e) { return null; } st = new StringTokenizer(s); } return st.nextToken(); } private static int nextInt() { return Integer.parseInt(next()); } private static long nextLong() { return Long.parseLong(next()); } public static void main(String[] args) { run(); } }
quadratic
911_D. Inversion Counting
CODEFORCES
//app.は全部けす import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; //流す前にfinalにする public final class EcRound35DApplication { public static void main(String[] args) { Input input = new Input(); input = SystemInput(); List<String> resultList = run(input); for(String result:resultList){ System.out.println(result); } } //流す前にstaticにする private static void tester(Integer no,List<Integer> inputList,List<String> answer) { Input input = new Input(); input.setInput(inputList); List<String> result = run(input); if(result.equals(answer)) { System.out.println("No." + no + ":OK"); } else { System.out.println("No." + no + ":failed"); System.out.println("result:" + result); System.out.println("answer:" + answer); } } //流す前にstaticにする private static Input SystemInput() { Input input = new Input(); Scanner sc = new Scanner(System.in); List<Integer> inputList = new ArrayList<Integer>(); while(sc.hasNextInt()) { inputList.add(sc.nextInt()); } input.setInput(inputList); sc.close(); return input; } //流す前にstaticにする private static List<String> run(Input input) { List<String> result = new ArrayList<String>(); List<Integer> permutation = input.getPermutationList(); Integer count; count = inversion(permutation); for(Integer i = 0;i < input.getQueryNum();i++) { count = count + change(input.getQuery(i)); result.add(evenOdd(count)); } return result; } //カウントする private static Integer inversion(List<Integer>permutation) { String result = new String(); Integer inversionCount = 0; for(Integer i = 0; i < permutation.size(); i++) { for(Integer j = i + 1; j < permutation.size(); j++) { if(permutation.get(i) > permutation.get(j)) { inversionCount++; } } } return inversionCount; } //交換時追加分カウント private static Integer change(Query query) { Integer result; result = query.getLength() * (query.getLength() - 1) / 2; return result; } //判定する private static String evenOdd(Integer i) { if(i % 2 == 0) { return "even"; } else { return "odd"; } } private static class Query{ private Integer l; private Integer r; public void setQuery(Integer l,Integer r) { this.l = l; this.r = r; } public Integer getL() { return l; } public void setL(Integer l) { this.l = l; } public Integer getR() { return r; } public void setR(Integer r) { this.r = r; } public Integer getLength(){ return r - l + 1; } } //流す前にstaticにする private static class Input{ private Integer length; private List<Integer> permutationList = new ArrayList<Integer>(); private Integer queryNum; private List<Query> queryList = new ArrayList<Query>(); public void setInput(List<Integer> inputList) { this.length = inputList.get(0); setPermutationList(inputList.subList(1, length+1)); this.queryNum = inputList.get(length+1); for(Integer j = length+2; j < inputList.size()-1; j = j + 2) { addQueryList(inputList.get(j),inputList.get(j+1)); } // checkInput(); } public void checkInput() { System.out.println("permutation length:" + permutationList.size()); System.out.println("permutation:" + permutationList); System.out.println("query length:" + queryList.size()); System.out.println("queries:"); for(Integer i = 0;i < queryList.size();i++) { System.out.println(this.getQueryL(i) + " " + this.getQueryR(i)); } } public Integer getLength() { return length; } public void setLength(Integer length) { this.length = length; } public List<Integer> getPermutationList() { return permutationList; } public void setPermutationList(List<Integer> permutationList) { this.permutationList = permutationList; } public Integer getPermutation(Integer i) { return permutationList.get(i); } public void addPermutationList(Integer newPermutation) { this.permutationList.add(newPermutation); } public Integer getQueryNum() { return queryNum; } public void setQueryNum(Integer queryNum) { this.queryNum = queryNum; } public Query getQuery(Integer i) { return queryList.get(i); } public Integer getQueryL(Integer i) { return queryList.get(i).getL(); } public Integer getQueryR(Integer i) { return queryList.get(i).getR(); } public List<Query> getQueryList() { return queryList; } public void setQueryList(List<Query> queryList) { this.queryList = queryList; } public void addQueryList(Query newQuery) { this.queryList.add(newQuery); } public void addQueryList(Integer l,Integer r) { Query newQuery = new Query(); newQuery.setQuery(l, r); addQueryList(newQuery); } } }
quadratic
911_D. Inversion Counting
CODEFORCES